Grails db-migration dbm-db-doc 脚本失败

Posted

技术标签:

【中文标题】Grails db-migration dbm-db-doc 脚本失败【英文标题】:Grails db-migration dbm-db-doc script fails 【发布时间】:2015-10-05 14:18:46 【问题描述】:

我尝试运行“dbm-db-doc”脚本。该脚本似乎无法正常工作,因为它在已执行的更改日志上失败。这是变化:

changeSet(author: "Bernardo (generated)", id: "1436991688243-1") 
    addColumn(tableName: "prepares_for_exam") 
        column(name: "exam_id", type: "bigint")
    

    grailsChange 
        change 
            sql.execute("UPDATE prepares_for_exam JOIN product ON prepares_for_exam.id = product.prepares_for_exam_id SET prepares_for_exam.exam_id = product.id")
        
        rollback 
        
    

    addNotNullConstraint(columnDataType: "bigint", tableName: "prepares_for_exam", columnName: "exam_id")


changeSet(author: "Bernardo (generated)", id: "1436991688243-32") 
    dropColumn(columnName: "prepares_for_exam_id", tableName: "product")

并且在数据库中,此更改日志已被标记为“已执行”

但在运行脚本时,脚本似乎想要执行该更改日志并失败,因为“product.prepares_for_exam_id”列已被删除。

完全错误:

|Starting dbm-db-doc for database root @ jdbc:mysql://localhost:3306/xxx?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'product.prepares_for_exam_id' in 'on clause'
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2828)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2777)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:949)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:795)
    at Script1$_run_closure1_closure2_closure35_closure36.doCall(Script1.groovy:10)
    at grails.plugin.databasemigration.GrailsChange.generateStatements(GrailsChange.groovy:159)
    at liquibase.change.AbstractChange.getAffectedDatabaseObjects(AbstractChange.java:208)
    at liquibase.changelog.visitor.DBDocVisitor.visit(DBDocVisitor.java:95)
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:58)
    at liquibase.Liquibase.generateDocumentation(Liquibase.java:740)
    at DbmDbDoc$_run_closure1_closure2.doCall(DbmDbDoc:27)
    at _DatabaseMigrationCommon_groovy$_run_closure2_closure11.doCall(_DatabaseMigrationCommon_groovy:59)
    at grails.plugin.databasemigration.MigrationUtils.executeInSession(MigrationUtils.groovy:133)
    at _DatabaseMigrationCommon_groovy$_run_closure2.doCall(_DatabaseMigrationCommon_groovy:51)
    at DbmDbDoc$_run_closure1.doCall(DbmDbDoc:26)
    at org.grails.ide.api.impl.GrailsConnectorImpl.executeCommand(GrailsConnectorImpl.java:160)
    at org.grails.ide.eclipse.longrunning.process.GrailsProcess.run(GrailsProcess.java:130)
    at org.grails.ide.eclipse.longrunning.process.GrailsProcess.main(GrailsProcess.java:93)
Error |
org.codehaus.groovy.grails.cli.ScriptExitException
Error |

所以我的问题是:

“dbm-db-doc”脚本中是否存在错误,或者我是否在变更集上搞砸了?

【问题讨论】:

【参考方案1】:

我正在回答我自己的问题,因为我找到了解决方案:

似乎迁移插件每次都在执行“grailsChange”部分,尽管已经执行了changeSet(我不知道为什么......欢迎回答!)

解决方案是不使用“grailsChange”,而是使用普通的“sql”命令。这是解决方案:

changeSet(author: "Bernardo (generated)", id: "1436991688243-1") 
        addColumn(tableName: "prepares_for_exam") 
            column(name: "exam_id", type: "bigint")
        

        sql(""" UPDATE prepares_for_exam JOIN product ON prepares_for_exam.id = product.prepares_for_exam_id SET prepares_for_exam.exam_id = product.id """ )

        addNotNullConstraint(columnDataType: "bigint", tableName: "prepares_for_exam", columnName: "exam_id")
    

【讨论】:

以上是关于Grails db-migration dbm-db-doc 脚本失败的主要内容,如果未能解决你的问题,请参考以下文章

当我想使用 db-migration 更新数据库时,如何防止数据丢失?

node.js db-migrate 创建扩展问题

将 npm db-migrate 与 Elastic Beanstalk 一起使用

Grails:Grails3:doWithWebDescriptor?

用于 Grails 2.0 的 Grails/Gradle 插件

如何使用 grails 命令行编译 grails 项目?