使用 Grails 数据库迁移,我在生产中定义了一个表,如下所示:
+--------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| version | bigint(20) | NO | | NULL | |
| basket_id | bigint(20) | NO | MUL | NULL | |
| gift_card_id | bigint(20) | NO | MUL | NULL | |
+--------------+------------+------+-----+---------+----------------+
在最新的开发分支中,我们修改了这个域以使用复合键并删除 id 和版本:
class BasketGiftCard implements Serializable {
Basket basket
GiftCard giftCard
static mapping = {
id composite: ['basket', 'giftCard']
version false
}
}
运行 dbm-gorm-diff 时,我们最终得到以下结果:
changeSet(author: "gdboling (generated)", id: "1340670757336-8") {
addPrimaryKey(columnNames: "basket_id, gift_card_id", constraintName: "basket_gift_cPK", tableName: "basket_gift_card")
}
changeSet(author: "gdboling (generated)", id: "1340670757336-16") {
dropPrimaryKey(tableName: "basket_gift_card")
}
changeSet(author: "gdboling (generated)", id: "1340670757336-188") {
dropColumn(columnName: "id", tableName: "basket_gift_card")
}
changeSet(author: "gdboling (generated)", id: "1340670757336-189") {
dropColumn(columnName: "version", tableName: "basket_gift_card")
}
关于这个的有趣(和错误)是顺序。dropPrimaryKey 应该首先出现,然后是其他所有内容。照原样,当运行 dbm-update 时,它会失败。