【问题标题】:grails changelog preconditions not doing anythinggrails changelog 先决条件不做任何事情
【发布时间】:2014-06-24 19:39:21
【问题描述】:

我正在尝试使用更改日志对数据库进行更改。由于我不能保证特定代码的值当前存在,但可能存在,因此我需要能够检查它们以便进行插入或更新。

这是我一直在测试的,它似乎没有做任何事情。欢迎任何意见。

databaseChangeLog = {
    changeSet(author:'kmert', id:'tubecap-insert-update-1') {
        preConditions(onFail="WARN",onFailMessage:"Tube cap does not exist,skipping because it cannot be updated."){
            sqlCheck(expectedResult='1', 'SELECT * FROM [ltc2_tube_cap] WHERE code=11')
        }
        grailsChange {
            change {
                sql.execute("""
                    UPDATE [ltc2_tube_cap]
                    SET [name] = 'White'
                    WHERE [code] = 11;
                """)
            }
            rollback {
            }
        }
    }
}

更新:我运行了更改日志脚本,但现在出现此错误。我从在线资源中找到了代码。我找不到很多关于先决条件的文档...

|为数据库 hapi_app_user @ jdbc:jtds:sqlserver://localhost;databaseName=LabTraffic;MVCC=TRUE;LOCK_TIMEOUT=10000 启动 dbm-update 解析 TubeCapUpdate.groovy 的问题:没有方法签名:grails.plugin.databasemigration.DslBuilder.sqlCheck() 适用于参数类型:(java.lang.String, java.lang.String) 值:[1, SELECT * FROM ltc2_tube_cap WHERE code=11](使用 --verbose 重新运行以查看堆栈跟踪) 解析 changelog.groovy 的问题:没有方法签名:grails.plugin.databasemigration.DslBuilder.sqlCheck() 适用于参数类型:(java.lang.String, java.lang.String) 值:[1, SELECT * FROM ltc2_tube_cap WHERE code=11](使用 --verbose 重新运行以查看堆栈跟踪) groovy.lang.MissingMethodException:没有方法签名:grails.plugin.databasemigration.DslBuilder.sqlCheck() 适用于参数类型:(java.lang.String, java.lang.String) 值:[1, SELECT * FROM ltc2_tube_cap哪里代码=11] 在 grails.plugin.databasemigration.DslBuilder.invokeMethod(DslBuilder.groovy:117) 在 Script1$_run_closure1_closure2_closure3.doCall(Script1.groovy:13) 在 grails.plugin.databasemigration.DslBuilder.invokeMethod(DslBuilder.groovy:117) 在 Script1$_run_closure1_closure2.doCall(Script1.groovy:12) 在 grails.plugin.databasemigration.DslBuilder.invokeMethod(DslBuilder.groovy:117) 在 Script1$_run_closure1.doCall(Script1.groovy:11) 在 grails.plugin.databasemigration.GrailsChangeLogParser.parse(GrailsChangeLogParser.groovy:84) 在 grails.plugin.databasemigration.DslBuilder.handleIncludedChangeLog(DslBuilder.groovy:747) 在 grails.plugin.databasemigration.DslBuilder.createNode(DslBuilder.groovy:139) 在 grails.plugin.databasemigration.DslBuilder.createNode(DslBuilder.groovy:590) 在 grails.plugin.databasemigration.DslBuilder.invokeMethod(DslBuilder.groovy:117) 在 Script1$_run_closure1.doCall(Script1.groovy:6) 在 grails.plugin.databasemigration.GrailsChangeLogParser.parse(GrailsChangeLogParser.groovy:84) 在 liquibase.Liquibase.update(Liquibase.java:107) 在 DbmUpdate$_run_closure1_closure2.doCall(DbmUpdate:26) 在 _DatabaseMigrationCommon_groovy$_run_closure2_closure11.doCall(_DatabaseMigrationCommon_groovy:59) 在 grails.plugin.databasemigration.MigrationUtils.executeInSession(MigrationUtils.groovy:133) 在 _DatabaseMigrationCommon_groovy$_run_closure2.doCall(_DatabaseMigrationCommon_groovy:51) 在 DbmUpdate$_run_closure1.doCall(DbmUpdate:25)

【问题讨论】:

    标签: grails changelog preconditions


    【解决方案1】:

    确保以下内容

    grails dbm-gorm-diff add-your-file-forupdate.groovy -add
    

    那么在your-file-forupdate.groovy里面应该会看到

    databaseChangeLog = {
        changeSet(author:'kmert', id:'tubecap-insert-update-1') {
     .
     .
     .
        }
    }
    

    那么,最重要的是您已经将其作为迁移脚本文件包含在内,以如下方式执行:

    只需在 grails-app/migrations/changelog.groovy 的末尾手动添加如下一行:

    包含文件:'your-file-forupdate.groovy'

    changelog.groovy 始终从头到尾运行,因此请确保始终将新创建的迁移添加到末尾。

    干杯!欲了解更多信息,请参阅this

    【讨论】:

    • 该文件已添加到我的 changelog.groovy 中。我终于发现它根本没有运行的原因显然是另一个迁移脚本正在死去。现在,它正在运行,我的困境已经改变。
    【解决方案2】:

    您的 sqlCheck 前置条件语法不正确。

     sqlCheck(expectedResult:'1', 'SELECT * FROM [ltc2_tube_cap] WHERE code=11')
    

    请注意,您的代码中的第一个参数是赋值语句expectedResult=1,它应该是映射条目expectedResult:1

    我在这个 Jira 页面上找到了答案。 https://jira.grails.org/browse/GPDATABASEMIGRATION-40 具有讽刺意味的是,它是在向文档中添加大量的 DB 迁移 DSL 示例。

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多