0

I am adding a field using the migrations plugin:

databaseChangeLog = {
    changeSet(author: "me", id: "add publish_date") {
        addColumn(tableName: "book") {
            column(name: "publish_date", type: "timestamp") {
                constraints(nullable: "true")
            }
        }
    }
}

I would also like to update publish_date for some Books. One way of doing it is by using sql.execute("UPDATE book SET publish_date = ____ WHERE year = 2012") but this doesn't seem very database agnostic... and I'd like it to be.

I'm thinking that using Book domain in the migration would ensure that the migration works for different databases. Can anyone comment if this is sane/possible?

changeSet(author: "me", id: "add publish_date") {
    grailsChange {
        change {
            Book.findAllByYear(2012).each { book ->
                book.publishDate = _____
                book.save()
            }
        }
    }
}

What are the options if you wanted to keep the migrations database agnostic?

4

1 回答 1

1

使用 Liquibase + 插件的方法是

update(tableName: 'book') {
   column name: 'publish_date', value: '____'
   where 'year = 2012'
}

这与原始 SQL 方法有类似的问题,因为它可能因不同的数据库而异,但 Liquibase 没有 Hibernate 的 HQL 之类的东西。您可以将变更集限制为仅适用于特定数据库,因此您可以为您使用的每个数据库创建一个(例如,在 prod 中的类型与 dev 不同),并且只有正确的数据库才会运行。

GORM 的问题在于脚本是 Groovy 类并且不会使用不同版本的代码进行编译,因此如果有人落后或新开发人员通过运行所有迁移来构建数据库,他们将因编译错误而失败.

所以你需要选择哪种方法最不坏。

于 2012-12-12T00:39:44.927 回答