Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Clean-up Script for Network Resets #27

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 $$;