public
Description: Some basic patterns for accomplishing semi-tricky things using mysql
Homepage:
Clone URL: git://github.com/lsimons/mysqltricks.git
mysqltricks / history_schema.sql
100644 348 lines (315 sloc) 10.91 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
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
-- this is an example of how one might implement history tables for MySQL.
 
 
-- Copyright (c) 2009 Leo Simons. All Rights Reserved.
--
-- Licensed under the Common Development and Distribution License, Version 1.0
-- (the "License"); you may not use this file except in compliance with the
-- License. You may obtain a copy of the License at
-- http://www.jicarilla.nl/licensing/
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-- License for the specific language governing permissions and limitations under
-- the License.
 
 
-- Some comments on the implementation:
-- * It puts the history data in delegate tables so that most applications
-- can be fully unaware of the history logging.
-- * It uses a specific history table for each main table so that it is easy
-- to build reports / do joins with/across history tables
-- * It requires that all cross-table links are done using dedicated link
-- tables that themselves are versioned
-- * It keeps track of creation dates, modification dates, and users,
-- allowing the application to override the username
-- * beware - there's a bug in how the username is set for the update triggers!
-- * It results in a _lot_ of typing if you write all the tables and
-- triggers by hand; a typical real-life solution would script more of that
--
-- WARNING: this kind of schema is _not_ good enough by itself to satisfy an
-- _audit_, see
-- http://www.scribd.com/doc/2569459/Securing-MySQL-for-a-Security-Audit
-- for pointers on how to do auditable database stuff
--
-- WARNING: can cause problems with replication:
-- http://www.mysqlperformanceblog.com/2008/09/29/why-audit-logging-with-triggers-in-mysql-is-bad-for-replication/
-- use row-based replication to avoid those problems:
-- http://dev.mysql.com/doc/refman/5.1/en/replication-formats.html
--
-- Ideas for an alternative approach (using one big history table):
-- http://www.go4expert.com/forums/showthread.php?t=7252
--
-- Ideas for a compact way to generate trigger code:
-- http://thenoyes.com/littlenoise/?p=43
 
 
-- 'main' schema
 
CREATE TABLE mythings (
    id int(11) unsigned NOT NULL auto_increment PRIMARY KEY,
    somevalue varchar(255) DEFAULT NULL,
    othervalue varchar(255) DEFAULT NULL,
    
    created timestamp NULL DEFAULT NULL,
    modified timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    modifier varchar(255) DEFAULT NULL
) Engine=InnoDB;
 
CREATE TABLE others (
    id int(11) unsigned NOT NULL auto_increment PRIMARY KEY,
    
    created timestamp NULL DEFAULT NULL,
    modified timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    modifier varchar(255) DEFAULT NULL
) Engine=InnoDB;
 
CREATE TABLE mythings2others (
    mythings_id int(11) unsigned NOT NULL,
    others_id int(11) unsigned NOT NULL PRIMARY KEY,
    
    created timestamp NULL DEFAULT NULL,
    modified timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    modifier varchar(255) DEFAULT NULL,
    
    CONSTRAINT FOREIGN KEY (mythings_id)
      REFERENCES mythings (id)
      ON DELETE CASCADE
      ON UPDATE CASCADE,
    CONSTRAINT FOREIGN KEY (others_id)
      REFERENCES others (id)
      ON DELETE CASCADE
      ON UPDATE CASCADE
) Engine=InnoDB;
 
DELIMITER |
CREATE TRIGGER mythings_before_insert BEFORE INSERT ON mythings
  FOR EACH ROW BEGIN
    SET NEW.created = IFNULL(NEW.created, NOW());
  END
|
CREATE TRIGGER others_before_insert BEFORE INSERT ON others
  FOR EACH ROW BEGIN
    SET NEW.created = IFNULL(NEW.created, NOW());
  END
