Bernhard
Bernhard

Reputation: 444

Grails db-migration dbm-db-doc script fails

I tried to run the "dbm-db-doc" script. It seems that the script is not working properly because it failed on a changelog that already been executed. This is the change:

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")
}

And in the database, this changelog has been marked as "EXECUTED" changelog has been executed

But when running the script it seems that the script wants to execute that changelog and fails because the column "product.prepares_for_exam_id" has already been dropped.

Full error:

|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 |

So my question is:

Is there a bug in the "dbm-db-doc"-script or did I messed up something with the changeset?

Upvotes: 1

Views: 168

Answers (1)

Bernhard
Bernhard

Reputation: 444

I am answering my own question because I found the sollution:

It seems that the migration plugin keeps executing the "grailsChange" part every time although the changeSet has been executed already (I have no clue why... answers welcome!)

The sollution is to not use "grailsChange" and use the normal "sql" command instead. This is the sollution:

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")
    }

Upvotes: 2

Related Questions