-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmysql_sp_audit_setup.sql
401 lines (313 loc) · 16.4 KB
/
mysql_sp_audit_setup.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
-- --------------------------------------------------------------------
-- MySQL Audit Trigger
-- Copyright (c) 2014 Du T. Dang. MIT License
-- https://github.com/hotmit/mysql-sp-audit
-- Version: v1.0
-- Build Date: Wed, 22 Oct 2014 16:42:08 GMT
-- --------------------------------------------------------------------
DELIMITER $$
DROP TABLE IF EXISTS `zaudit`
$$
CREATE TABLE `zaudit` (
`audit_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user` varchar(255) DEFAULT NULL,
`table_name` varchar(255) DEFAULT NULL,
`pk1` varchar(255) DEFAULT NULL,
`pk2` varchar(255) DEFAULT NULL,
`action` varchar(6) DEFAULT NULL COMMENT 'Values: insert|update|delete',
`timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`audit_id`),
KEY `pk_index` (`table_name`,`pk1`,`pk2`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
$$
DROP TABLE IF EXISTS `zaudit_meta`
$$
CREATE TABLE `zaudit_meta` (
`audit_meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`audit_id` bigint(20) unsigned NOT NULL,
`col_name` varchar(255) NOT NULL,
`old_value` longtext DEFAULT NULL,
`new_value` longtext DEFAULT NULL,
PRIMARY KEY (`audit_meta_id`),
KEY `zaudit_meta_index` (`audit_id`,`col_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
$$
DROP PROCEDURE IF EXISTS `zsp_generate_audit`;
$$
CREATE PROCEDURE `zsp_generate_audit` (IN audit_schema_name VARCHAR(255), IN audit_table_name VARCHAR(255), OUT script LONGTEXT, OUT errors LONGTEXT)
main_block: BEGIN
DECLARE trg_insert, trg_update, trg_delete, vw_audit, vw_audit_meta, out_errors LONGTEXT;
DECLARE stmt, header LONGTEXT;
DECLARE at_id1, at_id2 LONGTEXT;
DECLARE c INTEGER;
-- Default max length of GROUP_CONCAT IS 1024
SET SESSION group_concat_max_len = 100000;
SET out_errors := '';
-- Check to see if the specified table exists
SET c := (SELECT COUNT(*) FROM information_schema.tables
WHERE BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND BINARY table_name = BINARY audit_table_name);
IF c <> 1 THEN
SET out_errors := CONCAT( out_errors, '\n', 'The table you specified `', audit_schema_name, '`.`', audit_table_name, '` does not exists.' );
LEAVE main_block;
END IF;
-- Check audit and meta table exists
SET c := (SELECT COUNT(*) FROM information_schema.tables
WHERE BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND (BINARY table_name = BINARY 'zaudit' OR BINARY table_name = BINARY 'zaudit_meta') );
IF c <> 2 THEN
SET out_errors := CONCAT( out_errors, '\n', 'Audit table structure do not exists, please check or run the audit setup script again.' );
END IF;
-- Check triggers exists
SET c := ( SELECT GROUP_CONCAT( TRIGGER_NAME SEPARATOR ', ') FROM information_schema.triggers
WHERE BINARY EVENT_OBJECT_SCHEMA = BINARY audit_schema_name
AND BINARY EVENT_OBJECT_TABLE = BINARY audit_table_name
AND BINARY ACTION_TIMING = BINARY 'AFTER' AND BINARY TRIGGER_NAME NOT LIKE BINARY CONCAT('z', audit_table_name, '_%') GROUP BY EVENT_OBJECT_TABLE );
IF c IS NOT NULL AND LENGTH(c) > 0 THEN
SET out_errors := CONCAT( out_errors, '\n', 'MySQL 5 only supports one trigger per insert/update/delete action. Currently there are these triggers (', c, ') already assigned to `', audit_schema_name, '`.`', audit_table_name, '`. You must remove them before the audit trigger can be applied' );
END IF;
-- Get the first primary key
SET at_id1 := (SELECT COLUMN_NAME FROM information_schema.columns
WHERE BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND BINARY table_name = BINARY audit_table_name
AND column_key = 'PRI' LIMIT 1);
-- Get the second primary key
SET at_id2 := (SELECT COLUMN_NAME FROM information_schema.columns
WHERE BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND BINARY table_name = BINARY audit_table_name
AND column_key = 'PRI' LIMIT 1,1);
-- Check at least one id exists
IF at_id1 IS NULL AND at_id2 IS NULL THEN
SET out_errors := CONCAT( out_errors, '\n', 'The table you specified `', audit_schema_name, '`.`', audit_table_name, '` does not have any primary key.' );
END IF;
SET header := CONCAT(
'-- --------------------------------------------------------------------\n',
'-- MySQL Audit Trigger\n',
'-- Copyright (c) 2014 Du T. Dang. MIT License\n',
'-- https://github.com/hotmit/mysql-sp-audit\n',
'-- --------------------------------------------------------------------\n\n'
);
SET trg_insert := CONCAT( 'DROP TRIGGER IF EXISTS `', audit_schema_name, '`.`z', audit_table_name, '_AINS`\n$$\n',
'CREATE TRIGGER `', audit_schema_name, '`.`z', audit_table_name, '_AINS` AFTER INSERT ON `', audit_schema_name, '`.`', audit_table_name, '` FOR EACH ROW \nBEGIN\n', header );
SET trg_update := CONCAT( 'DROP TRIGGER IF EXISTS `', audit_schema_name, '`.`z', audit_table_name, '_AUPD`\n$$\n',
'CREATE TRIGGER `', audit_schema_name, '`.`z', audit_table_name, '_AUPD` AFTER UPDATE ON `', audit_schema_name, '`.`', audit_table_name, '` FOR EACH ROW \nBEGIN\n', header );
SET trg_delete := CONCAT( 'DROP TRIGGER IF EXISTS `', audit_schema_name, '`.`z', audit_table_name, '_ADEL`\n$$\n',
'CREATE TRIGGER `', audit_schema_name, '`.`z', audit_table_name, '_ADEL` AFTER DELETE ON `', audit_schema_name, '`.`', audit_table_name, '` FOR EACH ROW \nBEGIN\n', header );
SET stmt := 'DECLARE zaudit_last_inserted_id BIGINT(20);\n\n';
SET trg_insert := CONCAT( trg_insert, stmt );
SET trg_update := CONCAT( trg_update, stmt );
SET trg_delete := CONCAT( trg_delete, stmt );
-- ----------------------------------------------------------
-- [ Create Insert Statement Into Audit & Audit Meta Tables ]
-- ----------------------------------------------------------
SET stmt := CONCAT( 'INSERT IGNORE INTO `', audit_schema_name, '`.zaudit (user, table_name, pk1, ', CASE WHEN at_id2 IS NULL THEN '' ELSE 'pk2, ' END , 'action) VALUE ( IFNULL( @zaudit_user, USER() ), ',
'''', audit_table_name, ''', ', 'NEW.`', at_id1, '`, ', IFNULL( CONCAT('NEW.`', at_id2, '`, ') , '') );
SET trg_insert := CONCAT( trg_insert, stmt, '''INSERT''); \n\n');
SET stmt := CONCAT( 'INSERT IGNORE INTO `', audit_schema_name, '`.zaudit (user, table_name, pk1, ', CASE WHEN at_id2 IS NULL THEN '' ELSE 'pk2, ' END , 'action) VALUE ( IFNULL( @zaudit_user, USER() ), ',
'''', audit_table_name, ''', ', 'OLD.`', at_id1, '`, ', IFNULL( CONCAT('OLD.`', at_id2, '`, ') , '') );
SET trg_update := CONCAT( trg_update, stmt, '''UPDATE''); \n\n' );
SET trg_delete := CONCAT( trg_delete, stmt, '''DELETE''); \n\n' );
SET stmt := 'SET zaudit_last_inserted_id = LAST_INSERT_ID();\n';
SET trg_insert := CONCAT( trg_insert, stmt );
SET trg_update := CONCAT( trg_update, stmt );
SET trg_delete := CONCAT( trg_delete, stmt );
SET stmt := CONCAT( 'INSERT IGNORE INTO `', audit_schema_name, '`.zaudit_meta (audit_id, col_name, old_value, new_value) VALUES \n' );
SET trg_insert := CONCAT( trg_insert, '\n', stmt );
SET trg_update := CONCAT( trg_update, '\n', stmt );
SET trg_delete := CONCAT( trg_delete, '\n', stmt );
SET stmt := ( SELECT GROUP_CONCAT(' (zaudit_last_inserted_id, ''', COLUMN_NAME, ''', NULL, ',
CASE WHEN INSTR( '|binary|varbinary|tinyblob|blob|mediumblob|longblob|', LOWER(DATA_TYPE) ) <> 0 THEN
'''[UNSUPPORTED BINARY DATATYPE]'''
ELSE
CONCAT('NEW.`', COLUMN_NAME, '`')
END,
'),'
SEPARATOR '\n')
FROM information_schema.columns
WHERE BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND BINARY TABLE_NAME = BINARY audit_table_name );
SET stmt := CONCAT( TRIM( TRAILING ',' FROM stmt ), ';\n\nEND\n$$' );
SET trg_insert := CONCAT( trg_insert, stmt );
SET stmt := ( SELECT GROUP_CONCAT(' (zaudit_last_inserted_id, ''', COLUMN_NAME, ''', ',
CASE WHEN INSTR( '|binary|varbinary|tinyblob|blob|mediumblob|longblob|', LOWER(DATA_TYPE) ) <> 0 THEN
'''[SAME]'''
ELSE
CONCAT('OLD.`', COLUMN_NAME, '`')
END,
', ',
CASE WHEN INSTR( '|binary|varbinary|tinyblob|blob|mediumblob|longblob|', LOWER(DATA_TYPE) ) <> 0 THEN
CONCAT('CASE WHEN BINARY OLD.`', COLUMN_NAME, '` <=> BINARY NEW.`', COLUMN_NAME, '` THEN ''[SAME]'' ELSE ''[CHANGED]'' END')
ELSE
CONCAT('NEW.`', COLUMN_NAME, '`')
END,
'),'
SEPARATOR '\n')
FROM information_schema.columns
WHERE BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND BINARY TABLE_NAME = BINARY audit_table_name );
SET stmt := CONCAT( TRIM( TRAILING ',' FROM stmt ), ';\n\nEND\n$$' );
SET trg_update := CONCAT( trg_update, stmt );
SET stmt := ( SELECT GROUP_CONCAT(' (zaudit_last_inserted_id, ''', COLUMN_NAME, ''', ',
CASE WHEN INSTR( '|binary|varbinary|tinyblob|blob|mediumblob|longblob|', LOWER(DATA_TYPE) ) <> 0 THEN
'''[UNSUPPORTED BINARY DATATYPE]'''
ELSE
CONCAT('OLD.`', COLUMN_NAME, '`')
END,
', NULL ),'
SEPARATOR '\n')
FROM information_schema.columns
WHERE BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND BINARY TABLE_NAME = BINARY audit_table_name );
SET stmt := CONCAT( TRIM( TRAILING ',' FROM stmt ), ';\n\nEND\n$$' );
SET trg_delete := CONCAT( trg_delete, stmt );
-- -----------------------------------------------
-- [ Generating Helper Views For The Audit Table ]
-- -----------------------------------------------
SET stmt := CONCAT( 'DROP VIEW IF EXISTS `', audit_schema_name, '`.`zvw_audit_', audit_table_name, '_meta`\n$$\n',
'CREATE VIEW `', audit_schema_name, '`.`zvw_audit_', audit_table_name, '_meta` AS \n', header,
'SELECT za.audit_id, zm.audit_meta_id, za.user, \n',
' za.pk1, za.pk2,\n',
' za.action, zm.col_name, zm.old_value, zm.new_value, za.timestamp\n',
'FROM `', audit_schema_name, '`.zaudit za \n',
'INNER JOIN `', audit_schema_name, '`.zaudit_meta zm ON za.audit_id = zm.audit_id \n',
'WHERE za.table_name = ''', audit_table_name, '''');
SET vw_audit_meta := CONCAT( stmt, '$$' );
SET stmt := ( SELECT GROUP_CONCAT( ' MAX((CASE WHEN zm.col_name = ''', COLUMN_NAME, ''' THEN zm.old_value ELSE NULL END)) AS `', COLUMN_NAME, '_old`, \n',
' MAX((CASE WHEN zm.col_name = ''', COLUMN_NAME, ''' THEN zm.new_value ELSE NULL END)) AS `', COLUMN_NAME, '_new`, \n'
SEPARATOR '\n')
FROM information_schema.columns
WHERE BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND BINARY TABLE_NAME = BINARY audit_table_name
);
SET stmt := TRIM( TRAILING ', \n' FROM stmt );
SET stmt := ( SELECT CONCAT( 'DROP VIEW IF EXISTS `', audit_schema_name, '`.`zvw_audit_', audit_table_name, '`\n$$\n',
'CREATE VIEW `', audit_schema_name, '`.`zvw_audit_', audit_table_name, '` AS \n', header,
'SELECT za.audit_id, za.user, za.pk1, za.pk2,\n',
'za.action, za.timestamp, \n',
stmt , '\n',
' FROM `', audit_schema_name, '`.zaudit za \n',
' INNER JOIN `', audit_schema_name, '`.zaudit_meta zm ON za.audit_id = zm.audit_id \n'
'WHERE za.table_name = ''', audit_table_name, '''\n',
'GROUP BY zm.audit_id') );
SET vw_audit := CONCAT( stmt, '\n$$' );
-- SELECT trg_insert, trg_update, trg_delete, vw_audit, vw_audit_meta;
SET stmt = CONCAT(
'-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n',
'-- --------------------------------------------------------------------\n',
'-- Audit Script For `',audit_schema_name, '`.`', audit_table_name, '`\n',
'-- Date Generated: ', NOW(), '\n',
'-- Generated By: ', CURRENT_USER(), '\n',
'-- BEGIN\n',
'-- --------------------------------------------------------------------\n\n'
'DELIMITER $$',
'\n\n-- [ `',audit_schema_name, '`.`', audit_table_name, '` After Insert Trigger Code ]\n',
'-- -----------------------------------------------------------\n',
trg_insert,
'\n\n-- [ `',audit_schema_name, '`.`', audit_table_name, '` After Update Trigger Code ]\n',
'-- -----------------------------------------------------------\n',
trg_update,
'\n\n-- [ `',audit_schema_name, '`.`', audit_table_name, '` After Delete Trigger Code ]\n',
'-- -----------------------------------------------------------\n',
trg_delete,
'\n\n-- [ `',audit_schema_name, '`.`', audit_table_name, '` Audit Meta View ]\n',
'-- -----------------------------------------------------------\n',
vw_audit_meta,
'\n\n-- [ `',audit_schema_name, '`.`', audit_table_name, '` Audit View ]\n',
'-- -----------------------------------------------------------\n',
vw_audit,
'\n\n',
'-- --------------------------------------------------------------------\n',
'-- END\n',
'-- Audit Script For `',audit_schema_name, '`.`', audit_table_name, '`\n',
'-- --------------------------------------------------------------------\n\n',
'-- $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n'
);
-- SELECT stmt AS `Audit Script`, out_errors AS `ERRORS`;
SET script := stmt;
SET errors := out_errors;
END
$$
DROP PROCEDURE IF EXISTS `zsp_generate_batch_audit`
$$
CREATE PROCEDURE `zsp_generate_batch_audit` (IN audit_schema_name VARCHAR(255), IN audit_table_names VARCHAR(255), OUT out_script LONGTEXT, OUT out_error_msgs LONGTEXT)
main_block: BEGIN
DECLARE s, e, scripts, error_msgs LONGTEXT;
DECLARE audit_table_name VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
DECLARE cursor_table_list CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE BINARY TABLE_TYPE = BINARY 'BASE TABLE'
AND BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND LOCATE( BINARY CONCAT(TABLE_NAME, ','), BINARY CONCAT(audit_table_names, ',') ) > 0;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET done = TRUE;
SET scripts := '';
SET error_msgs := '';
OPEN cursor_table_list;
cur_loop: LOOP
FETCH cursor_table_list INTO audit_table_name;
IF done THEN
LEAVE cur_loop;
END IF;
CALL zsp_generate_audit(audit_schema_name, audit_table_name, s, e);
SET scripts := CONCAT( scripts, '\n\n', IFNULL(s, '') );
SET error_msgs := CONCAT( error_msgs, '\n\n', IFNULL(e, '') );
END LOOP;
CLOSE cursor_table_list;
SET out_script := scripts;
SET out_error_msgs := error_msgs;
END
$$
DROP PROCEDURE IF EXISTS `zsp_generate_remove_audit`
$$
CREATE PROCEDURE `zsp_generate_remove_audit` (IN audit_schema_name VARCHAR(255), IN audit_table_name VARCHAR(255), OUT script LONGTEXT)
main_block: BEGIN
SET script := CONCAT(
'-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n',
'-- --------------------------------------------------------------------\n',
'-- Audit Removal Script For `',audit_schema_name, '`.`', audit_table_name, '` \n',
'-- Date Generated: ', NOW(), '\n',
'-- Generated By: ', CURRENT_USER(), '\n',
'-- BEGIN\n',
'-- --------------------------------------------------------------------\n\n',
'DELIMITER $$\n\n',
'DROP TRIGGER IF EXISTS `', audit_schema_name, '`.`z', audit_table_name, '_AINS`\n$$\n',
'DROP TRIGGER IF EXISTS `', audit_schema_name, '`.`z', audit_table_name, '_AUPD`\n$$\n',
'DROP TRIGGER IF EXISTS `', audit_schema_name, '`.`z', audit_table_name, '_ADEL`\n$$\n',
'DROP VIEW IF EXISTS `', audit_schema_name, '`.`zvw_audit_', audit_table_name, '_meta`\n$$\n',
'DROP VIEW IF EXISTS `', audit_schema_name, '`.`zvw_audit_', audit_table_name, '`\n$$\n',
'\n\n',
'-- --------------------------------------------------------------------\n',
'-- END\n',
'-- Audit Removal Script For `',audit_schema_name, '`.`', audit_table_name, '`\n',
'-- --------------------------------------------------------------------\n\n',
'-- $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n'
);
END
$$
DROP PROCEDURE IF EXISTS `zsp_generate_batch_remove_audit`
$$
CREATE PROCEDURE `zsp_generate_batch_remove_audit` (IN audit_schema_name VARCHAR(255), IN audit_table_names VARCHAR(255), OUT out_script LONGTEXT)
main_block: BEGIN
DECLARE s, scripts LONGTEXT;
DECLARE audit_table_name VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
DECLARE cursor_table_list CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE BINARY TABLE_TYPE = BINARY 'BASE TABLE'
AND BINARY TABLE_SCHEMA = BINARY audit_schema_name
AND LOCATE( BINARY CONCAT(TABLE_NAME, ','), BINARY CONCAT(audit_table_names, ',') ) > 0;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET done = TRUE;
SET scripts := '';
OPEN cursor_table_list;
cur_loop: LOOP
FETCH cursor_table_list INTO audit_table_name;
IF done THEN
LEAVE cur_loop;
END IF;
CALL zsp_generate_remove_audit(audit_schema_name, audit_table_name, s);
SET scripts := CONCAT( scripts, '\n\n', IFNULL(s, '') );
END LOOP;
CLOSE cursor_table_list;
SET out_script := scripts;
END
$$