-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathdrop.sql
More file actions
136 lines (126 loc) · 4.93 KB
/
drop.sql
File metadata and controls
136 lines (126 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
do $$ declare
rec record;
begin
-- schemas
for rec in
select pn.*
from pg_namespace pn
left join pg_depend pd on pd.objid = pn.oid
where pd.deptype is null
and not pn.nspname like any(array['information\_schema', 'pg\_%', '\_analytics', '\_realtime', '\_supavisor', 'pgbouncer', 'pgsodium', 'pgtle', 'supabase\_migrations', 'vault', 'extensions', 'public'])
and pn.nspowner::regrole::text != 'supabase_admin'
loop
-- If an extension uses a schema it doesn't create, dropping the schema will cascade to also
-- drop the extension. But if an extension creates its own schema, dropping the schema will
-- throw an error. Hence, we drop schemas first while excluding those created by extensions.
raise notice 'dropping schema: %', rec.nspname;
execute format('drop schema if exists %I cascade', rec.nspname);
end loop;
-- extensions
for rec in
select *
from pg_extension p
where p.extname not in ('pg_graphql', 'pg_net', 'pg_stat_statements', 'pgcrypto', 'pgjwt', 'pgsodium', 'plpgsql', 'supabase_vault', 'uuid-ossp')
loop
raise notice 'dropping extension: %', rec.extname;
execute format('drop extension if exists %I cascade', rec.extname);
end loop;
-- functions
for rec in
select *
from pg_proc p
where p.pronamespace::regnamespace::name = 'public'
loop
-- supports aggregate, function, and procedure
raise notice 'dropping function: %.%', rec.pronamespace::regnamespace::name, rec.proname;
execute format('drop routine if exists %I.%I(%s) cascade', rec.pronamespace::regnamespace::name, rec.proname, pg_catalog.pg_get_function_identity_arguments(rec.oid));
end loop;
-- views (necessary for views referencing objects in Supabase-managed schemas)
for rec in
select *
from pg_class c
where
c.relnamespace::regnamespace::name = 'public'
and c.relkind = 'v'
loop
raise notice 'dropping view: %.%', rec.relnamespace::regnamespace::name, rec.relname;
execute format('drop view if exists %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);
end loop;
-- materialized views (necessary for materialized views referencing objects in Supabase-managed schemas)
for rec in
select *
from pg_class c
where
c.relnamespace::regnamespace::name = 'public'
and c.relkind = 'm'
loop
raise notice 'dropping materialized view: %.%', rec.relnamespace::regnamespace::name, rec.relname;
execute format('drop materialized view if exists %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);
end loop;
-- tables (cascade to dependent objects)
for rec in
select *
from pg_class c
where
c.relnamespace::regnamespace::name = 'public'
and c.relkind not in ('c', 'S', 'v', 'm')
order by c.relkind desc
loop
-- supports all table like relations, except views, complex types, and sequences
raise notice 'dropping table: %.%', rec.relnamespace::regnamespace::name, rec.relname;
execute format('drop table if exists %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);
end loop;
-- truncate tables in auth, webhooks, and migrations schema
for rec in
select *
from pg_class c
where
(c.relnamespace::regnamespace::name = 'auth' and c.relname != 'schema_migrations'
or c.relnamespace::regnamespace::name = 'supabase_functions' and c.relname != 'migrations'
or c.relnamespace::regnamespace::name = 'supabase_migrations')
and c.relkind = 'r'
loop
raise notice 'truncating table: %.%', rec.relnamespace::regnamespace::name, rec.relname;
execute format('truncate %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);
end loop;
-- sequences
for rec in
select *
from pg_class c
where
c.relnamespace::regnamespace::name = 'public'
and c.relkind = 's'
loop
raise notice 'dropping sequence: %.%', rec.relnamespace::regnamespace::name, rec.relname;
execute format('drop sequence if exists %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);
end loop;
-- types
for rec in
select *
from pg_type t
where
t.typnamespace::regnamespace::name = 'public'
and typtype != 'b'
loop
raise notice 'dropping type: %.%', rec.typnamespace::regnamespace::name, rec.typname;
execute format('drop type if exists %I.%I cascade', rec.typnamespace::regnamespace::name, rec.typname);
end loop;
-- policies
for rec in
select *
from pg_policies p
loop
raise notice 'dropping policy: %', rec.policyname;
execute format('drop policy if exists %I on %I.%I cascade', rec.policyname, rec.schemaname, rec.tablename);
end loop;
-- publications
for rec in
select *
from pg_publication p
where
not p.pubname like any(array['supabase\_realtime%', 'realtime\_messages%'])
loop
raise notice 'dropping publication: %', rec.pubname;
execute format('drop publication if exists %I', rec.pubname);
end loop;
end $$;