Basic version control for MySQL table

别等时光非礼了梦想. 提交于 2019-12-04 10:57:28

Ideally you would want everything in the same table with something in your query to get the correct version, however you should be careful how you do this as an inefficient query will put extra load on your server. If normally you would select a single item like this:

SELECT * FROM your_table WHERE id = 42

This would then become:

SELECT * FROM your_table
WHERE id = 42 
AND date < '2010-10-12 15:23:24'
ORDER BY date DESC
LIMIT 1

Index (id, e_timestamp) to allow this to perform efficiently.

Selecting multiple rows in a single query is more tricky and requires a groupwise-maximum approach but it can be done.

You can use a technique called "auditing". You would set up audit tables. Then you would either write it into your code or setup triggers on the DB side so that every time a change is made, an entry is added into the appropriate audit table. Then you can go back through the audit table and see things like: "Oh, yesterday Sue went in and fixed a typo" "Uh oh, steve wiped out an entire paragraph by accident earlier today while trying to rewrite this section"

Your primary table that stores the data doesn't keep all that data, so it can stay slim. If you ever need to look at that data and say roll stuff back, you can go look in your audit table and do that. You can setup the audit table however you want, so each audit row can have the entire content BEFORE edit, and not just what was edited. That should make "rolling back" fairly easy.

Add a version column and a delete column (bool) and create some functions that compare the versions of rows with the same id. You'll definitely want to be able to easily find the current version and the previous version. To get rid of the data you'll want to write another function that sorts all of the versions of id, figures out which are old enough to be deleted, and marks them for deletion by another function. You'll probably want to have an option to make certain pages immune to deletion or postpone it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!