borinquenkid commented on code in PR #16539:
URL: https://github.com/apache/grails-core/pull/16539#discussion_r4213508013


##########
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:
   Done in 36ad658187: the spec now reads the foreign keys through JDBC 
`getImportedKeys` in `KEY_SEQ` order and asserts each foreign key column 
against its referenced primary key column, including `cfk_middle_name`, plus 
new same-typed, out-of-name-order parts. Removing the sort in 
`tryExpandNestedComposite` makes it fail with the zeta and alpha columns 
crossed.
   



##########
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:
   Done in 36ad658187: the ordering claim now rests on the Hibernate-backed 
spec (a real mapping with parts declared zeta, alpha, direct and nested). I 
removed the reversed array from the mock-based 
`CompositeIdentifierToManyToOneBinderSpec`, which no longer asserts ordering.
   



-- 
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]

Reply via email to