jamesfredley commented on code in PR #16539: URL: https://github.com/apache/grails-core/pull/16539#discussion_r4210684577
########## grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CompositeForeignKeyColumnTypesSpec.groovy: ########## @@ -0,0 +1,179 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.grails.orm.hibernate.cfg.domainbinding.binder + +import java.sql.Connection + +import grails.gorm.annotation.Entity +import grails.gorm.hibernate.mapping.MappingBuilder +import grails.gorm.transactions.Transactional +import org.grails.orm.hibernate.HibernateDatastore +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +/** + * A foreign key to an entity whose composite identifier has parts of different types must name + * and type each column after the referenced primary key column it points at. + */ +class CompositeForeignKeyColumnTypesSpec extends Specification { + + @Shared + @AutoCleanup + HibernateDatastore datastore = new HibernateDatastore( + [ + 'dataSource.url' : 'jdbc:h2:mem:compositeFkTypesDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto': 'create', + ], + CfkParent, CfkChild, CfkGrandParent, CfkMiddle, CfkLeaf) + + private List<List> columns(String table) { + List<List> rows = [] + datastore.sessionFactory.openSession().withCloseable { session -> + session.doWork { Connection c -> + c.createStatement().withCloseable { st -> + st.executeQuery( + "select column_name, data_type from information_schema.columns where table_name = '${table}' order by ordinal_position".toString()) + .withCloseable { rs -> + while (rs.next()) { + rows << [rs.getString(1).toLowerCase(), rs.getString(2).toUpperCase()] + } + } + } + } + } + rows + } + + void "foreign key columns carry the type of the primary key column they are named after"() { + when: + Map<String, String> parent = columns('CFK_PARENT').collectEntries { [(it[0]): it[1]] } + Map<String, String> child = columns('CFK_CHILD').collectEntries { [(it[0]): it[1]] } + + then: + parent.name == 'CHARACTER VARYING' + parent.lucky_number == 'INTEGER' + child.cfk_parent_name == parent.name + child.cfk_parent_lucky_number == parent.lucky_number + } + + void "foreign key columns of a three level composite chain are named and typed after the referenced key"() { + when: + Map<String, String> grand = columns('CFK_GRAND_PARENT').collectEntries { [(it[0]): it[1]] } Review Comment: `columns()` reads `ordinal_position`, then `collectEntries` throws that order away. The assertions check each column's name and type. That catches an integer paired with a string. It does not catch two same-type key parts written in the wrong order, which is the bug this change fixes. Please assert the foreign key's column pairs in `KEY_SEQ` order, including `cfk_middle_name`. Table-column ordinal order is not the key order. ########## grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CompositeIdentifierToManyToOneBinderSpec.groovy: ########## @@ -83,7 +83,10 @@ class CompositeIdentifierToManyToOneBinderSpec extends Specification { def nestedPartA = Mock(HibernatePersistentProperty) def nestedPartB = Mock(HibernatePersistentProperty) - def perArray = [nestedPartA, nestedPartB] as HibernatePersistentProperty[] + nestedPartA.name >> "partA" + nestedPartB.name >> "partB" + // mapped in the reverse of the name order: the columns must still follow the sorted order of the referenced key + def perArray = [nestedPartB, nestedPartA] as HibernatePersistentProperty[] Review Comment: Reversing this array is the ordering case, but the spec still calls `bindCompositeIdentifierToManyToOne()` directly and stubs `sortOrIndexForeignKeyColumns` and `getReferencedIdentifierColumns`. Those stubs are the behavior under test, so the column-name checks below do not show that a real mapping sorts the referenced key. Please cover the reversed declaration order with a Hibernate-backed spec and the ordered foreign-key metadata assertion, instead of this mock. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
