Today was in fact the first time I ever got into a situation where the setting dbCreate = 'update' couldn't manage to update my data base scheme after modifying some of my domain classes. This was unfortunate of course, but since I've been reading that this is bad practice anyway I decided to have a closer look at the new database migration plugin that came out at roughly the same time as grails 2.0 did. This plugin utilizes Liquibase, which has been around for a while from what I could see and seemingly does a very good job. It turns out that the database migration plugin gives you a really smooth integration of Liquibase and grails.
Coming to my actual problem:
After adding the spring-security plugin I wanted to have some light history with two of my domain classes, so I introduced the fields
- createdBy,
- lastUpdatedBy,
- created,
- lastUpdated.
The latter two are automagically populated by GORM and the first two I could populate in the controller classes using the springSecurityService. This obviously leaves existing database rows without this information and since I assigned a "not nullable" constraint, hibernate sure enough complaint when trying to change the scheme.
So I did some reading on the excellent tutorials I found to get up to speed:
http://blog.springsource.org/2011/08/17/countdown-to-grails-2-0-database-migrations/
http://fbflex.wordpress.com/2011/01/19/working-with-the-grails-database-migration-plugin/
After understanding the key concepts I went ahead and followed the instructions at the end of the second blog post (updating an existing database). I created a new environment "migrate" which I configured to use a copy of my database. My domain classes where already changed, so I just needed to compare the current database scheme with my domain classes using
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grails -Dgrails.env=migrate dbm-gorm-diff --add changelog-0.2.groovy |
You can try and run it using the dbm-update command:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grails -Dgrails.env=migrate dbm-update |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//select migration file | |
grails.plugin.databasemigration.changelogFileName = 'changelog-0.3.groovy' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
changeSet(author: "mlist (generated)", id: "1340373645634-47") { | |
addNotNullConstraint(columnDataType: "bigint", columnName: "createdBy", schemaName: "dbo", tableName: "slide_layout", defaultNullValue: 1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
changeSet(author: "mlist (generated)", id: "1340373645634-47") { | |
addNotNullConstraint(columnDataType: "datetime2", columnName: "date_created", schemaName: "dbo", tableName: "slide_layout", | |
defaultNullValue: "2012-06-22 00:00:00.0000000") | |
} |
A few important tips for working with this plugin:
- Use grails -Dgrails.env=yourEnv to run a custom environment besides dev, test or prod.
- Use grails dbm-update-sql to see what SQL code is created.
- Be aware: The dbm-update command often fails more or less silently. Therefore:
- Look into your database! There is a new table called DATABASECHANGELOG.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select * from databasechangelog order by orderexecuted |
| Finished dbm-update
You finally did it and you can leave for the week-end.
No comments:
Post a Comment