Keeping a version number in the database, and applying update scripts on startup, is an important part of this strategy.
Here's how startup works:
- checks DB_VERSION record in database,
- finds updates > current version; maybe by code.
- runs each applicable "update", script or programmatic actions..
- DB_VERSION is updated after each, so a failure partway thru can be re-run.
Example:
- find DB_VERSION currently = 789;
- sophisticated code, or a big long IF chain, finds updates 790 and up.
- update #790, upgrade Customer & Account tables;
- update #791, upgrade Email table;
- update #792, restructure Order table;
- database version now = 792.
There are a few caveats. This works reasonably well; people claim it should be 100% reliable, but it's not.
Issues of incomplete scripts, variations in field lengths or differences in server versions can occasionally cause scripts/SQL to pass on some databases, but fail on others.
Finding the scripts to run, can be as simple as a big single method with many IF statements. Or you could load the scripts via discovery or metadata, more elegantly. Sometimes it's useful to be able to include programmatic code, not just SQL.
public void runDatabaseUpgrades() {
if (version < 790) {
// upgrade Customer and Account tbls
version = 790;
}
if (version < 791) {
// upgrade Email tbl
version = 791;
}
}
answered 2012-10-06T10:20:59.187