3

我正在尝试使用此站点作为参考来熟悉Grails 数据库迁移插件。尝试添加具有非空约束的属性时遇到问题。我以与站点上显示的相同方式对脚本进行建模(添加列,为现有记录设置默认值,然后向列添加不可为空的约束):

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")
    }
}

当我尝试更新我的数据库时,它会默默地失败。因此,我将这三个更改拆分为三个不同的 groovy 脚本来追踪问题。

第一个文件(添加列)工作正常:

databaseChangeLog = {
    changeSet(author: "Ryan (generated)", id: "1340893788251-1") {
        addColumn(tableName: "game") {
            column(name: "genre", type: "varchar(255)")
        }
    }
}

第二个文件(为现有记录分配默认值)工作正常:

databaseChangeLog = {
    changeSet(author: "Ryan (generated)", id: "defaultValue") {
        grailsChange{
            change{
                sql.executeUpdate("UPDATE game SET genre = 'Other'")
            }
        }
    }
}

第三个文件(添加非空约束)静默失败:

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 from DATABASECHANGELOG
2012-06-28 10:17:11,980 [main] INFO liquibase - Reading from DATABASECHANGELOG
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?

4

1 回答 1

5

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.

于 2012-06-28T18:18:55.140 回答