liquibase-hibernate icon indicating copy to clipboard operation
liquibase-hibernate copied to clipboard

Wrong column type generated with diffChangelog

Open aepshteyn opened this issue 1 year ago • 0 comments

I'm using liquibase-hibernate5-4.25.1 with a hibernate:ejb3:... referenceUrl.

In my java source code I removed a nullable = false attribute from a @Column annotation on an entity field like this:

  @Column(columnDefinition = "binary(32)", length = 32/* nullable = false*/)
  private byte[] passwordHash;

After making the above modification, I ran liquibase diff-changelog and got the following changeset:

  <changeSet author="Alex (generated)" id="1706924110096-1">
    <dropNotNullConstraint columnDataType="blob(32)" columnName="passwordHash" tableName="User"/>
  </changeSet>

This is wrong because the actual column type is binary(32), but the generated changeSet uses blob(32), which results in the following update SQL:

--  Changeset changelog-0.1.mysql.xml::1706924110096-1::Alex (generated)
ALTER TABLE snake.User MODIFY passwordHash BLOB NULL;

Applying this change to the (MySQL) database changed the column type from binary(32) to blob, and this, in turn, causes the Hibernate schema validation ("hibernate.hbm2ddl.auto"="validate") to fail at runtime:

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [passwordHash] in table [User]; found [blob (Types#LONGVARBINARY)], but expecting [binary(32) (Types#VARBINARY)]

I suspect that the liquibase-hibernate plugin is failing to correctly parse the columnDefinition attribute of the JPA @Column annotation.

For your reference, my baseline changelog generated with liquibase generateChangeLog against the pre-migration database schema (created by Hibernate with "hibernate.hbm2ddl.auto"="create") from the original JPA source code (before I removed the nullable = false constraint) does have the correct column type:

<createTable tableName="user">
   ...
    <column name="passwordHash" type="BINARY(32)">
        <constraints nullable="false"/>
    </column>
    ...
</createTable>

aepshteyn avatar Feb 04 '24 22:02 aepshteyn