grails-database-migration icon indicating copy to clipboard operation
grails-database-migration copied to clipboard

Postgres dbm-gorm-diff fails with Integer defaultValue

Open koji-k opened this issue 8 years ago • 8 comments

Situation

I use Grails 3.2.4, Hibernate5 and PostgreSQL 9.3.15. When i specify defaultValue in mapping on Domain, dbm-gorm-diff fails after second time. I summarized the situation.

Preparation and Migrate first time

1. Create new Grails 3.2.4 application. (with database-migration:3.0.0, org.liquibase:liquibase-core:3.5.3)

2. Setup database-migration plugin.(create changelog.groovy and tables)

grails dbm-generate-changelog changelog.groovy
grails dbm-update

changelog.groovy

databaseChangeLog = {
}

tables

migrationtest=# \d
               List of relations
 Schema |         Name          | Type  | Owner 
--------+-----------------------+-------+-------
 public | databasechangelog     | table | koji
 public | databasechangeloglock | table | koji
(2 rows)

migrationtest=# 

3. Create Domain.

package migrationtest

class Person {

    Integer age = -1

    static constraints = {
    }

    static mapping = {
        age defaultValue: "-1"
    }
}

4. migration!

dbm-gorm-diff 1.groovy --add
dbm-update

5. check result. No problem.

migrationtest=# \d
                 List of relations
 Schema |         Name          |   Type   | Owner 
--------+-----------------------+----------+-------
 public | databasechangelog     | table    | koji
 public | databasechangeloglock | table    | koji
 public | hibernate_sequence    | sequence | koji
 public | person                | table    | koji
 public | person_id_seq         | sequence | koji
(5 rows)

migrationtest=# 
migrationtest=# \d person
                          Table "public.person"
 Column  |  Type   |                      Modifiers                      
---------+---------+-----------------------------------------------------
 id      | bigint  | not null default nextval('person_id_seq'::regclass)
 version | bigint  | not null
 age     | integer | not null default (-1)
Indexes:
    "personPK" PRIMARY KEY, btree (id)

migrationtest=# 

Then a problem occurs.

When i execute dbm-gorm-diff now again, occurs follwoing error.

INFO 17/01/10 11:03: liquibase: Can not use class org.grails.plugins.databasemigration.liquibase.GormDatabase as a Liquibase service because it does not have a no-argument constructor
Command execution error: liquibase.command.CommandExecutionException: java.lang.ClassCastException: liquibase.statement.DatabaseFunction cannot be cast to java.lang.Integer
:dbmGormDiff FAILED

FAILURE: Build failed with an exception.

Actually the following message should be displayed because there is NOT change.

databaseChangeLog = {
}

BUILD SUCCESSFUL

When i delete or comemnt out age defaultValue: "-1" from static mapping, then

databaseChangeLog = {
}

BUILD SUCCESSFUL

displayed.

By the way, if property is String, does not occurs this problem. I think that this problem is caused by "defaultValue". Or am i something wrong?

I created sample repository for this issue. https://github.com/koji-k/grails3-database-migration-debug

Note

This problem occurs Liquibase side.
I added issue on JIRA of Liquibase. https://liquibase.jira.com/browse/CORE-2986

koji-k avatar Jan 10 '17 15:01 koji-k

This is a problem with liquibase/liquibase-hibernate. You need to report the issue there.

The issue is:

The postgres snapshot for the age column returns a liquibase.statement.DatabaseFunction when getAttribute("defaultValue", Object.class) is called (https://github.com/liquibase/liquibase/blob/a57dfe9c73a849dcfd71a59cbcd394611a828bb2/liquibase-core/src/main/java/liquibase/diff/compare/core/DefaultDatabaseObjectComparator.java#L62).

Eventually to compare the values it attempts to call Integer.compareTo() and pass in the DatabaseFunction, which fails because the compareTo method only accepts another Integer (https://github.com/liquibase/liquibase/blob/a57dfe9c73a849dcfd71a59cbcd394611a828bb2/liquibase-core/src/main/java/liquibase/diff/ObjectDifferences.java#L133)

When you create an issue with liquibase, please link to this issue.

jameskleeh avatar Jan 18 '17 21:01 jameskleeh

PS: Thanks so much for the sample repo with the docker instructions. Made my life a lot easier!

jameskleeh avatar Jan 18 '17 21:01 jameskleeh

@jameskleeh Thank you for checking this proboem! I wrote about this proboem on JIRA of Liquibase. https://liquibase.jira.com/browse/CORE-2986

I am glad that i can take part for this plugin. This Migration plugin is helping my life every day. Really thanks for providing this nice plugin!

koji-k avatar Jan 19 '17 10:01 koji-k

I got the same issue with h2 database. So I think this is not just related to Postgres.

YingchunLi avatar Feb 08 '17 02:02 YingchunLi

If you came here with the same issue, please create a jira account and upvote the ticket to speed up resolution: https://liquibase.jira.com/browse/CORE-2986

edwardotis avatar Feb 09 '17 05:02 edwardotis

Hi Liquibase project works again. https://github.com/liquibase/liquibase/releases There 2 Updates from 3.5.3.(so latest version is 3.5.5)

I wrote a ISSUE on Jira of Liquibase last year. https://liquibase.jira.com/browse/CORE-2986) My issue is not yet resolved maybe. But there are many Bugfix from Liquibase 3.5.3. Does grails-database-migration plugin update Liquibase version?

koji-k avatar Mar 26 '18 12:03 koji-k

@koji-k v3.0.4 of the plugin will be using liquibase 3.5.5

puneetbehl avatar Aug 24 '18 15:08 puneetbehl

@puneetbehl Thanks! i will try it!

koji-k avatar Sep 24 '18 15:09 koji-k