I'm looking for some ideas on how other people are managing the following situation - I've got some ideas but they seem a little messy and I can't help thinking I'm missing something.
Here's the situation:
- You're using NHibernate with Fluent NHibernate for mappings.
- You have an application in production with a database with live data
- You're adding a new feature in development and it requires a new database column.
The new column cannot be blank. For example, I recently had to a DateCreated column to a table and the app now uses that date. As the only time the data will be missing is now, it seems unnecessary to add code to check for errors.
In my application I have an updater which can execute a SchemaUpdate to add the new database column - however, the application will starting crashing as it is expecting a value in the new column.
I need to get some sensible default data into that column. In this case I manually ran an Update to set the date to the current date (good enough for the situation). In this particular case I believe that you cannot set the column default to getdate() using fluent mappings.
My Idea
- Keep a schema version number in a config file
- In the updater that runs SchemaUpdate start adding upgrade methods for each version. These methods would run updates to add default data (or other required actions).
- After the schema update has been run, call all methods required for a version greater than the current version (i.e. those that haven't previously been run). So if the app is now version 4 and version 2 is installed, methods 3 and 4 would be run.
- Update the saved version number
How are other people handling this situation?