Reputation: 2044
When using the ORM NHibernate, what are the standard or best open source methods for propogating changes to the schema to application users who have an existing version of the software?
If I need to add or remove properties from an existing class, how can I properly handle those changes for users who are using a previous version of the software?
I am looking for something that will run on Mono, if possible.
I would prefer not to have to analyze the resulting schema from ORM changes and hand code a version update every time I create a new build of the application.
(I am making the assumption that I will be using NHibernate to create the schema for the database, but if there are different methods which eliminate this issue then I am open to an answer).
Upvotes: 3
Views: 135
Reputation: 12668
Like you use version control for source code you should use version control for database schema, especially when you are intended to provide update functionality for your application. There are some tools to help you, but I prefer do to things manually using XML file with SQL commands to update schema revision or downgrade, something like this:
<Revision Id="3">
<Up>
<SqlCommand>CREATE TABLE [Category] ( [Id] BigInt Primary Key, [Name] nvarchar(255) )</SqlCommand>
<SqlCommand>ALTER TABLE [Group] ADD CanRead bigint NULL</SqlCommand>
<SqlCommand>UPDATE [SysValue] SET [Value] = '3' WHERE [Name] = 'Revision'</SqlCommand>
</Up>
<Down>
<SqlCommand>DELETE TABLE [Category]</SqlCommand>
<SqlCommand>ALTER TABLE [Group] DROP COLUMN [CanRead]</SqlCommand>
<SqlCommand>UPDATE [SysValue] SET [Value] = '2' WHERE [Name] = 'Revision'</SqlCommand>
</Down>
</Revision>
The good thing about it that if you write SQL commands right you can change schema without loosing data, you can write complex queries to copy data to temp table then dramatically alter this table and copy data back, convert data etc.
Upvotes: 1