Document Versioning and DELETE FROM Recovery in MariaDB

Let's build a place for authors to ink their great novels. Writing takes time and we'd want to ensure an authors could view prior versions and drafts. We can manually support this with audit tables or other ledgering, but MariaDB system versioned tables makes this automatic.

CREATE TABLE notes (
    id int AUTO_INCREMENT PRIMARY KEY,
    body text
) ENGINE=InnoDB WITH SYSTEM VERSIONING;
INSERT INTO notes SET body="Hello World!";
SELECT * FROM notes;
id, body
1 , Hello World!

Now we keep writing:

UPDATE notes SET body = "Hello World! My name is Anil" WHERE id=1;
SELECT * FROM notes;
id, body
1 , Hello World! My name is Anil

Now say we wanted to go back to the version from a few minutes back. Let's look at the history of updates:

SELECT *, ROW_START, ROW_END FROM notes FOR SYSTEM_TIME ALL;
id, body,                             ROW_START,                              ROW_END
1,  Hello World!,                  2022-02-20 08:40:19.762372, 2022-02-20 08:41:18.343344
1,  Hello World! My name is Anil,  2022-02-20 08:41:18.343344, 2038-01-19 03:14:07.999999

Now lets revert to the first version of the document:

UPDATE notes
INNER JOIN (
    SELECT * FROM notes
    FOR SYSTEM_TIME AS OF TIMESTAMP '2022-02-20 08:41:00'
    WHERE id=1
) notes_old ON notes.id = notes_old.id

SET notes.body = notes_old.body
WHERE notes.id = 1;

And Voila we're back to our first version!

SELECT * FROM notes;
id, body
1 , Hello World!

Ok now let's put this in production and have some great authors tell their tales. First we reset the database:

ALTER TABLE notes DROP SYSTEM VERSIONING;
TRUNCATE TABLE notes;
ALTER TABLE notes ADD SYSTEM VERSIONING;

SELECT * FROM notes; -- is empty
SELECT * FROM notes FOR SYSTEM_TIME ALL; -- is empty;

And we begin:

INSERT INTO notes (body) VALUE ("This book aims to give in brief space");
INSERT INTO notes (body) VALUE ("Alice was beginning to get very tired");
INSERT INTO notes (body) VALUE ("It was the best of times, it was the worst of times");
INSERT INTO notes (body) VALUE ("Call me Ishmael.");

And we rest. Great work takes time. But we must get back to work:

UPDATE notes SET body = "This book aims to give in brief space the principal requirements of plain English style. It aims to lighten the task of instructor and student by concentrating attention (in Chapters II and III) on a few essentials, the rules of usage and principles of composition most commonly violated."
WHERE id=1;

UPDATE notes SET body = "Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice without pictures or conversations?"
WHERE id=2;

UPDATE notes SET body = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way--in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only."
WHERE id=3;

UPDATE notes SET body = "Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world."
WHERE id=4;

Then in a stroke of great misfortune we accidentally run this terrible command:

DELETE FROM notes;

In a panic we check to see the damage:

SELECT * FROM notes;

And there is only emptiness. The deed has been done... We may at this point begin to look to restore from backups but in a ray of light we recall system tables to our rescue:

SELECT *, ROW_START, ROW_END FROM notes FOR SYSTEM_TIME ALL;
id, body, ROW_START, ROW_END
1,"This book aims to give in brief space",2022-02-20 08:48:53.013632,2022-02-20 08:49:45.641598
1,"This book aims to give in brief space the principal requirements of plain English style. It aims to lighten the task of instructor and student by concentrating attention (in Chapters II and III) on a few essentials, the rules of usage and principles of composition most commonly violated.",2022-02-20 08:49:45.641598,2022-02-20 08:52:47.257788
2,Alice was beginning to get very tired,2022-02-20 08:48:54.128858,2022-02-20 08:49:47.255860
2,"Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice without pictures or conversations?",2022-02-20 08:49:47.255860,2022-02-20 08:52:47.257788
3,"It was the best of times, it was the worst of times",2022-02-20 08:48:55.005831,2022-02-20 08:49:48.411337
3,"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way--in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.",2022-02-20 08:49:48.411337,2022-02-20 08:52:47.257788
4,Call me Ishmael.,2022-02-20 08:48:55.841094,2022-02-20 08:49:49.647887
4,"Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.",2022-02-20 08:49:49.647887,2022-02-20 08:52:47.257788

We can use the timestamps to restore these works:

INSERT INTO notes (SELECT * FROM notes FOR SYSTEM_TIME AS OF TIMESTAMP '2022-02-20 08:50:00');
SELECT * FROM notes;
1,"This book aims to give in brief space the principal requirements of plain English style. It aims to lighten the task of instructor and student by concentrating attention (in Chapters II and III) on a few essentials, the rules of usage and principles of composition most commonly violated."
2,"Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice without pictures or conversations?"
3,"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way--in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only."
4,"Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world."

And now for rest!


Disclaimer: Note that all text is sourced from gutenberg.org with the public domain license: