jamesfredley commented on code in PR #15393: URL: https://github.com/apache/grails-core/pull/15393#discussion_r2824333116
########## grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/DataServiceMultiTenantMultiDataSourceSpec.groovy: ########## @@ -0,0 +1,274 @@ +/* + * 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.connections + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.services.Service +import grails.gorm.transactions.Transactional +import org.grails.datastore.gorm.GormEnhancer +import org.grails.datastore.gorm.GormEntity +import org.grails.datastore.gorm.GormStaticApi +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.multitenancy.MultiTenancySettings +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver +import org.grails.orm.hibernate.HibernateDatastore +import org.hibernate.Session +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +/** + * Tests GORM Data Service auto-implemented CRUD methods when both DISCRIMINATOR + * multi-tenancy and a non-default datasource are configured on the same domain. + * + * This combination triggers the allQualifiers() bug: when MultiTenant is present, + * allQualifiers() returns tenant IDs instead of datasource names, causing schema + * creation and query routing to go to the wrong database. + * + * Covers: + * - Schema creation on the correct (analytics) datasource for MultiTenant domains + * - save(), get(), delete(), count() with tenant isolation on secondary datasource + * - findBy* dynamic finders with tenant isolation on secondary datasource + * - GormEnhancer escape-hatch for aggregate HQL on secondary datasource + * - Tenant isolation: same-named data under different tenants stays separate + * + * @see PartitionedMultiTenancySpec for basic DISCRIMINATOR multi-tenancy + * @see DataServiceMultiDataSourceSpec for Data Services on secondary datasource without multi-tenancy + */ +class DataServiceMultiTenantMultiDataSourceSpec extends Specification { + + @Shared @AutoCleanup HibernateDatastore datastore + + void setupSpec() { + Map config = [ + "grails.gorm.multiTenancy.mode": MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR, + "grails.gorm.multiTenancy.tenantResolverClass": SystemPropertyTenantResolver, + 'dataSource.url': "jdbc:h2:mem:grailsDB;LOCK_TIMEOUT=10000", + 'dataSource.dbCreate': 'create-drop', + 'dataSource.dialect': H2Dialect.name, + 'dataSource.formatSql': 'true', + 'hibernate.flush.mode': 'COMMIT', + 'hibernate.cache.queries': 'true', + 'hibernate.hbm2ddl.auto': 'create-drop', + 'dataSources.analytics': [url: "jdbc:h2:mem:analyticsDB;LOCK_TIMEOUT=10000"], + ] + + datastore = new HibernateDatastore( + DatastoreUtils.createPropertyResolver(config), Metric) + } + + MetricService metricService + + void setup() { + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "tenant1") + metricService = datastore.getDatastoreForConnection("analytics").getService(MetricService) + metricService.deleteAll() Review Comment: No change needed. The unqualified path is already exercised by the `GormEnhancer.findStaticApi(Metric)` test at line 197, which verifies the API registration resolves correctly without specifying a connection. The qualifier resolution logic itself is covered in isolation by `GormEnhancerAllQualifiersSpec` with 7 test cases covering all MultiTenant/non-MultiTenant + explicit/default/ALL permutations. The integration test intentionally uses the explicit service path (`@Transactional(connection = "analytics")`) because that is the canonical usage pattern for Data Services targeting a secondary datasource. ########## grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/DataServiceMultiTenantMultiDataSourceSpec.groovy: ########## @@ -0,0 +1,274 @@ +/* + * 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.connections + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.services.Service +import grails.gorm.transactions.Transactional +import org.grails.datastore.gorm.GormEnhancer +import org.grails.datastore.gorm.GormEntity +import org.grails.datastore.gorm.GormStaticApi +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.multitenancy.MultiTenancySettings +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver +import org.grails.orm.hibernate.HibernateDatastore +import org.hibernate.Session +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +/** + * Tests GORM Data Service auto-implemented CRUD methods when both DISCRIMINATOR + * multi-tenancy and a non-default datasource are configured on the same domain. + * + * This combination triggers the allQualifiers() bug: when MultiTenant is present, + * allQualifiers() returns tenant IDs instead of datasource names, causing schema + * creation and query routing to go to the wrong database. + * + * Covers: + * - Schema creation on the correct (analytics) datasource for MultiTenant domains + * - save(), get(), delete(), count() with tenant isolation on secondary datasource + * - findBy* dynamic finders with tenant isolation on secondary datasource + * - GormEnhancer escape-hatch for aggregate HQL on secondary datasource + * - Tenant isolation: same-named data under different tenants stays separate + * + * @see PartitionedMultiTenancySpec for basic DISCRIMINATOR multi-tenancy + * @see DataServiceMultiDataSourceSpec for Data Services on secondary datasource without multi-tenancy + */ +class DataServiceMultiTenantMultiDataSourceSpec extends Specification { + + @Shared @AutoCleanup HibernateDatastore datastore + + void setupSpec() { + Map config = [ + "grails.gorm.multiTenancy.mode": MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR, + "grails.gorm.multiTenancy.tenantResolverClass": SystemPropertyTenantResolver, + 'dataSource.url': "jdbc:h2:mem:grailsDB;LOCK_TIMEOUT=10000", + 'dataSource.dbCreate': 'create-drop', + 'dataSource.dialect': H2Dialect.name, + 'dataSource.formatSql': 'true', + 'hibernate.flush.mode': 'COMMIT', + 'hibernate.cache.queries': 'true', + 'hibernate.hbm2ddl.auto': 'create-drop', + 'dataSources.analytics': [url: "jdbc:h2:mem:analyticsDB;LOCK_TIMEOUT=10000"], + ] + + datastore = new HibernateDatastore( + DatastoreUtils.createPropertyResolver(config), Metric) + } + + MetricService metricService + + void setup() { + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "tenant1") + metricService = datastore.getDatastoreForConnection("analytics").getService(MetricService) + metricService.deleteAll() + // Also clean tenant2 data + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "tenant2") + metricService.deleteAll() + // Reset to tenant1 for tests + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "tenant1") + } + + void cleanup() { + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "") + } + + void "schema is created on analytics datasource"() { + expect: "The analytics datasource connects to the analyticsDB H2 database" + Metric.analytics.withNewSession { Session s -> + assert s.connection().metaData.getURL() == "jdbc:h2:mem:analyticsDB" + return true + } + + and: "The default datasource connects to a different database" + datastore.withNewSession { Session s -> + assert s.connection().metaData.getURL() == "jdbc:h2:mem:grailsDB" + return true + } + } + + void "save routes to analytics datasource with tenant isolation"() { + when: "A metric is saved under tenant1" + Metric saved = metricService.save(new Metric(name: "page_views", amount: 100)) + + then: "The metric is persisted with an ID" + saved != null + saved.id != null + saved.name == "page_views" + saved.amount == 100 Review Comment: The test already asserts `Metric.analytics.get(saved.id) != null` in the `and:` block at lines 119-122, verifying the data exists on the analytics datasource. The suggested negative assertion `Metric.get(saved.id) == null` would be fragile - with the fix applied, the Metric table should not exist on the default datasource (it is mapped to `datasource 'analytics'` only), so `Metric.get()` against the default connection would throw a table-not-found exception rather than returning null. ########## grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/DataServiceMultiTenantMultiDataSourceSpec.groovy: ########## @@ -0,0 +1,274 @@ +/* + * 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.connections + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.services.Service +import grails.gorm.transactions.Transactional +import org.grails.datastore.gorm.GormEnhancer +import org.grails.datastore.gorm.GormEntity +import org.grails.datastore.gorm.GormStaticApi +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.multitenancy.MultiTenancySettings +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver +import org.grails.orm.hibernate.HibernateDatastore +import org.hibernate.Session +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +/** + * Tests GORM Data Service auto-implemented CRUD methods when both DISCRIMINATOR + * multi-tenancy and a non-default datasource are configured on the same domain. + * + * This combination triggers the allQualifiers() bug: when MultiTenant is present, + * allQualifiers() returns tenant IDs instead of datasource names, causing schema + * creation and query routing to go to the wrong database. + * + * Covers: + * - Schema creation on the correct (analytics) datasource for MultiTenant domains + * - save(), get(), delete(), count() with tenant isolation on secondary datasource + * - findBy* dynamic finders with tenant isolation on secondary datasource + * - GormEnhancer escape-hatch for aggregate HQL on secondary datasource + * - Tenant isolation: same-named data under different tenants stays separate + * + * @see PartitionedMultiTenancySpec for basic DISCRIMINATOR multi-tenancy + * @see DataServiceMultiDataSourceSpec for Data Services on secondary datasource without multi-tenancy Review Comment: No change needed. The class-level Javadoc does not reference `DataServiceMultiDataSourceSpec`. The actual `@see` references are: - `@see PartitionedMultiTenancySpec` (line 53) - exists at `grails-data-hibernate5/core/src/test/groovy/.../connections/PartitionedMultiTenancySpec.groovy` - `@see MultipleDataSourceConnectionsSpec` (line 54) - exists at `grails-data-hibernate5/core/src/test/groovy/.../connections/MultipleDataSourceConnectionsSpec.groovy` Both are in the same test package and are valid references. -- 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]
