Reputation: 760
I am trying to familiarize myself with the Grails Database Migration Plugin using this site as a reference. I have encountered a problem when trying to add a property with a not null constraint. I modeled my script the same way as it is shown on the site (add the column, set defaults for existing records, then add a not nullable constraint to the column):
databaseChangeLog = {
changeSet(author: "Ryan (generated)", id: "1340893788251-1") {
addColumn(tableName: "game") {
column(name: "genre", type: "varchar(255)")
}
grailsChange{
change{
sql.executeUpdate("UPDATE game SET genre = 'Other'")
}
}
addNotNullConstraint(tableName: "game", columnName: "genre")
}
}
When I attempt to update my database it silently fails. So, I split the three changes into three different groovy scripts to track down the problem.
First file (add column) worked fine:
databaseChangeLog = {
changeSet(author: "Ryan (generated)", id: "1340893788251-1") {
addColumn(tableName: "game") {
column(name: "genre", type: "varchar(255)")
}
}
}
Second file (assign default values to existing records) worked fine:
databaseChangeLog = {
changeSet(author: "Ryan (generated)", id: "defaultValue") {
grailsChange{
change{
sql.executeUpdate("UPDATE game SET genre = 'Other'")
}
}
}
}
Third file (add not null constraint) silently fails:
databaseChangeLog = {
changeSet(author: "Ryan (generated)", id: "notNull") {
addNotNullConstraint(tableName: "game", columnName: "genre")
}
}
The log file only shows this in relation to the plugin:
2012-06-28 10:17:11,694 [main] INFO liquibase - Successfully acquired change log lock
2012-06-28 10:17:11,972 [main] INFO liquibase - Reading fromDATABASECHANGELOG
2012-06-28 10:17:11,980 [main] INFO liquibase - Reading fromDATABASECHANGELOG
2012-06-28 10:17:12,009 [main] INFO liquibase - Successfully released change log lock
If I check the databasechangelog table I can see that the script has not been executed. The console gave me this:
| Starting dbm-update for database root @ jdbc:mysql://localhost/migration
but there is no
| Finished dbm-update
like I see with the successful updates.
NOTE: It may be important to note that the site I am using as a reference is using version 1.0, I am using version 1.1. I am having a hard time finding tutorials or examples of this plugin at all, harder still to find info for the most recent version (released a month ago).
Can anyone pinpoint where I'm going wrong with the not null constraint?
Upvotes: 3
Views: 2767
Reputation: 760
While at lunch I was thinking about this and wondered why I hadn't taken an obvious debugging step. The groovy script changes were made from scratch. The update to add the column worked, which means that the only difference between my DB and my domain object was the not nullable constraint:
class Game {
String genre
static constraints = {
genre(nullable: false, blank: false)
}
}
...so why not run grails dbm-gorm-diff
and see what the plugin comes up with?
The result was this:
databaseChangeLog = {
changeSet(author: "Ryan (generated)", id: "1340897310305-1") {
addNotNullConstraint(columnDataType: "varchar(255)", columnName: "genre", tableName: "game")
}
}
The only difference between my created change script and the script that the plugin made was the:
columnDataType: "varchar(255)"
I added that to the script I made (just to be sure I hadn't done anything else wrong) and bingo, update worked.
Upvotes: 6