diff --git a/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/CompositeIndexColumnOrderSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/CompositeIndexColumnOrderSpec.groovy new file mode 100644 index 00000000000..f00ac96c1a0 --- /dev/null +++ b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/CompositeIndexColumnOrderSpec.groovy @@ -0,0 +1,101 @@ +/* + * 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 grails.gorm.tests + +import grails.gorm.annotation.Entity + +import spock.lang.Issue + +@Issue('https://github.com/apache/grails-core/issues/14424') +class CompositeIndexColumnOrderSpec extends HibernateGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(OrderedIndexNft, ShuffledIndexNft) + } + + void "properties naming the same index give a composite index in declaration order"() { + when: + List columns = [] + datastore.dataSource.connection.withCloseable { conn -> + [ 'ordered_index_nft', 'ORDERED_INDEX_NFT' ].each { String table -> + conn.metaData.getIndexInfo(null, null, table, false, false).withCloseable { rs -> + List rows = [] + while (rs.next()) { + if (rs.getString('INDEX_NAME')?.equalsIgnoreCase('idx_ordered_nft')) { + rows << [rs.getInt('ORDINAL_POSITION'), rs.getString('COLUMN_NAME').toLowerCase()] + } + } + if (rows) { + columns = rows.sort { it[0] }.collect { (String) it[1] } + } + } + } + } + + then: + columns == ['token', 'address'] + } + + void "the columns follow the order the properties are declared in the class, not the order of the mapping block"() { + when: + List columns = [] + datastore.dataSource.connection.withCloseable { conn -> + [ 'shuffled_index_nft', 'SHUFFLED_INDEX_NFT' ].each { String table -> + conn.metaData.getIndexInfo(null, null, table, false, false).withCloseable { rs -> + List rows = [] + while (rs.next()) { + if (rs.getString('INDEX_NAME')?.equalsIgnoreCase('idx_shuffled_nft')) { + rows << [rs.getInt('ORDINAL_POSITION'), rs.getString('COLUMN_NAME').toLowerCase()] + } + } + if (rows) { + columns = rows.sort { it[0] }.collect { (String) it[1] } + } + } + } + } + + then: 'the mapping block names chain, then address, then token' + columns == ['token', 'address', 'chain'] + } +} + +@Entity +class OrderedIndexNft { + String token + String address + + static mapping = { + token index: 'idx_ordered_nft' + address index: 'idx_ordered_nft' + } +} + +@Entity +class ShuffledIndexNft { + String token + String address + String chain + + static mapping = { + chain index: 'idx_shuffled_nft' + address index: 'idx_shuffled_nft' + token index: 'idx_shuffled_nft' + } +} diff --git a/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/SaveWithoutValidationSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/SaveWithoutValidationSpec.groovy new file mode 100644 index 00000000000..cf7b2cda589 --- /dev/null +++ b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/SaveWithoutValidationSpec.groovy @@ -0,0 +1,59 @@ +/* + * 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 grails.gorm.tests + +import grails.gorm.annotation.Entity + +import spock.lang.Issue + +@Issue('https://github.com/apache/grails-core/issues/15276') +class SaveWithoutValidationSpec extends HibernateGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(UnvalidatedNote) + } + + void "an invalid instance saved with validate false is persisted by the flush"() { + when: + new UnvalidatedNote(title: '').save(validate: false, flush: true) + session.clear() + + then: + UnvalidatedNote.count() == 1 + + when: 'a later valid save flushes while the invalid instance is still managed' + UnvalidatedNote kept = UnvalidatedNote.list().first() + kept.body = 'edited' + new UnvalidatedNote(title: 'valid').save(failOnError: true, flush: true) + + then: + UnvalidatedNote.count() == 2 + } +} + +@Entity +class UnvalidatedNote { + String title + String body + + static constraints = { + title blank: false + body nullable: true + } +} diff --git a/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/WhereQueryRegressionSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/WhereQueryRegressionSpec.groovy new file mode 100644 index 00000000000..3a63675a1ba --- /dev/null +++ b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/WhereQueryRegressionSpec.groovy @@ -0,0 +1,117 @@ +/* + * 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 grails.gorm.tests + +import grails.gorm.annotation.Entity +import jakarta.persistence.criteria.JoinType +import spock.lang.Issue + +class WhereQueryRegressionSpec extends HibernateGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(WrPet, WrNick, WrSite, WrSeries, WrStory) + } + + private long statements() { + manager.sessionFactory.statistics.prepareStatementCount + } + + @Issue('https://github.com/apache/grails-core/issues/14425') + void "creating a detached where query that joins a collection runs no SQL"() { + given: + manager.sessionFactory.statistics.statisticsEnabled = true + long before = statements() + + when: + def query = WrPet.where { + def tags = nickNames + tags.nickname == 'Spot' + order('tags.nickname') + } + + then: + statements() == before + + when: + new WrPet(name: 'p').addToNickNames(new WrNick(nickname: 'Spot')).save(flush: true) + session.clear() + + then: 'the query still finds the pet when it is finally run' + query.list()*.name == ['p'] + } + + @Issue('https://github.com/apache/grails-core/issues/14422') + void "a left join in a nested where subquery keeps rows matched only through the primary site"() { + given: + WrSite a = new WrSite(name: 'A').save() + WrSite b = new WrSite(name: 'B').save() + WrSeries s1 = new WrSeries(name: 's1', primarySite: a).save() + WrSeries s2 = new WrSeries(name: 's2', primarySite: b) + s2.addToAdditionalSites(a) + s2.save() + new WrStory(title: 't1', series: s1).save() + new WrStory(title: 't2', series: s2).save(flush: true) + session.clear() + Long siteId = a.id + Closure seriesCriteria = { + WrSeries.where { + join('additionalSites', JoinType.LEFT) + or { + eq('primarySite.id', siteId) + additionalSites { eq('id', siteId) } + } + } + } + + expect: + seriesCriteria().id().list().size() == 2 + WrStory.where { series in seriesCriteria().id() }.id().list().size() == 2 + } +} + +@Entity +class WrPet { + String name + static hasMany = [nickNames: WrNick] +} + +@Entity +class WrNick { + String nickname + static belongsTo = [pet: WrPet] +} + +@Entity +class WrSite { + String name +} + +@Entity +class WrSeries { + String name + WrSite primarySite + static hasMany = [additionalSites: WrSite] +} + +@Entity +class WrStory { + String title + WrSeries series +} diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/WhereQueryRegressionSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/WhereQueryRegressionSpec.groovy new file mode 100644 index 00000000000..3a63675a1ba --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/WhereQueryRegressionSpec.groovy @@ -0,0 +1,117 @@ +/* + * 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 grails.gorm.tests + +import grails.gorm.annotation.Entity +import jakarta.persistence.criteria.JoinType +import spock.lang.Issue + +class WhereQueryRegressionSpec extends HibernateGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(WrPet, WrNick, WrSite, WrSeries, WrStory) + } + + private long statements() { + manager.sessionFactory.statistics.prepareStatementCount + } + + @Issue('https://github.com/apache/grails-core/issues/14425') + void "creating a detached where query that joins a collection runs no SQL"() { + given: + manager.sessionFactory.statistics.statisticsEnabled = true + long before = statements() + + when: + def query = WrPet.where { + def tags = nickNames + tags.nickname == 'Spot' + order('tags.nickname') + } + + then: + statements() == before + + when: + new WrPet(name: 'p').addToNickNames(new WrNick(nickname: 'Spot')).save(flush: true) + session.clear() + + then: 'the query still finds the pet when it is finally run' + query.list()*.name == ['p'] + } + + @Issue('https://github.com/apache/grails-core/issues/14422') + void "a left join in a nested where subquery keeps rows matched only through the primary site"() { + given: + WrSite a = new WrSite(name: 'A').save() + WrSite b = new WrSite(name: 'B').save() + WrSeries s1 = new WrSeries(name: 's1', primarySite: a).save() + WrSeries s2 = new WrSeries(name: 's2', primarySite: b) + s2.addToAdditionalSites(a) + s2.save() + new WrStory(title: 't1', series: s1).save() + new WrStory(title: 't2', series: s2).save(flush: true) + session.clear() + Long siteId = a.id + Closure seriesCriteria = { + WrSeries.where { + join('additionalSites', JoinType.LEFT) + or { + eq('primarySite.id', siteId) + additionalSites { eq('id', siteId) } + } + } + } + + expect: + seriesCriteria().id().list().size() == 2 + WrStory.where { series in seriesCriteria().id() }.id().list().size() == 2 + } +} + +@Entity +class WrPet { + String name + static hasMany = [nickNames: WrNick] +} + +@Entity +class WrNick { + String nickname + static belongsTo = [pet: WrPet] +} + +@Entity +class WrSite { + String name +} + +@Entity +class WrSeries { + String name + WrSite primarySite + static hasMany = [additionalSites: WrSite] +} + +@Entity +class WrStory { + String title + WrSeries series +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/CompositeIndexColumnOrderDialectSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/CompositeIndexColumnOrderDialectSpec.groovy new file mode 100644 index 00000000000..9d41ba17146 --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/CompositeIndexColumnOrderDialectSpec.groovy @@ -0,0 +1,106 @@ +/* + * 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 + +import grails.gorm.annotation.Entity +import grails.gorm.hibernate.HibernateEntity +import grails.gorm.tests.HibernateGormDatastoreSpec +import org.testcontainers.mariadb.MariaDBContainer +import org.testcontainers.mysql.MySQLContainer +import org.testcontainers.postgresql.PostgreSQLContainer +import org.testcontainers.spock.Testcontainers +import spock.lang.Issue +import spock.lang.Requires +import spock.lang.Shared + +/** + * Pins https://github.com/apache/grails-core/issues/14424 on every externally-run dialect (see + * {@link GormTextTypeColumnIntegrationSpec} for the same precedent): properties that name the same + * index get a composite index whose columns follow the order the properties are declared in the class, whatever order the mapping block names them in. The + * reporter used PostgreSQL and saw the columns in alphabetical order. + */ +@Issue('https://github.com/apache/grails-core/issues/14424') +@Testcontainers +@Requires({ isDockerAvailable() }) +class CompositeIndexColumnOrderDialectSpec extends HibernateGormDatastoreSpec { + + @Shared PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:16") + @Shared MySQLContainer mysql = new MySQLContainer("mysql:8.0") + @Shared MariaDBContainer mariadb = new MariaDBContainer("mariadb:10.11") + + void setupSpec() { + manager.registerDomainClasses(IndexedNft) + } + + void "a composite index lists its columns in declaration order on #db"() { + given: + if (!container.isRunning()) { + container.start() + } + manager.destroy() + manager.grailsConfig = [ + 'dataSource.url' : container.jdbcUrl, + 'dataSource.driverClassName' : container.driverClassName, + 'dataSource.username' : container.username, + 'dataSource.password' : container.password, + 'dataSource.dbCreate' : 'create-drop', + 'hibernate.hbm2ddl.auto' : 'create', + ] + manager.setup(this.class) + + when: + List columns = [] + datastore.dataSource.connection.withCloseable { conn -> + // the table name is folded to lower or upper case depending on the database + [ 'indexed_nft', 'INDEXED_NFT' ].each { String table -> + conn.metaData.getIndexInfo(null, null, table, false, false).withCloseable { rs -> + List rows = [] + while (rs.next()) { + if (rs.getString('INDEX_NAME')?.equalsIgnoreCase('idx_nft')) { + rows << [rs.getInt('ORDINAL_POSITION'), rs.getString('COLUMN_NAME').toLowerCase()] + } + } + if (rows) { + columns = rows.sort { it[0] }.collect { (String) it[1] } + } + } + } + } + + then: + columns == ['token', 'address'] + + where: + db | container + "Postgres" | postgres + "MySQL" | mysql + "MariaDB" | mariadb + } +} + +@Entity +class IndexedNft implements HibernateEntity { + String token + String address + + static mapping = { + address index: 'idx_nft' + token index: 'idx_nft' + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/CompositeIndexColumnOrderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/CompositeIndexColumnOrderSpec.groovy new file mode 100644 index 00000000000..140bcfafcda --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/CompositeIndexColumnOrderSpec.groovy @@ -0,0 +1,101 @@ +/* + * 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 + +import grails.gorm.annotation.Entity +import grails.gorm.tests.HibernateGormDatastoreSpec +import spock.lang.Issue + +@Issue('https://github.com/apache/grails-core/issues/14424') +class CompositeIndexColumnOrderSpec extends HibernateGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(OrderedIndexNft, ShuffledIndexNft) + } + + void "properties naming the same index give a composite index in declaration order"() { + when: + List columns = [] + datastore.dataSource.connection.withCloseable { conn -> + [ 'ordered_index_nft', 'ORDERED_INDEX_NFT' ].each { String table -> + conn.metaData.getIndexInfo(null, null, table, false, false).withCloseable { rs -> + List rows = [] + while (rs.next()) { + if (rs.getString('INDEX_NAME')?.equalsIgnoreCase('idx_ordered_nft')) { + rows << [rs.getInt('ORDINAL_POSITION'), rs.getString('COLUMN_NAME').toLowerCase()] + } + } + if (rows) { + columns = rows.sort { it[0] }.collect { (String) it[1] } + } + } + } + } + + then: + columns == ['token', 'address'] + } + + void "the columns follow the order the properties are declared in the class, not the order of the mapping block"() { + when: + List columns = [] + datastore.dataSource.connection.withCloseable { conn -> + [ 'shuffled_index_nft', 'SHUFFLED_INDEX_NFT' ].each { String table -> + conn.metaData.getIndexInfo(null, null, table, false, false).withCloseable { rs -> + List rows = [] + while (rs.next()) { + if (rs.getString('INDEX_NAME')?.equalsIgnoreCase('idx_shuffled_nft')) { + rows << [rs.getInt('ORDINAL_POSITION'), rs.getString('COLUMN_NAME').toLowerCase()] + } + } + if (rows) { + columns = rows.sort { it[0] }.collect { (String) it[1] } + } + } + } + } + + then: 'the mapping block names chain, then address, then token' + columns == ['token', 'address', 'chain'] + } +} + +@Entity +class OrderedIndexNft { + String token + String address + + static mapping = { + token index: 'idx_ordered_nft' + address index: 'idx_ordered_nft' + } +} + +@Entity +class ShuffledIndexNft { + String token + String address + String chain + + static mapping = { + chain index: 'idx_shuffled_nft' + address index: 'idx_shuffled_nft' + token index: 'idx_shuffled_nft' + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/SaveWithoutValidationDialectSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/SaveWithoutValidationDialectSpec.groovy new file mode 100644 index 00000000000..62f74092fbd --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/SaveWithoutValidationDialectSpec.groovy @@ -0,0 +1,101 @@ +/* + * 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 + +import grails.gorm.annotation.Entity +import grails.gorm.hibernate.HibernateEntity +import grails.gorm.tests.HibernateGormDatastoreSpec +import org.testcontainers.mariadb.MariaDBContainer +import org.testcontainers.mysql.MySQLContainer +import org.testcontainers.postgresql.PostgreSQLContainer +import org.testcontainers.spock.Testcontainers +import spock.lang.Issue +import spock.lang.Requires +import spock.lang.Shared + +/** + * Pins https://github.com/apache/grails-core/issues/15276 on every externally-run dialect (see + * {@link GormTextTypeColumnIntegrationSpec} for the same precedent): an instance saved with + * {@code validate: false} is persisted by the flush instead of being validated again, and does not + * stop a later valid save from flushing. The reporter used PostgreSQL. H2 coverage lives in + * {@link SaveWithoutValidationSpec}, which needs no container. + */ +@Issue('https://github.com/apache/grails-core/issues/15276') +@Testcontainers +@Requires({ isDockerAvailable() }) +class SaveWithoutValidationDialectSpec extends HibernateGormDatastoreSpec { + + @Shared PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:16") + @Shared MySQLContainer mysql = new MySQLContainer("mysql:8.0") + @Shared MariaDBContainer mariadb = new MariaDBContainer("mariadb:10.11") + + void setupSpec() { + manager.registerDomainClasses(UnvalidatedNote) + } + + void "an invalid instance saved with validate false is persisted on #db"() { + given: + if (!container.isRunning()) { + container.start() + } + manager.destroy() + manager.grailsConfig = [ + 'dataSource.url' : container.jdbcUrl, + 'dataSource.driverClassName' : container.driverClassName, + 'dataSource.username' : container.username, + 'dataSource.password' : container.password, + 'dataSource.dbCreate' : 'create-drop', + 'hibernate.hbm2ddl.auto' : 'create', + ] + manager.setup(this.class) + + when: + new UnvalidatedNote(title: '').save(validate: false, flush: true) + session.clear() + + then: 'the flush does not validate the instance again' + UnvalidatedNote.count() == 1 + UnvalidatedNote.list().first().title == '' + + when: 'a later valid save flushes while the invalid instance is still managed' + UnvalidatedNote kept = UnvalidatedNote.list().first() + kept.body = 'edited' + new UnvalidatedNote(title: 'valid').save(failOnError: true, flush: true) + + then: + UnvalidatedNote.count() == 2 + + where: + db | container + "Postgres" | postgres + "MySQL" | mysql + "MariaDB" | mariadb + } +} + +@Entity +class UnvalidatedNote implements HibernateEntity { + String title + String body + + static constraints = { + title blank: false + body nullable: true + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/SaveWithoutValidationSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/SaveWithoutValidationSpec.groovy new file mode 100644 index 00000000000..40d6bc21c04 --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/SaveWithoutValidationSpec.groovy @@ -0,0 +1,47 @@ +/* + * 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 + +import grails.gorm.tests.HibernateGormDatastoreSpec +import spock.lang.Issue + +@Issue('https://github.com/apache/grails-core/issues/15276') +class SaveWithoutValidationSpec extends HibernateGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(UnvalidatedNote) + } + + void "an invalid instance saved with validate false is persisted by the flush"() { + when: + new UnvalidatedNote(title: '').save(validate: false, flush: true) + session.clear() + + then: + UnvalidatedNote.count() == 1 + + when: 'a later valid save flushes while the invalid instance is still managed' + UnvalidatedNote kept = UnvalidatedNote.list().first() + kept.body = 'edited' + new UnvalidatedNote(title: 'valid').save(failOnError: true, flush: true) + + then: + UnvalidatedNote.count() == 2 + } +} diff --git a/grails-doc/src/en/ref/Database Mapping/column.adoc b/grails-doc/src/en/ref/Database Mapping/column.adoc index 8e5279127b6..38acc3efbcd 100644 --- a/grails-doc/src/en/ref/Database Mapping/column.adoc +++ b/grails-doc/src/en/ref/Database Mapping/column.adoc @@ -57,6 +57,21 @@ Arguments: * `comment` (optional) - The comment for the column (used to create the table DDL) * `defaultValue` (optional) - The database default value +Properties that name the same `index` share one composite index. The columns appear in the index in the order the properties are declared in the class, whatever order the mapping block names them in, so this declaration creates an index on `(token, address)`: + +[source,groovy] +---- +class Nft { + String token + String address + + static mapping = { + address index: 'idx_nft' + token index: 'idx_nft' + } +} +---- + By default GORM uses the property name and type to determine how to map a property in the database. For example a `String` property is typically mapped as a `varchar(255)` column. You can customize these with column configuration arguments: [source,groovy]