4

I am trying to make changes to the database using the changelog. Since I cannot guarantee that the values currently exist for the specific code, but could exist, I need to be able to check for them in order to either do an insert or an update.

Here is what I have been testing, which doesn't appear to do anything. Any words of advice are welcome.

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

UPDATE: I got the changelog script running, but I am now getting this error. I found the code from an online source. I cannot find a lot of documentation on preconditions...

| Starting dbm-update for database hapi_app_user @ jdbc:jtds:sqlserver://localhost;databaseName=LabTraffic;MVCC=TRUE;LOCK_TIMEOUT=10000 problem parsing TubeCapUpdate.groovy: No signature of method: grails.plugin.databasemigration.DslBuilder.sqlCheck() is applicable for argument types: (java.lang.String, java.lang.String) values: [1, SELECT * FROM ltc2_tube_cap WHERE code=11] (re-run with --verbose to see the stacktrace) problem parsing changelog.groovy: No signature of method: grails.plugin.databasemigration.DslBuilder.sqlCheck() is applicable for argument types: (java.lang.String, java.lang.String) values: [1, SELECT * FROM ltc2_tube_cap WHERE code=11] (re-run with --verbose to see the stacktrace) groovy.lang.MissingMethodException: No signature of method: grails.plugin.databasemigration.DslBuilder.sqlCheck() is applicable for argument types: (java.lang.String, java.lang.String) values: [1, SELECT * FROM ltc2_tube_cap WHERE code=11] at grails.plugin.databasemigration.DslBuilder.invokeMethod(DslBuilder.groovy:117) at Script1$_run_closure1_closure2_closure3.doCall(Script1.groovy:13) at grails.plugin.databasemigration.DslBuilder.invokeMethod(DslBuilder.groovy:117) at Script1$_run_closure1_closure2.doCall(Script1.groovy:12) at grails.plugin.databasemigration.DslBuilder.invokeMethod(DslBuilder.groovy:117) at Script1$_run_closure1.doCall(Script1.groovy:11) at grails.plugin.databasemigration.GrailsChangeLogParser.parse(GrailsChangeLogParser.groovy:84) at grails.plugin.databasemigration.DslBuilder.handleIncludedChangeLog(DslBuilder.groovy:747) at grails.plugin.databasemigration.DslBuilder.createNode(DslBuilder.groovy:139) at grails.plugin.databasemigration.DslBuilder.createNode(DslBuilder.groovy:590) at grails.plugin.databasemigration.DslBuilder.invokeMethod(DslBuilder.groovy:117) at Script1$_run_closure1.doCall(Script1.groovy:6) at grails.plugin.databasemigration.GrailsChangeLogParser.parse(GrailsChangeLogParser.groovy:84) at liquibase.Liquibase.update(Liquibase.java:107) at DbmUpdate$_run_closure1_closure2.doCall(DbmUpdate:26) 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 DbmUpdate$_run_closure1.doCall(DbmUpdate:25)

k_jaeger
  • 61
  • 1
  • 6

2 Answers2

2

Your syntax is incorrect for the sqlCheck preCondition.

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

Notice in your code the first argument is an assignment statement expectedResult=1 and it should be a map entry expectedResult:1.

I found the answer buried on this Jira page. https://jira.grails.org/browse/GPDATABASEMIGRATION-40 which ironically is about adding lots of examples to the DB migration DSL to the documentation.

burns
  • 1,091
  • 13
  • 15
0

Make sure the following

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

then inside your-file-forupdate.groovy is expected to see

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

Then ,the Big deal is either you have included this as migration script file to be executed as follow:

just manually add a line like the following to the end of grails-app/migrations/changelog.groovy:

include file: 'your-file-forupdate.groovy'

The changelog.groovy is always run from beginning to end, so make sure that you always add newly created migrations to the end.

Cheers! for more info see this

Develop4Life
  • 7,581
  • 8
  • 58
  • 76
  • The file has been added to my changelog.groovy already. I finally figured out that the reason it wasn't running at all was apparently another migration script was dying. Now, that it is running my dilemma has changed. – k_jaeger May 08 '14 at 15:31