In the article ‘Versioning MySQL data‘, I showed the basics of
implementing a revisioning system using trigger. As Jens Schauder
already pointed out, often the data of a record is spread across
multiple tables, like an invoice with multiple invoice lines.
Having each invoice line versioned individually isn’t really
useful. Instead we want a new revision of the whole invoice on
each change.
The perfect solution
Ideally a change of one or more parts of the invoice would be
changed, a new revision would be created. There are several
issues in actually creating this those. Detecting the change of
multiple parts of the invoice at once, generating a single
revision, would mean we need to know if the actions are done
within the same transaction. Unfortunately there is a
connection_id(), but no transaction_id() function in …
As a developer you’re probably using a versioning control system,
like subversion or git, to safeguard your data. Advantages of
using a VCS are that you can walk to the individual changes for a
document, see who made each change and revert back to specific
revision if needed. These are features which would also be nice
for data stored in a database. With the use of triggers we can implement versioning for data
stored in a MySQL db.
The revisioning table
We will not store the different versions of the records in the
original table. We want this solution to be in the database layer
instead of putting all the logic in the application layer.
Instead we’ll create a new table, which stores all the different
versions and lives next to the original table, which only
contains the current version of each record. This revisioning
table …