24
Johan Sölve @macsolve http://re.solve.se

Database versioning

Embed Size (px)

DESCRIPTION

Describes how to givee MySQL databases the notion of time. Lets you roll back database tables to any point in time, consistently across tables and without affecting other users.

Citation preview

Page 1: Database versioning

Johan Sölve@macsolve

http://re.solve.se

Page 2: Database versioning

Relational databases

• Two dimensional

• Rows and columns

• Only know about NOW

• Has no concept of ”history”

Page 3: Database versioning

What if we can add a

third dimension?

Page 4: Database versioning

What if we can add the notion of

TIME

Page 5: Database versioning

Data Versioning

Page 6: Database versioning

Data Versioning

• ”Time Machine” for data

• Snapshot of any point in time

• Immediately and continously available

• No rollback or restore of database

• Demo:

http://johan.solve.se/dataversioning/

Page 7: Database versioning

Data Versioning

• Consistent across tables

• Solution for MySQL

• Uses VIEW to access data

• Data access it transparent to the application

• Unique for each database session/visitor

Page 8: Database versioning

Views

• Views are like search macros

• They are created as a saved SELECT statement

• Performs good if indexed properly

Page 9: Database versioning

Data Versioning

• Each table is replaced by a ”data” table that maintains every version of each row/record

• Common ”version” table

• Points at current row in data table

• Keeps meta data about changes

• Each table is completed by a VIEW to

Page 10: Database versioning

Never Change

• Most important:

• Never update or delete a row in the data table

• All changes add a new row in the data table

Page 11: Database versioning

Time

Row 1Row 2Row 3Row 4Row 5

Table

Page 12: Database versioning

Time

Row 1Row 2Row 3Row 4Row 5

Table

Table 2

Row 1Row 2Row 3Row 4

Page 13: Database versioning

Database operations– CRUD

• SELECT from a view - just as usual

• INSERT creates a row in data table and verison table

• UPDATE updates a timestamp in the version table and creates a row in the data table and version table

• DELETE updates a timestamp in the version table

Page 14: Database versioning

Database operations– CRUD

• SELECT – no change

• INSERT, UPDATE, DELETE handles specially

• Triggers and Stored procedures

• Or in the application’s database abstraction layer

Page 15: Database versioning

INSERT

INSERT INTO version (guid, changedby_start) VALUES ("gweyufghkj", "JS");

INSERT INTO people_data (id_version, firstname, lastname) VALUES(LAST_INSERT_ID(), "Nisse", "Hult");

Page 16: Database versioning

UPDATE

UPDATE version SET changedby_end="JS", dt_end=NOW() WHERE guid="gweyufghkj" AND dt_end="3000-01-01 00:00:00";

INSERT INTO version (guid, changedby_start) VALUES ("gweyufghkj", "JS");

Page 17: Database versioning

DELETE

UPDATE version SET changedby_end="JS", dt_end=NOW() WHERE guid="gweyufghkj" AND dt_end="3000-01-01 00:00:00";

Page 18: Database versioning

SELECT

SELECT * FROM people;

Page 19: Database versioning

View

CREATE VIEW people AS

SELECT people_data.*, version.guid

FROM people_data

JOIN version ON

people_data.id_version=version.id

WHERE dt_start <= history()

AND dt_end > history();

Page 20: Database versioning

Go Back in Time

SET @history="2010-02-22 10:00:00";

SELECT * FROM people;

Only affects this database session

Page 21: Database versioning

Uses

• Content Management

• Definitions for dynamic forms

• Data entry must match form definition at any point in time

Page 22: Database versioning

Considerations• Carefully evaluate for each table if it’s

relevant to include it in data versioning

Example:

• Permission settings

• What if the table structure changes?

• Performance

• Consider using emulated

Page 23: Database versioning

Implementation

• Modified knop_database

• Methods for:

-> addrecord

-> saverecord

-> deleterecord

Page 24: Database versioning

Read more

• Description:

http://re.solve.se/database-versioning

• Demo:

http://johan.solve.se/dataversioning/

Johan Sölve