Search code examples
hibernategrailsgrails-ormdata-migration

Grails: Hibernate and Data migrations


I have a domain class that is for a customer. I have had to change to domain class recently because we were gathering a billing address and a shipping address, but were only recording one zip code value. So I thought what a simple fix. Change zipcode to billzipcode and then create a new field for shipzipcode. I ran dbm-gorm-diff to create the change log and then ran dbm-update to execute the changes I've made. Everything to this point works swimmingly. But then I go to debug the app to see that the changes to datasource were ok. However, now when I try to save a new customer record it get this error:

    NULL not allowed for column "ZIPCODE"; SQL statement:
insert into customer (id, version, billaddr, billcity, billstate, billzipcode, cell, contact, country_id, custcode, custname, date_created, email, fax, last_updated, organization, phone, shipaddr, shipasbill, shipcity, shipstate, shipzipcode, status, tenant_id) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-164]. Stacktrace follows:
org.h2.jdbc.JdbcSQLException: NULL not allowed for column "ZIPCODE"; SQL statement:
insert into customer (id, version, billaddr, billcity, billstate, billzipcode, cell, contact, country_id, custcode, custname, date_created, email, fax, last_updated, organization, phone, shipaddr, shipasbill, shipcity, shipstate, shipzipcode, status, tenant_id) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-164]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
    at org.h2.message.DbException.get(DbException.java:169)
    at org.h2.message.DbException.get(DbException.java:146)
    at org.h2.table.Column.validateConvertUpdateSequence(Column.java:293)
    at org.h2.table.Table.validateConvertUpdateSequence(Table.java:680)
    at org.h2.command.dml.Insert.insertRows(Insert.java:120)
    at org.h2.command.dml.Insert.update(Insert.java:84)
    at org.h2.command.CommandContainer.update(CommandContainer.java:73)
    at org.h2.command.Command.executeUpdate(Command.java:226)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:143)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:129)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    at com.companyName.appname.billing.CustomerController$_closure6.doCall(CustomerController.groovy:45)
    at grails.plugin.multitenant.core.servlet.CurrentTenantServletFilter.doFilter(CurrentTenantServletFilter.java:53)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

I'm at a loss because the ZIPCODE column is not longer in the domain class. Any guidance would be appreciated.

EDIT: per request here are the changes that were generated by the migrations plugin:

changeSet(author: "user (generated)", id: "1363806498118-1") {
    dropColumn(tableName: "admission", columnName: "pen")
    addColumn(tableName: "admission") {
        column(name:"pen_id", type:"bigint")
    }       
}


changeSet(author: "user (generated)", id: "1363806498118-2") {
    addColumn(tableName: "customer") {
        column(name: "billzipcode", type: "varchar(50)") {
            constraints(nullable: "false")
        }
    }
}

changeSet(author: "user (generated)", id: "1363806498118-3") {
    addColumn(tableName: "customer") {
        column(name: "shipzipcode", type: "varchar(50)") {
            constraints(nullable: "false")
        }
    }
}

changeSet(author: "user (generated)", id: "1363806498118-4") {
    addColumn(tableName: "customer_costs") {
        column(name: "subtotal", type: "float(19)") {
            constraints(nullable: "false")
        }
    }
}


changeSet(author: "user (generated)", id: "1363806498118-5") {
    addForeignKeyConstraint(baseColumnNames: "pen_id", baseTableName: "admission", constraintName: "FK1A21809D0C6EF15", deferrable: "false", initiallyDeferred: "false", referencedColumnNames: "id", referencedTableName: "pen", referencesUniqueColumn: "false")
}

changeSet(author: "user (generated)", id: "1363806498118-6") {
    dropColumn(columnName: "zipcode", tableName: "customer")
}

Solution

  • If you notice your changeset the entry for billzipcode is actually adding a new column instead of renaming the zipcode column. Because of this you would still have to manage the data migration. Something like this should work in you case:

    changeSet(author: 'user (generated)', id: '1363806498118-4') {
        comment { 'Renaming zipcode to billzipcode' }
        renameColumn(tableName: 'customer_costs', oldColumnName: 'zipcode', newColumnName: 'billzipcode', columnDataType: 'varchar(50)')
    }
    

    Here's a related question and @Burt's accepted answer points to this link.