-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleanSchema.sql
133 lines (114 loc) · 4.62 KB
/
cleanSchema.sql
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
-- Drop all objects of a given schema
-- Thanks to Lucas Bernardini for the script wireframe
PROMPT INFO: BEGIN cleanSchema.sql
var v_owner varchar2(30);
exec :v_owner := '&1';
set serveroutput on size unlimited verify off
PROMPT INFO: Object count BEFORE cleaning the schema
select object_type, count(*)
from dba_objects
where owner = upper(:v_owner)
group by object_type
;
declare
cmd varchar2(200);
cursor tables is
select t.owner, t.table_name
from dba_tables t
where t.owner = upper(:v_owner)
and t.table_name not in (select object_name
from dba_objects
where owner = t.owner
and object_type = 'MATERIALIZED VIEW');
cursor miscObjects is
select object_type, owner, object_name
from dba_objects
where object_type not in ('TABLE', 'INDEX', 'TRIGGER', 'LOB', 'PACKAGE BODY', 'JOB', 'SCHEDULE', 'DATABASE LINK')
and object_type not like '%LINK%'
and object_type not like '%PARTITION%'
and owner = upper(:v_owner)
order by 1;
cursor pkgs is
select object_type, owner, object_name
from dba_objects
where object_type = 'PACKAGE BODY'
and owner = upper(:v_owner)
order by 1;
cursor jobs is
select owner, object_name
from dba_objects
where object_type = 'JOB'
and owner = upper(:v_owner)
order by 1;
cursor schedules is
select owner, object_name
from dba_objects
where object_type = 'SCHEDULE'
and owner = upper(:v_owner)
order by 1;
begin
dbms_output.put_line('INFO: Dropping objects from schema ' || :v_owner);
dbms_output.put_line('INFO: Dropping tables...');
for tbl in tables loop
begin
execute immediate 'drop table '||tbl.owner||'.'||tbl.table_name||' cascade constraints purge';
--dbms_output.put_line('drop table '||tbl.owner||'.'||tbl.table_name||' cascade constraints purge');
exception
when others then
dbms_output.put_line('ERROR: Drop failed on Table: '||tbl.owner||'.'||tbl.table_name||' --> '|| sqlerrm);
end;
end loop;
dbms_output.put_line('INFO: Done.');
dbms_output.put_line('INFO: Dropping other objects...');
for obj in miscObjects loop
begin
execute immediate 'drop '||obj.object_type||' '||obj.owner||'.'||obj.object_name;
--dbms_output.put_line('drop '||obj.object_type||' '||obj.owner||'.'||obj.object_name);
exception
when others then
dbms_output.put_line('ERROR: Drop failed on '||obj.object_type||': '||obj.owner||'.'||obj.object_name||' --> '|| sqlerrm);
end;
end loop;
dbms_output.put_line('INFO: Done.');
dbms_output.put_line('INFO: Dropping Packages...');
for pkg in pkgs loop
begin
execute immediate 'drop '||pkg.object_type||' '||pkg.owner||'.'||pkg.object_name;
--dbms_output.put_line('drop '||pkg.object_type||' '||pkg.owner||'.'||pkg.object_name);
exception
when others then
dbms_output.put_line('ERROR: Drop failed on '||pkg.object_type||': '||pkg.owner||'.'||pkg.object_name||' --> '|| sqlerrm);
end;
end loop;
dbms_output.put_line('INFO: Done.');
dbms_output.put_line('INFO: Dropping jobs...');
for job in jobs loop
begin
dbms_scheduler.drop_job(job.owner||'.'||job.object_name);
--dbms_output.put_line('dbms_scheduler.drop_job('||job.owner||'.'||job.object_name||')');
exception
when others then
dbms_output.put_line('ERROR: Drop failed on JOB: '||job.owner||'.'||job.object_name||' --> '|| sqlerrm);
end;
end loop;
dbms_output.put_line('INFO: Done.');
dbms_output.put_line('INFO: Dropping schedules...');
for schedule in schedules loop
begin
dbms_scheduler.drop_schedule(schedule.owner||'.'||schedule.object_name);
--dbms_output.put_line('dbms_scheduler.drop_schedule('||schedule.owner||'.'||schedule.object_name||')');
exception
when others then
dbms_output.put_line('ERROR: Drop failed on SCHEDULE: '||schedule.owner||'.'||schedule.object_name||' --> '|| sqlerrm);
end;
end loop;
dbms_output.put_line('INFO: Done.');
end;
/
PROMPT INFO: Object count AFTER cleaning the schema
select object_type, count(*)
from dba_objects
where owner = upper(:v_owner)
group by object_type
;
PROMPT INFO: END cleanSchema.sql