|
CREATE TRIGGER mythings2others_before_insert BEFORE INSERT ON mythings2others
  FOR EACH ROW BEGIN
    SET NEW.created = IFNULL(NEW.created, NOW());
  END
|
DELIMITER ;
 
 
-- history tracking for mythings
 
CREATE TABLE mythings_history (
    version int(11) unsigned NOT NULL auto_increment PRIMARY KEY,
    mythings_id int(11) unsigned NOT NULL,
    
    action varchar(16) NOT NULL,
    old_somevalue varchar(255) DEFAULT NULL,
    new_somevalue varchar(255) DEFAULT NULL,
    old_othervalue varchar(255) DEFAULT NULL,
    new_othervalue varchar(255) DEFAULT NULL,
    modified timestamp NULL DEFAULT NULL,
    modifier varchar(255) DEFAULT NULL
) Engine=InnoDB;
 
DELIMITER |
CREATE TRIGGER mythings_after_insert AFTER INSERT ON mythings
  FOR EACH ROW BEGIN
    INSERT INTO mythings_history
            (mythings_id,
            action,
            old_somevalue,
            new_somevalue,
            old_othervalue,
            new_othervalue,
            modified,
            modifier)
        VALUES
            (NEW.id,
            'INSERT',
            NULL,
            NEW.somevalue,
            NULL,
            NEW.othervalue,
            NEW.modified,
            IFNULL(NEW.modifier,USER()));
  END
|
CREATE TRIGGER mythings_after_update AFTER UPDATE ON mythings
  FOR EACH ROW BEGIN
    INSERT INTO mythings_history
            (mythings_id,
            action,
            old_somevalue,
            new_somevalue,
            old_othervalue,
            new_othervalue,
            modified,
            modifier)
        VALUES
            (NEW.id,
            'UPDATE',
            OLD.somevalue,
            NEW.somevalue,
            OLD.othervalue,
            NEW.othervalue,
            NEW.modified,
            IFNULL(NEW.modifier,USER()));
  END
|
CREATE TRIGGER mythings_after_delete AFTER DELETE ON mythings
  FOR EACH ROW BEGIN
    INSERT INTO mythings_history
        (mythings_id,
        action,
        old_somevalue,
        new_somevalue,
        old_othervalue,
        new_othervalue,
        modified,
        modifier)
    VALUES
        (OLD.id,
        'DELETE',
        OLD.somevalue,
        NULL,
        OLD.othervalue,
        NULL,
        NOW(),
        USER());
  END
|
DELIMITER ;
 
 
-- history tracking for others
 
CREATE TABLE others_history (
    version int(11) unsigned NOT NULL auto_increment PRIMARY KEY,
    others_id int(11) unsigned NOT NULL,
    
    action varchar(16) NOT NULL,
    modified timestamp NULL DEFAULT NULL,
    modifier varchar(255) DEFAULT NULL
) Engine=InnoDB;
 
DELIMITER |
CREATE TRIGGER others_after_insert AFTER INSERT ON others
  FOR EACH ROW BEGIN
    INSERT INTO others_history
            (others_id,
            action,
            modified,
            modifier)
        VALUES
            (NEW.id,
            'INSERT',
            NEW.modified,
            IFNULL(NEW.modifier,USER()));
  END
|
CREATE TRIGGER others_after_update AFTER UPDATE ON others
  FOR EACH ROW BEGIN
    INSERT INTO others_history
            (others_id,
            action,
            modified,
            modifier)
        VALUES
            (NEW.id,
            'UPDATE',
            NEW.modified,
            IFNULL(NEW.modifier,USER()));
  END
|
CREATE TRIGGER others_after_delete AFTER DELETE ON others
  FOR EACH ROW BEGIN
    INSERT INTO others_history
        (others_id,
        action,
        modified,
        modifier)
    VALUES
        (OLD.id,
        'DELETE',
        NOW(),
        USER());
  END
|
DELIMITER ;
 
 
-- history tracking for mythings2others
 
