Grails 从迁移更新字段
我正在使用迁移插件添加一个字段:Grails 从迁移更新字段,grails,gorm,database-migration,Grails,Gorm,Database Migration,我正在使用迁移插件添加一个字段: databaseChangeLog = { changeSet(author: "me", id: "add publish_date") { addColumn(tableName: "book") { column(name: "publish_date", type: "timestamp") { constraints(nullable: "true")
databaseChangeLog = {
changeSet(author: "me", id: "add publish_date") {
addColumn(tableName: "book") {
column(name: "publish_date", type: "timestamp") {
constraints(nullable: "true")
}
}
}
}
我还想更新一些书籍的出版日期
。一种方法是使用sql.execute(“UPDATE book SET publish\u date=\uuuuuu\uuuwhere year=2012”)
,但这似乎不是非常不可知于数据库的。。。我希望是这样
我认为在迁移中使用Book
domain可以确保迁移适用于不同的数据库。如果这是合理的/可能的,有人能发表评论吗
changeSet(author: "me", id: "add publish_date") {
grailsChange {
change {
Book.findAllByYear(2012).each { book ->
book.publishDate = _____
book.save()
}
}
}
}
如果想让迁移数据库不可知,有哪些选项?使用Liquibase+插件的方法是
update(tableName: 'book') {
column name: 'publish_date', value: '____'
where 'year = 2012'
}
这与原始SQL方法有类似的问题,因为不同的数据库可能不同,但Liquibase没有Hibernate的HQL。您可以将更改集限制为仅应用于特定数据库,这样您就可以为您使用的每个数据库创建一个更改集(例如,prod中的类型与dev中的类型不同),并且只有正确的更改集才会运行
GORM的问题是,脚本是Groovy类,不会使用不同版本的代码进行编译,因此如果有人落后,或者如果新开发人员通过运行所有迁移来构建数据库,它们将因编译错误而失败
因此,您需要选择哪种方法最不坏