-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclean-db.sql
More file actions
47 lines (40 loc) · 1.25 KB
/
clean-db.sql
File metadata and controls
47 lines (40 loc) · 1.25 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
-- purge all data in DB keeping model
do $$
declare
tbl record;
begin
for tbl in (select relname
from pg_class c, pg_namespace ns
where relkind ='r'
and c.relnamespace = ns.oid
and ns.nspname = 'public'
order by 1) loop
raise notice '%', tbl.relname;
execute 'alter table ' || tbl.relname || ' disable trigger all';
end loop;
for tbl in (select relname
from pg_class c, pg_namespace ns
where relkind ='r'
and c.relnamespace = ns.oid
and ns.nspname = 'public'
order by 1) loop
raise notice '%', tbl.relname;
execute 'delete from ' || tbl.relname;
end loop;
end$$;
-- now you can do whatever you need without data
-- enable triggers + constraints
do $$
declare
tbl record;
begin
for tbl in (select relname
from pg_class c, pg_namespace ns
where relkind ='r'
and c.relnamespace = ns.oid
and ns.nspname = 'public'
order by 1) loop
raise notice '%', tbl.relname;
execute 'alter table ' || tbl.relname || ' enable trigger all';
end loop;
end$$;