-
Notifications
You must be signed in to change notification settings - Fork 6
/
DeleteAdempiereClient.SQL
executable file
·197 lines (176 loc) · 7.11 KB
/
DeleteAdempiereClient.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*************************************************************************
* The contents of this file are subject to the LGPL.
* The Original Author is Peter Shen
* The Personal Information of the contributors:
* 1, Peter Shen
* Shanghai, China
*
*************************************************************************
* $Id: RemoveClient.sql,v 1.1 2005/01/26 14:47:29 pshen Exp $
***
* Title: Remove a Client in Adempiere.
* Description:
* - Disable all the constraints and triggers
* - Delete the records from all the table
* - Enable all the constraints and triggers
*
* Warning:
* - Please backup all the data before you use this script
*
* Guide:
* - Log in the oracle sqlplus (or other sqlplus tools) with the schema from which you want to delete data
* - Changed the v_Client_ID to AD_Client_ID which you want to delete
* - This script would be a little slow, please wait patiently
*
* Known issues:
* - the script enables the triggers/constraints that you previously disabled for some reasons
* - if your tables are not in Application Dictionary (AD_Table) then the script won't touch them
*
* Contributor(s):
* Carlos Ruiz - globalqss - 2006/12/11
* - Test it in Oracle 10G XE
* - Add reference columns not named AD_Client_ID (like AD_Replication.Remote_Client_ID
* - Add novalidate variable for faster but unsure process (or even for failed)
* Teo Sarca <[email protected]> - 2007/06/12
* - fixed: trigger enabling issue
* - fixed: check if the AD_Table row is really a table
* - fixed: role dependencies issue
* - fixed: more verbose - this is a critical task so it should be more verbose
* - tested on Oracle 10G XE
************************************************************************/
set serveroutput on
DECLARE
/**
* Please change this one to any client id you want to delete
**/
v_client_id NUMBER := 1000000;
-- novalidate will make the process faster but the constraints won't be validated
-- v_novalidate VARCHAR2 (10) := ' '; -- slower but sure
v_novalidate VARCHAR2(10) := 'novalidate'; -- faster but unsure
v_sql1 VARCHAR2 (1024);
CURSOR cur_contraints_ena
IS
SELECT table_name, constraint_name
FROM user_constraints
WHERE constraint_type = 'R' AND status = 'ENABLED';
CURSOR cur_contraints_dis
IS
SELECT table_name, constraint_name
FROM user_constraints
WHERE constraint_type = 'R' AND status = 'DISABLED';
CURSOR cur_triggers_ena
IS
SELECT trigger_name
FROM user_triggers
WHERE status = 'ENABLED';
CURSOR cur_triggers_dis
IS
SELECT trigger_name
FROM user_triggers
WHERE status = 'DISABLED';
CURSOR cur_removedata
IS
SELECT 'delete from '
|| tablename
|| ' where AD_Client_ID='
|| v_client_id AS v_sql
, a.TableName
FROM AD_TABLE a
WHERE a.isview = 'N'
AND EXISTS (
SELECT ad_column_id
FROM AD_COLUMN c
WHERE a.ad_table_id = c.ad_table_id
AND UPPER (c.columnname) = 'AD_CLIENT_ID')
-- Assure that the table is really a table in database
AND EXISTS (SELECT 1 FROM user_objects dbo WHERE UPPER(dbo.object_name)=UPPER(a.TableName) AND dbo.object_type='TABLE')
UNION
SELECT 'delete from '
|| t.tablename
|| ' where '
|| columnname
|| '='
|| v_client_id AS v_sql
, t.TableName
FROM AD_COLUMN c, AD_TABLE t
WHERE ad_reference_value_id = 129
AND UPPER (columnname) <> 'AD_CLIENT_ID'
AND t.ad_table_id = c.ad_table_id
-- Assure that the table is really a table in database
AND EXISTS (SELECT 1 FROM user_objects dbo WHERE UPPER(dbo.object_name)=UPPER(t.TableName) AND dbo.object_type='TABLE')
;
-- Role dependencies
cursor cur_role_dep is
select TableName from AD_Table t, AD_Column c
where t.AD_Table_ID=c.AD_Table_ID AND t.TableName<>'AD_Role_ID' AND c.ColumnName='AD_Role_ID';
BEGIN
DBMS_OUTPUT.PUT_LINE (' Delete Client Where AD_Client_ID=' || v_client_id);
/****************************************************************
* Disable all triggers and constraints one by one
****************************************************************/
DBMS_OUTPUT.PUT_LINE (' Disable the triggers ');
FOR p IN cur_triggers_ena
LOOP
v_sql1 := 'alter trigger ' || p.trigger_name || ' disable ';
DBMS_OUTPUT.put_line ('..' || v_sql1);
EXECUTE IMMEDIATE v_sql1;
END LOOP; -- Disable contraints
DBMS_OUTPUT.PUT_LINE (' Disable the contraints '||v_novalidate);
FOR p IN cur_contraints_ena
LOOP
v_sql1 :=
'alter table '
|| p.table_name
|| ' disable constraint '
|| p.constraint_name;
DBMS_OUTPUT.put_line ('..' || v_sql1);
EXECUTE IMMEDIATE v_sql1;
END LOOP; -- Disable contraints
/****************************************************************
* Remove all the records belongs to that client
****************************************************************/
FOR p IN cur_removedata
LOOP
v_sql1 := p.v_sql;
DBMS_OUTPUT.put_line ('..' || v_sql1);
EXECUTE IMMEDIATE v_sql1;
IF SQL%ROWCOUNT > 0 THEN
DBMS_OUTPUT.put_line('Deleted from '||p.TableName || ': #'||SQL%ROWCOUNT);
END IF;
COMMIT;
END LOOP; -- Remove data
FOR p IN cur_role_dep loop
v_sql1 := 'delete from '||p.TableName||' where AD_Role_ID not in (select AD_Role_ID from AD_Role)';
EXECUTE IMMEDIATE v_sql1;
IF SQL%ROWCOUNT > 0 THEN
DBMS_OUTPUT.put_line('Deleted from '||p.TableName||': #'||SQL%ROWCOUNT);
END IF;
COMMIT;
END LOOP;
/****************************************************************
* Disable all constraints and triggers one by one
****************************************************************/
DBMS_OUTPUT.PUT_LINE (' Enable the contraints '||v_novalidate);
FOR p IN cur_contraints_dis
LOOP
v_sql1 :=
'alter table '
|| p.table_name
|| ' enable '
|| v_novalidate
|| ' constraint '
|| p.constraint_name;
DBMS_OUTPUT.put_line ('..' || v_sql1);
EXECUTE IMMEDIATE v_sql1;
END LOOP; -- Enable contraints
DBMS_OUTPUT.PUT_LINE (' Enable the triggers ');
FOR p IN cur_triggers_dis
LOOP
v_sql1 := 'alter trigger ' || p.trigger_name || ' enable ';
DBMS_OUTPUT.put_line ('..' || v_sql1);
EXECUTE IMMEDIATE v_sql1;
END LOOP; -- Enable triggers
COMMIT;
DBMS_OUTPUT.PUT_LINE ('Done. ');
END;