-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_triggers.py
351 lines (321 loc) · 13.5 KB
/
create_triggers.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
__author__ = 'valerio cosentino'
import database_manager as dbman
#the triggers are created to filter out the projects marked as "deleted",
#however other filters could be implemented (selecting a sub-set of projects, projects with more than X followers, etc.)
def main():
dbman.execute_sql_statement("USE " + dbman.DATABASE)
if dbman.check_table_exists('projects'):
trigger_delete_insert_project = """DROP TRIGGER IF EXISTS insert_project;"""
trigger_insert_project = """
CREATE TRIGGER insert_project
BEFORE INSERT
ON projects FOR EACH ROW
BEGIN
IF NEW.deleted = 1 THEN
SET NEW.id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_project)
dbman.execute_sql_statement(trigger_insert_project)
if dbman.check_table_exists("issues"):
trigger_delete_insert_issue = """DROP TRIGGER IF EXISTS insert_issue;"""
trigger_insert_issue = """
CREATE TRIGGER insert_issue
BEFORE INSERT
ON issues FOR EACH ROW
BEGIN
DECLARE project_found INTEGER;
SELECT id INTO project_found
FROM projects
WHERE id = NEW.repo_id;
IF (project_found IS NULL OR project_found = -1) THEN
SET NEW.id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_issue)
dbman.execute_sql_statement(trigger_insert_issue)
if dbman.check_table_exists("commits"):
trigger_delete_insert_commit = """DROP TRIGGER IF EXISTS insert_commit;"""
trigger_insert_commit = """
CREATE TRIGGER insert_commit
BEFORE INSERT
ON commits FOR EACH ROW
BEGIN
DECLARE project_found INTEGER;
SELECT id INTO project_found
FROM projects
WHERE id = NEW.project_id;
IF (project_found IS NULL OR project_found = -1) THEN
SET NEW.id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_commit)
dbman.execute_sql_statement(trigger_insert_commit)
if dbman.check_table_exists("project_members"):
trigger_delete_insert_project_member = """DROP TRIGGER IF EXISTS insert_project_member;"""
trigger_insert_project_member = """
CREATE TRIGGER insert_project_member
BEFORE INSERT
ON project_members FOR EACH ROW
BEGIN
DECLARE project_found INTEGER;
SELECT id INTO project_found
FROM projects
WHERE id = NEW.repo_id;
IF (project_found IS NULL OR project_found = -1) THEN
SET NEW.repo_id = -1;
SET NEW.user_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_project_member)
dbman.execute_sql_statement(trigger_insert_project_member)
if dbman.check_table_exists("watchers"):
trigger_delete_insert_watcher = """DROP TRIGGER IF EXISTS insert_watcher;"""
trigger_insert_watcher = """
CREATE TRIGGER insert_watcher
BEFORE INSERT
ON watchers FOR EACH ROW
BEGIN
DECLARE project_found INTEGER;
SELECT id INTO project_found
FROM projects
WHERE id = NEW.repo_id;
IF (project_found IS NULL OR project_found = -1) THEN
SET NEW.repo_id = -1;
SET NEW.user_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_watcher)
dbman.execute_sql_statement(trigger_insert_watcher)
if dbman.check_table_exists("forks"):
trigger_delete_insert_fork = """DROP TRIGGER IF EXISTS insert_fork;"""
trigger_insert_fork = """
CREATE TRIGGER insert_fork
BEFORE INSERT
ON forks FOR EACH ROW
BEGIN
DECLARE project_found INTEGER;
SELECT id INTO project_found
FROM projects
WHERE id = NEW.forked_from_id;
IF (project_found IS NULL OR project_found = -1) THEN
SET NEW.fork_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_fork)
dbman.execute_sql_statement(trigger_insert_fork)
if dbman.check_table_exists("repo_labels"):
trigger_delete_insert_repo_label = """DROP TRIGGER IF EXISTS insert_repo_label;"""
trigger_insert_repo_label = """
CREATE TRIGGER insert_repo_label
BEFORE INSERT
ON repo_labels FOR EACH ROW
BEGIN
DECLARE project_found INTEGER;
SELECT id INTO project_found
FROM projects
WHERE id = NEW.repo_id;
IF (project_found IS NULL OR project_found = -1) THEN
SET NEW.id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_repo_label)
dbman.execute_sql_statement(trigger_insert_repo_label)
if dbman.check_table_exists("repo_milestones"):
trigger_delete_insert_repo_milestone = """DROP TRIGGER IF EXISTS insert_repo_milestone;"""
trigger_insert_repo_milestone = """
CREATE TRIGGER insert_repo_milestone
BEFORE INSERT
ON repo_milestones FOR EACH ROW
BEGIN
DECLARE project_found INTEGER;
SELECT id INTO project_found
FROM projects
WHERE id = NEW.repo_id;
IF (project_found IS NULL OR project_found = -1) THEN
SET NEW.id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_repo_milestone)
dbman.execute_sql_statement(trigger_insert_repo_milestone)
if dbman.check_table_exists("pull_requests"):
trigger_delete_insert_pull_request = """DROP TRIGGER IF EXISTS insert_pull_request;"""
trigger_insert_pull_request = """
CREATE TRIGGER insert_pull_request
BEFORE INSERT
ON pull_requests FOR EACH ROW
BEGIN
DECLARE project_found INTEGER;
SELECT id INTO project_found
FROM projects
WHERE id = NEW.base_repo_id;
IF (project_found IS NULL OR project_found = -1) THEN
SET NEW.id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_pull_request)
dbman.execute_sql_statement(trigger_insert_pull_request)
if dbman.check_table_exists("project_commits"):
trigger_delete_insert_project_commit = """DROP TRIGGER IF EXISTS insert_project_commit;"""
trigger_insert_project_commit = """
CREATE TRIGGER insert_project_commit
BEFORE INSERT
ON project_commits FOR EACH ROW
BEGIN
DECLARE project_found INTEGER;
SELECT id INTO project_found
FROM projects
WHERE id = NEW.project_id;
IF (project_found IS NULL OR project_found = -1) THEN
SET NEW.project_id = -1;
SET NEW.commit_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_project_commit)
dbman.execute_sql_statement(trigger_insert_project_commit)
if dbman.check_table_exists("commit_parents"):
trigger_delete_insert_commit_parents = """DROP TRIGGER IF EXISTS insert_commit_parents;"""
trigger_insert_commit_parents = """
CREATE TRIGGER insert_commit_parents
BEFORE INSERT
ON commit_parents FOR EACH ROW
BEGIN
DECLARE commit_found INTEGER;
DECLARE commit_parent_found INTEGER;
SELECT id INTO commit_found
FROM commits
WHERE id = NEW.commit_id;
SELECT id INTO commit_parent_found
FROM commits
WHERE id = NEW.parent_id;
IF (commit_found IS NULL OR commit_found = -1 OR commit_parent_found IS NULL OR commit_parent_found = -1) THEN
SET NEW.commit_id = -1;
SET NEW.parent_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_commit_parents)
dbman.execute_sql_statement(trigger_insert_commit_parents)
if dbman.check_table_exists("commit_comments"):
trigger_delete_insert_commit_comments = """DROP TRIGGER IF EXISTS insert_commit_comments;"""
trigger_insert_commit_comments = """
CREATE TRIGGER insert_commit_comments
BEFORE INSERT
ON commit_comments FOR EACH ROW
BEGIN
DECLARE commit_found INTEGER;
SELECT id INTO commit_found
FROM commits
WHERE id = NEW.commit_id;
IF (commit_found IS NULL OR commit_found = -1) THEN
SET NEW.id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_commit_comments)
dbman.execute_sql_statement(trigger_insert_commit_comments)
if dbman.check_table_exists("pull_request_history"):
trigger_delete_insert_pull_request_history = """DROP TRIGGER IF EXISTS insert_pull_request_history;"""
trigger_insert_pull_request_history = """
CREATE TRIGGER insert_pull_request_history
BEFORE INSERT
ON pull_request_history FOR EACH ROW
BEGIN
DECLARE pull_request_found INTEGER;
SELECT id INTO pull_request_found
FROM pull_requests
WHERE id = NEW.pull_request_id;
IF (pull_request_found IS NULL OR pull_request_found = -1) THEN
SET NEW.id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_pull_request_history)
dbman.execute_sql_statement(trigger_insert_pull_request_history)
if dbman.check_table_exists("pull_request_comments"):
trigger_delete_insert_pull_request_comment = """DROP TRIGGER IF EXISTS insert_pull_request_comment;"""
trigger_insert_pull_request_comment = """
CREATE TRIGGER insert_pull_request_comment
BEFORE INSERT
ON pull_request_comments FOR EACH ROW
BEGIN
DECLARE pull_request_found INTEGER;
SELECT id INTO pull_request_found
FROM pull_requests
WHERE id = NEW.pull_request_id;
IF (pull_request_found IS NULL OR pull_request_found = -1) THEN
SET NEW.comment_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_pull_request_comment)
dbman.execute_sql_statement(trigger_insert_pull_request_comment)
if dbman.check_table_exists("pull_request_commits"):
trigger_delete_insert_pull_request_commit = """DROP TRIGGER IF EXISTS insert_pull_request_commit;"""
trigger_insert_pull_request_commit = """
CREATE TRIGGER insert_pull_request_commit
BEFORE INSERT
ON pull_request_commits FOR EACH ROW
BEGIN
DECLARE pull_request_found INTEGER;
SELECT id INTO pull_request_found
FROM pull_requests
WHERE id = NEW.pull_request_id;
IF (pull_request_found IS NULL OR pull_request_found = -1) THEN
SET NEW.pull_request_id = -1;
SET NEW.commit_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_pull_request_commit)
dbman.execute_sql_statement(trigger_insert_pull_request_commit)
if dbman.check_table_exists("issue_labels"):
trigger_delete_insert_issue_label = """DROP TRIGGER IF EXISTS insert_issue_label;"""
trigger_insert_issue_label = """
CREATE TRIGGER insert_issue_label
BEFORE INSERT
ON issue_labels FOR EACH ROW
BEGIN
DECLARE issue_found INTEGER;
SELECT id INTO issue_found
FROM issues
WHERE id = NEW.issue_id;
IF (issue_found IS NULL OR issue_found = -1) THEN
SET NEW.issue_id = -1;
SET NEW.label_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_issue_label)
dbman.execute_sql_statement(trigger_insert_issue_label)
if dbman.check_table_exists("issue_events"):
trigger_delete_insert_issue_event = """DROP TRIGGER IF EXISTS insert_issue_event;"""
trigger_insert_issue_event = """
CREATE TRIGGER insert_issue_event
BEFORE INSERT
ON issue_events FOR EACH ROW
BEGIN
DECLARE issue_found INTEGER;
SELECT id INTO issue_found
FROM issues
WHERE id = NEW.issue_id;
IF (issue_found IS NULL OR issue_found = -1) THEN
SET NEW.event_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_issue_event)
dbman.execute_sql_statement(trigger_insert_issue_event)
if dbman.check_table_exists("issue_comments"):
trigger_delete_insert_issue_comment = """DROP TRIGGER IF EXISTS insert_issue_comment;"""
trigger_insert_issue_comment = """
CREATE TRIGGER insert_issue_comment
BEFORE INSERT
ON issue_comments FOR EACH ROW
BEGIN
DECLARE issue_found INTEGER;
SELECT id INTO issue_found
FROM issues
WHERE id = NEW.issue_id;
IF (issue_found IS NULL OR issue_found = -1) THEN
SET NEW.comment_id = -1;
END IF;
END;"""
dbman.execute_sql_statement(trigger_delete_insert_issue_comment)
dbman.execute_sql_statement(trigger_insert_issue_comment)
dbman.close_connection()
if __name__ == "__main__":
main()