Grails数据库迁移更改日志困难

时间:2012-07-11 02:38:49

标签: grails database-migration

我正在努力关注this教程。以下是我采取的步骤:

1。)我首先创建我的空更改日志。在运行此命令之前,我将开发dbCreate设置为create

grails dbm-create-changelog

2。)然后我生成初始gorm更改日志。教程说我应该使用prod标志,但由于我的生产数据库是空的,我删除了prod标志。在我运行此命令之前,我会注释掉我的开发dbCreate设置。

grails dbm-generate-gorm-changelog --add changelog-1.0.groovy

此时,我的migrations文件夹包含文件changelog.groovychangelog-1.0.groovy。我将changelog-1.0.groovy称为changelog #1

3。)我现在尝试使用刚刚创建的更改日志初始化我的生产数据库。我在配置中设置了数据库迁移updateOnStartupdateOnStartFileNames,如我链接到的教程中所示。我的生产数据库存在,但是为空(没有表格)。我的生产环境的dbCreate设置已被注释掉。

grails prod run-app

问题:

乍一看,这似乎有效,但当我运行grails prod dbm-gorm-diff并生成changelog #2时,我看到了不应存在的更改。我还可以看到,与生产数据库的当前状态相比,变更集是合法的。这意味着生成数据库未使用changelog #1正确初始化。由于我没有更改任何内容,因此不应存在任何更改。我在运行grails prod dbm-gorm-diff之后立即运行了grails prod run-app

changelog #2中有五个变更集。我已经验证所有这些更改集都在changelog #1中表示。 changelog #2中的前四个变更集都有unique: "true"。这四个更改集也是unique: "true"中包含changelog #1的唯一更改集。我仍然无法解释为什么存在第五个变更集。虽然它是Spring Security plugin使用的user_role表格,但我没有什么值得注意的。这是changelog #2

databaseChangeLog = {

    changeSet(author: "typoknig (generated)", id: "1341970550063-1") {
        createIndex(indexName: "authority_unique_1341970549765", tableName: "role", unique: "true") { // This table is for the Spring Security plugin's "Role" domain class.
            column(name: "authority")
        }
    }

    changeSet(author: "typoknig (generated)", id: "1341970550063-2") {
        createIndex(indexName: "name_unique_1341970549772", tableName: "foo", unique: "true") { // This is just a table for one of my domain classes that happens to have a field with a unique constraint.
            column(name: "path")
        }
    }

    changeSet(author: "typoknig (generated)", id: "1341970550063-3") {
        createIndex(indexName: "name_unique_1341970549774", tableName: "bar", unique: "true") { // This is just a table for one of my domain classes that happens to have a field with a unique constraint.
            column(name: "name")
        }
    }

    changeSet(author: "typoknig (generated)", id: "1341970550063-4") {
        createIndex(indexName: "username_unique_1341970549777", tableName: "user", unique: "true") { // This table is for the Spring Security plugin's "User" domain class.
            column(name: "username")
        }
    }

    changeSet(author: "typoknig (generated)", id: "1341970550063-5") {
        createIndex(indexName: "FK143BF46A5FBC0B79", tableName: "user_role") { // This table is for the Spring Security plugin's "UserRole" domain class.
            column(name: "role_id")
        }
    }
}

如何确保正确初始化生产数据库?

更新#1:

我不知道为什么,但createIndex无法使用unique: "true"。对于前四个变更集,我只是将unique: "true"移动到changeset #1中最初创建相关列的位置。这只留下changelog #2中的第五个变更集来处理。我仍然没有看到变更集有任何问题,所以我不明白为什么它没有被应用。

更新#2:

我发现我可以将各个变更集中的所有调用createIndex移动到相应的变更集中,其中创建了表(索引所属的表)。 这似乎已经解决了我的所有问题。除非有人能提供令人信服的理由,否则我认为我会将这部分改成日志生成工作流程。例如,这些变更集:

changeSet(author: "typoknig (generated)", id: "1342037835503-31") {
    createTable(tableName: "user_role") {
        column(name: "role_id", type: "bigint") {
            constraints(nullable: "false")
        }

        column(name: "user_id", type: "bigint") {
            constraints(nullable: "false")
        }
    }
}

changeSet(author: "typoknig (generated)", id: "1342037835503-103") {
    createIndex(indexName: "FK143BF46A4E6CF59", tableName: "user_role") {
        column(name: "user_id")
    }
}

changeSet(author: "typoknig (generated)", id: "1342037835503-104") {
    createIndex(indexName: "FK143BF46A5FBC0B79", tableName: "user_role") {
        column(name: "role_id")
    }
}

将会出现这种变化:

changeSet(author: "typoknig (generated)", id: "1342037835503-31") {
    createTable(tableName: "user_role") {
        column(name: "role_id", type: "bigint") {
            constraints(nullable: "false")
        }

        column(name: "user_id", type: "bigint") {
            constraints(nullable: "false")
        }
    }
    createIndex(indexName: "FK143BF46A5FBC0B79", tableName: "user_role") {
        column(name: "role_id")
    }
    createIndex(indexName: "FK143BF46A4E6CF59", tableName: "user_role") {
        column(name: "user_id")
    }
}

1 个答案:

答案 0 :(得分:0)

变更集可能依赖于目标数据库。例如,用于将MyISAM表与MySQL和dbm-gorm-diff一起使用的grails.org站点将始终添加一些createIndex变更集,即使它们是在先前的变更集中定义的。

我真的不明白将createIndex声明移动到createTable变更集会有什么不同。你能澄清这样做是如何解决你所有问题的吗?

相关问题