How to version control data stored in mysql

廉价感情. 提交于 2020-01-11 05:06:25

问题


I'm trying to use a simple mysql database but tweak it so that every field is backed up up to an indefinite number of versions. The best way I can illustrate this is by replacing each and every field of every table with a stack of all the values this field has ever had (each of these values should be timestamped). I guess it's kind of like having customized version control for all my data..

Any ideas on how to do this?


回答1:


The usual method for "tracking any changes" to a table is to add insert/update/delete trigger procedures on the table and have those records saved in a history table.

For example, if your main data table is "ItemInfo" then you would also have an ItemInfo_History table that got a copy of the new record every time anything changed (via the triggers).

This keeps the performance of your primary table consistent, yet gives you access to the history of any changes if you need it.

Here are some examples, they are for SQL Server but they demonstrate the logic:

My Repository table My Repository History table My Repository Insert trigger procedure My Repository Update trigger procedure




回答2:


Hmm, what you're talking about sounds similar to Slowly Changing Dimension.

Be aware that version control on arbitrary database structures is officially a rather Hard Problem. :-)




回答3:


A simple solution would be to add a version/revision field to the tables, and whenever a record is updated, instead of updating it in place, insert a copy with the changes applied and the version number incremented. Then when selecting, always choose the record with the latest version. That's roughly how most such schemes are implemented (e.g. Wikimedia does it pretty much this exact way).




回答4:


Maybe a tool can help you to do that for you. Have a look at nextep designer :

https://github.com/christophefondacci/nextep-designer

With this IDE you will be able to take snapshots of your database structure and data and put it under version control. After this you can compute the differences between any 2 versions and generate the appropriate SQL that can insert / update / delete your data.

Maybe this is an alternative way to achieve what you wanted.



来源:https://stackoverflow.com/questions/2751808/how-to-version-control-data-stored-in-mysql

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