This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 9.0.x-h7-improvement-phase3 in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 180a626d04928cee377575af2a7ac3cf50122d28 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Fri Oct 9 09:36:02 2026 -0500 test(hibernate7): state the column order native binding gives a generated change log With native binding the default, dbm-generate-gorm-changelog and dbm-gorm-diff list the columns of a createTable change set in the order Hibernate's annotation binder bound them: the identifier, the other properties sorted by name (the version among them), then the foreign keys of the second pass. Classic binding listed the identifier, the version, then the properties in declaration order. Hibernate only reorders the columns of the mapping model when a schema action is configured, which the migration commands do not set, so the two CLI specs that spelled out the classic order now state the native one, with the reason, and GeneratedModeSnapshotSpec pins the rule in both modes. Liquibase does not compare column order, so a diff against an existing database is unchanged. The deprecation spec's "default" feature follows the mode of the suite: with -Pgrails.test.classicDomainBinding=true the classic binder is the default and the warning is expected. Co-Authored-By: Claude Fable 5.1 <[email protected]> --- .../cfg/ClassicDomainBindingDeprecationSpec.groovy | 5 +++- .../cli/DbmGenerateGormChangelogCommandSpec.groovy | 24 ++++++++++------ .../dbmigration/cli/DbmGormDiffCommandSpec.groovy | 16 ++++++++--- .../generated/GeneratedModeSnapshotSpec.groovy | 32 ++++++++++++++++++++++ 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/ClassicDomainBindingDeprecationSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/ClassicDomainBindingDeprecationSpec.groovy index 8aef741e79..9029f3059d 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/ClassicDomainBindingDeprecationSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/ClassicDomainBindingDeprecationSpec.groovy @@ -76,11 +76,14 @@ class ClassicDomainBindingDeprecationSpec extends Specification { } void "the default, native domain binding, starts without the warning"() { + given: 'the suite may run through the classic binder as a whole, which makes the classic binder the default' + boolean classicSuite = Boolean.getBoolean('grails.hibernate.classicDomainBinding') + when: String log = bootAndCaptureLog([:]) then: - !log.contains(WARNING) + log.contains(WARNING) == classicSuite DomainOne.withTransaction { new DomainOne(controller: 'book', action: 'native').save(flush: true, failOnError: true) DomainOne.count() == 1 diff --git a/grails-data-hibernate7/dbmigration/src/test-cli/groovy/org/apache/grails/data/hibernate7/dbmigration/cli/DbmGenerateGormChangelogCommandSpec.groovy b/grails-data-hibernate7/dbmigration/src/test-cli/groovy/org/apache/grails/data/hibernate7/dbmigration/cli/DbmGenerateGormChangelogCommandSpec.groovy index 8ef523c163..2a9d7aa86a 100644 --- a/grails-data-hibernate7/dbmigration/src/test-cli/groovy/org/apache/grails/data/hibernate7/dbmigration/cli/DbmGenerateGormChangelogCommandSpec.groovy +++ b/grails-data-hibernate7/dbmigration/src/test-cli/groovy/org/apache/grails/data/hibernate7/dbmigration/cli/DbmGenerateGormChangelogCommandSpec.groovy @@ -28,6 +28,14 @@ class DbmGenerateGormChangelogCommandSpec extends ApplicationContextDatabaseMigr return DbmGenerateGormChangelogCommand } + // Native domain binding (the default) lists the columns of a table in the order Hibernate's annotation binder binds + // them: the identifier, then the other properties sorted by name (the version among them), then the foreign keys it + // resolves in a second pass. Classic binding listed the identifier, the version, then the properties in declaration + // order. Hibernate only reorders the columns of the mapping model when a schema action (hbm2ddl.auto / dbCreate) is + // configured, which the migration commands do not set, so the generated change log shows the binding order. The + // snapshot of an existing database is not affected: Liquibase does not compare column order. See + // GeneratedModeSnapshotSpec and the "Differences in the Schema" section of the Native Domain Binding chapter. + def "writes Change Log to copy the current state of the database to STDOUT"() { when: command.handle(getExecutionContext()) @@ -42,11 +50,11 @@ databaseChangeLog = \\{ constraints\\(nullable: "false", primaryKey: "true", primaryKeyName: "authorPK"\\) \\} - column\\(name: "version", type: "BIGINT"\\) \\{ + column\\(name: "name", type: "VARCHAR\\(255\\)"\\) \\{ constraints\\(nullable: "false"\\) \\} - column\\(name: "name", type: "VARCHAR\\(255\\)"\\) \\{ + column\\(name: "version", type: "BIGINT"\\) \\{ constraints\\(nullable: "false"\\) \\} \\} @@ -58,11 +66,11 @@ databaseChangeLog = \\{ constraints\\(nullable: "false", primaryKey: "true", primaryKeyName: "bookPK"\\) \\} - column\\(name: "version", type: "BIGINT"\\) \\{ + column\\(name: "title", type: "VARCHAR\\(255\\)"\\) \\{ constraints\\(nullable: "false"\\) \\} - column\\(name: "title", type: "VARCHAR\\(255\\)"\\) \\{ + column\\(name: "version", type: "BIGINT"\\) \\{ constraints\\(nullable: "false"\\) \\} @@ -97,11 +105,11 @@ databaseChangeLog = \\{ constraints\\(nullable: "false", primaryKey: "true", primaryKeyName: "authorPK"\\) \\} - column\\(name: "version", type: "BIGINT"\\) \\{ + column\\(name: "name", type: "VARCHAR\\(255\\)"\\) \\{ constraints\\(nullable: "false"\\) \\} - column\\(name: "name", type: "VARCHAR\\(255\\)"\\) \\{ + column\\(name: "version", type: "BIGINT"\\) \\{ constraints\\(nullable: "false"\\) \\} \\} @@ -113,11 +121,11 @@ databaseChangeLog = \\{ constraints\\(nullable: "false", primaryKey: "true", primaryKeyName: "bookPK"\\) \\} - column\\(name: "version", type: "BIGINT"\\) \\{ + column\\(name: "title", type: "VARCHAR\\(255\\)"\\) \\{ constraints\\(nullable: "false"\\) \\} - column\\(name: "title", type: "VARCHAR\\(255\\)"\\) \\{ + column\\(name: "version", type: "BIGINT"\\) \\{ constraints\\(nullable: "false"\\) \\} diff --git a/grails-data-hibernate7/dbmigration/src/test-cli/groovy/org/apache/grails/data/hibernate7/dbmigration/cli/DbmGormDiffCommandSpec.groovy b/grails-data-hibernate7/dbmigration/src/test-cli/groovy/org/apache/grails/data/hibernate7/dbmigration/cli/DbmGormDiffCommandSpec.groovy index 801e6d5c35..9ef0a38ae4 100644 --- a/grails-data-hibernate7/dbmigration/src/test-cli/groovy/org/apache/grails/data/hibernate7/dbmigration/cli/DbmGormDiffCommandSpec.groovy +++ b/grails-data-hibernate7/dbmigration/src/test-cli/groovy/org/apache/grails/data/hibernate7/dbmigration/cli/DbmGormDiffCommandSpec.groovy @@ -32,6 +32,14 @@ class DbmGormDiffCommandSpec extends ApplicationContextDatabaseMigrationCommandS sql.executeUpdate 'CREATE TABLE PUBLIC.author (id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, version BIGINT NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT authorPK PRIMARY KEY (id));' } + // Native domain binding (the default) lists the columns of a table in the order Hibernate's annotation binder binds + // them: the identifier, then the other properties sorted by name (the version among them), then the foreign keys it + // resolves in a second pass. Classic binding listed the identifier, the version, then the properties in declaration + // order. Hibernate only reorders the columns of the mapping model when a schema action (hbm2ddl.auto / dbCreate) is + // configured, which the migration commands do not set, so the generated change log shows the binding order. The + // snapshot of an existing database is not affected: Liquibase does not compare column order. See + // GeneratedModeSnapshotSpec and the "Differences in the Schema" section of the Native Domain Binding chapter. + def "diffs GORM classes against a database and generates a changelog to STDOUT"() { when: command.handle(getExecutionContext()) @@ -46,11 +54,11 @@ databaseChangeLog = \\{ constraints\\(nullable: "false", primaryKey: "true", primaryKeyName: "bookPK"\\) \\} - column\\(name: "version", type: "BIGINT"\\) \\{ + column\\(name: "title", type: "VARCHAR\\(255\\)"\\) \\{ constraints\\(nullable: "false"\\) \\} - column\\(name: "title", type: "VARCHAR\\(255\\)"\\) \\{ + column\\(name: "version", type: "BIGINT"\\) \\{ constraints\\(nullable: "false"\\) \\} @@ -85,11 +93,11 @@ databaseChangeLog = \\{ constraints\\(nullable:"false", primaryKey: "true", primaryKeyName: "bookPK"\\) \\} - column\\(name: "version", type: "BIGINT"\\) \\{ + column\\(name: "title", type: "VARCHAR\\(255\\)"\\) \\{ constraints\\(nullable: "false"\\) \\} - column\\(name: "title", type: "VARCHAR\\(255\\)"\\) \\{ + column\\(name: "version", type: "BIGINT"\\) \\{ constraints\\(nullable: "false"\\) \\} diff --git a/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/generated/GeneratedModeSnapshotSpec.groovy b/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/generated/GeneratedModeSnapshotSpec.groovy index ec32c4f3d9..08bbd05a20 100644 --- a/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/generated/GeneratedModeSnapshotSpec.groovy +++ b/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/generated/GeneratedModeSnapshotSpec.groovy @@ -41,9 +41,11 @@ import liquibase.structure.core.PrimaryKey import liquibase.structure.core.Sequence import liquibase.structure.core.Table import liquibase.structure.core.UniqueConstraint +import org.hibernate.boot.spi.MetadataImplementor import org.hibernate.dialect.H2Dialect import spock.lang.AutoCleanup import spock.lang.Specification +import spock.lang.Unroll import org.grails.orm.hibernate.HibernateDatastore import org.grails.plugins.databasemigration.liquibase.GormDatabase @@ -184,6 +186,36 @@ class GeneratedModeSnapshotSpec extends Specification { binder - generated == [] generated - binder == [] } + /** + * The order of the columns of a table in the generated change log is the order Hibernate's mapping model holds them, + * because the migration commands configure no schema action and Hibernate only reorders the columns of the model for + * one (hbm2ddl.auto / dbCreate). Hibernate's annotation binder, which native binding uses, binds the persistent + * attributes of a class sorted by name, so after the identifier the columns follow the names of the properties, the + * version among them, and the foreign keys it resolves in a second pass come last. The classic binder listed the + * identifier, the version, then the properties in declaration order. Liquibase does not compare the order of + * columns, so dbm-gorm-diff against an existing database is not affected; only a createTable change set lists them + * in the new order. + */ + @Unroll + def "without a schema action the columns of a table are in the order the binding created them (generated: #generated)"() { + when: + boot(generated, [SnapAuthor, SnapBook]) + + then: + tableColumns('snap_book') == columns + + where: + generated | columns + false | ['id', 'version', 'title', 'isbn', 'author_id'] + true | ['id', 'isbn', 'title', 'version', 'author_id'] + } + + private List<String> tableColumns(String table) { + org.hibernate.mapping.Table mapping = ((MetadataImplementor) datastore.metadata).collectTableMappings().find { + it.name.equalsIgnoreCase(table) + } + return mapping.columns*.name*.toLowerCase() + } } enum SnapStatus { OPEN, CLOSED }
