#!/bin/bash
[ "$2" == "" ] && exec echo "Usage: $0 <DATABASE_NAME> <TABLE_NAME_PATTERN>"

DATABASE_NAME=$1
TABLE_NAME_PATTERN=$2

psql -U postgres ${DATABASE_NAME} -c "
DO \$\$
DECLARE
    r RECORD;
BEGIN
    FOR r IN
        SELECT tablename
        FROM pg_tables
        WHERE schemaname = 'public'
          AND tablename LIKE '%${TABLE_NAME_PATTERN}%'
    LOOP
        EXECUTE format('DROP TABLE IF EXISTS public.%I CASCADE;', r.tablename);
    END LOOP;
END \$\$;                                    
"
