Skip to content

Commit

Permalink
SQL Clean-up Script for Network Resets (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 authored Jan 22, 2024
1 parent 4a7d29d commit ba34362
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions scripts/db/cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
DO $$
DECLARE
drop_schemas_cmd text;
drop_tables_cmd text;
drop_views_cmd text;
BEGIN
-- Prepare command to drop all non-system schemas except 'public'
SELECT INTO drop_schemas_cmd
string_agg('DROP SCHEMA ' || quote_ident(schema_name) || ' CASCADE', '; ')
FROM information_schema.schemata
WHERE schema_name NOT IN ('public', 'information_schema', 'pg_catalog', 'pg_toast', 'pg_temp_4', 'pg_toast_temp_4');

-- Execute if command is not null
IF drop_schemas_cmd IS NOT NULL THEN
EXECUTE drop_schemas_cmd;
END IF;

-- Prepare command to drop all user-created tables in the 'public' schema
SELECT INTO drop_tables_cmd
string_agg('DROP TABLE IF EXISTS public.' || quote_ident(table_name) || ' CASCADE', '; ')
FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE'
AND table_name NOT LIKE 'pg_%';

-- Execute if command is not null
IF drop_tables_cmd IS NOT NULL THEN
EXECUTE drop_tables_cmd;
END IF;

-- Prepare command to drop all user-created views in the 'public' schema
SELECT INTO drop_views_cmd
string_agg('DROP VIEW IF EXISTS public.' || quote_ident(table_name) || ' CASCADE', '; ')
FROM information_schema.views
WHERE table_schema = 'public' AND table_name NOT LIKE 'pg_%';

-- Execute if command is not null
IF drop_views_cmd IS NOT NULL THEN
EXECUTE drop_views_cmd;
END IF;
END $$;

0 comments on commit ba34362

Please sign in to comment.