Database migration pattern for Java?

Posted by Eno on Stack Overflow See other posts from Stack Overflow or by Eno
Published on 2010-03-21T21:24:28Z Indexed on 2010/03/21 21:31 UTC
Read the original article Hit count: 405

Filed under:
|
|
|
|

Im working on some database migration code in Java. Im also using a factory pattern so I can use different kinds of databases. And each kind of database im using implements a common interface.

What I would like to do is have a migration check that is internal to the class and runs some database schema update code automatically. The actual update is pretty straight forward (I check schema version in a table and compare against a constant in my app to decide whether to migrate or not and between which versions of schema).

To make this automatic I was thinking the test should live inside (or be called from) the constructor. OK, fair enough, that's simple enough. My problem is that I dont want the test to run every single time I instantiate a database object (it runs a query so having it run on every construction is not efficient). So maybe this should be a class static method? I guess my question is, what is a good design pattern for this type of problem? There ought to be a clean way to ensure the migration test runs only once OR is super-efficient.

© Stack Overflow or respective owner

Related posts about java

Related posts about database