-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL Clean-up Script for Network Resets (#27)
Signed-off-by: Alfredo Gutierrez <[email protected]>
- Loading branch information
1 parent
4a7d29d
commit ba34362
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 $$; |