开发者

Keeping a database Schema upto date

开发者 https://www.devze.com 2023-03-25 03:40 出处:网络
I\'m writing an application that is using a d开发者_如何学编程atabase (currently MySQL 4) to store data.

I'm writing an application that is using a d开发者_如何学编程atabase (currently MySQL 4) to store data.

It is likely that I will make changes to this in the form of updates later to add additional data. Updating the application is simple, it essentially comes down to overwriting the program files with the new ones. However how do I go about updating the database schema?

The database is remote and so my application might exist in several places, so simply dumping the ALTER and CREATE statements in an installer would result in the changes being made multiple times, and I have been asked explicitly for an automatic solution that allows for the application copies to be updated over a transition period, and for schema updates to be automatic.

I considered examining the schema at start-up to look for missing tables and columns, and adding them as needed, however this does not seem like a clean solution. I also considered putting some kind of “schema version” number on the database, but can’t see any way to do this short of a single row table with an int “Version” column which doesn’t seem a good way either.


I can highly recommend Liquibase. It really does work - I've used it and was very impressed.

Essentially, it keeps its own log of statements run on a database and runs them only if not already run/needed. It is XML driven and allows you to use optional pre- and post-execution statements and conditions. You check your XML files into your source control and invoke it from your build tool. It's even suitable for driving production releases.

It's magic.


Rather than rolling your own system for versioning your database it's probably worth looking into an existing framework that will manage it for you.

I use liquibase and have integrated into my build using the maven plugin. Worth checking out!


Just as you proposed, add a table where you store the current version of the database schema. Then you only have to apply the changes between your last schema update and the new release, and set the new version number accordingly. I've done this to update our production database about 300 times, it just works.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号