CREATE TABLE mythings2others_history (
    version int(11) unsigned NOT NULL auto_increment PRIMARY KEY,
    old_mythings_id int(11) unsigned DEFAULT NULL,
    new_mythings_id int(11) unsigned DEFAULT NULL,
    mythings_history_version int(11) unsigned DEFAULT NULL,
    old_others_id int(11) unsigned DEFAULT NULL,
    new_others_id int(11) unsigned DEFAULT NULL,
    others_history_version int(11) unsigned DEFAULT NULL,
 
    action varchar(16) NOT NULL,
    modified timestamp NULL DEFAULT NULL,
    modifier varchar(255) DEFAULT NULL
) Engine=InnoDB;
 
DELIMITER |
CREATE TRIGGER mythings2others_after_insert AFTER INSERT ON mythings2others
  FOR EACH ROW BEGIN
    INSERT INTO mythings2others_history
            (old_mythings_id,
            new_mythings_id,
            mythings_history_version,
            old_others_id,
            new_others_id,
            others_history_version,
            action,
            modified,
            modifier)
        VALUES
            (NULL,
            NEW.mythings_id,
            (SELECT version FROM mythings_history WHERE mythings_id = NEW.mythings_id ORDER BY version DESC LIMIT 1),
            NULL,
            NEW.others_id,
            (SELECT version FROM others_history WHERE others_id = NEW.others_id ORDER BY version DESC LIMIT 1),
            'INSERT',
            NEW.modified,
            IFNULL(NEW.modifier,USER()));
  END
|
CREATE TRIGGER mythings2others_after_update AFTER UPDATE ON mythings2others
  FOR EACH ROW BEGIN
    INSERT INTO mythings2others_history
            (old_mythings_id,
            new_mythings_id,
            mythings_history_version,
            old_others_id,
            new_others_id,
            others_history_version,
            action,
            modified,
            modifier)
        VALUES
            (OLD.mythings_id,
            NEW.mythings_id,
            (SELECT version FROM mythings_history WHERE mythings_id = NEW.mythings_id ORDER BY version DESC LIMIT 1),
            OLD.mythings_id,
            NEW.others_id,
            (SELECT version FROM others_history WHERE others_id = NEW.others_id ORDER BY version DESC LIMIT 1),
            'UPDATE',
            NEW.modified,
            IFNULL(NEW.modifier,USER()));
  END
|
CREATE TRIGGER mythings2others_after_delete AFTER DELETE ON mythings2others
  FOR EACH ROW BEGIN
    INSERT INTO mythings2others_history
            (old_mythings_id,
            new_mythings_id,
            mythings_history_version,
            old_others_id,
            new_others_id,
            others_history_version,
            action,
            modified,
            modifier)
        VALUES
            (OLD.mythings_id,
            NULL,
            (SELECT version FROM mythings_history WHERE mythings_id = OLD.mythings_id ORDER BY version DESC LIMIT 1),
            OLD.mythings_id,
            NULL,
            (SELECT version FROM others_history WHERE others_id = OLD.others_id ORDER BY version DESC LIMIT 1),
            'DELETE',
            NOW(),
            USER());
  END
|
DELIMITER ;
 
 
-- some sample data
 
INSERT INTO mythings (somevalue, modifier) VALUES ('blah', 'mail@leosimons.com');
INSERT INTO mythings (somevalue, modifier) VALUES ('second thing', 'mail@leosimons.com');
UPDATE mythings SET othervalue = 'things are the same', modifier = 'mail@leosimons.com';
INSERT INTO others (modifier) VALUES ('mail@leosimons.com');
INSERT INTO others (modifier) VALUES ('mail@leosimons.com');
INSERT INTO mythings2others (mythings_id, others_id, modifier) VALUES (1, 1, 'mail@leosimons.com');
UPDATE mythings2others SET mythings_id = 2, modifier = 'mail@leosimons.com' WHERE mythings_id = 1 LIMIT 1;