Repository navigation
fix(hibernate): apply maxSize to the column of a string-mapped enum property #16844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
borinquenkid
wants to merge
6
commits into
8.0.x
Choose a base branch
from
fix/enum-max-size-column-8.0.x
base: 8.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+420
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
98149b5
fix(hibernate5): apply maxSize to the column of a string-mapped enum …
borinquenkid f5ada8f
fix(hibernate7): apply maxSize to the column of a string-mapped enum …
borinquenkid 2c837ec
fix(hibernate): an enum with an inList of enum values no longer fails…
borinquenkid 354a72a
fix(hibernate7): the maxSize of a collection of enum values is not th…
borinquenkid 312255f
fix(hibernate7): the column of an enum with a maxSize holds the longe…
borinquenkid d364b7c
Merge remote-tracking branch 'origin/8.0.x' into fix/enum-max-size-co…
borinquenkid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * 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/14505') | ||
| class EnumMaxSizeColumnSpec extends HibernateGormDatastoreSpec { | ||
|
|
||
| void setupSpec() { | ||
| manager.registerDomainClasses(EnumSizedThing, EnumListedThing, EnumCollectionThing) | ||
| } | ||
|
|
||
| void "an enum property with maxSize still stores and reads its value"() { | ||
| given: | ||
| new EnumSizedThing(stringField: 'x', enumField: EnumSizedKind.TWO).save(flush: true, failOnError: true) | ||
| session.clear() | ||
|
|
||
| expect: | ||
| EnumSizedThing.findByEnumField(EnumSizedKind.TWO).enumField == EnumSizedKind.TWO | ||
| EnumSizedThing.count() == 1 | ||
| } | ||
|
|
||
| void "the string property keeps the length from maxSize"() { | ||
| when: | ||
| Map<String, Integer> lengths = [:] | ||
| manager.sessionFactory.currentSession.doWork { conn -> | ||
| def rs = conn.createStatement().executeQuery( | ||
| "select column_name, character_maximum_length from information_schema.columns " + | ||
| "where lower(table_name) = 'enum_sized_thing'") | ||
| while (rs.next()) { | ||
| lengths[rs.getString(1).toLowerCase()] = rs.getInt(2) | ||
| } | ||
| } | ||
|
|
||
| then: | ||
| lengths['string_field'] == 100 | ||
| } | ||
|
|
||
| void "an enum property with an inList of enum values stores and reads its value"() { | ||
| given: | ||
| new EnumListedThing(kind: EnumSizedKind.ONE).save(flush: true, failOnError: true) | ||
| session.clear() | ||
|
|
||
| expect: | ||
| EnumListedThing.findByKind(EnumSizedKind.ONE).kind == EnumSizedKind.ONE | ||
| EnumListedThing.count() == 1 | ||
| } | ||
|
|
||
| void "a maxSize on a collection of enum values limits the number of values and not the length of one"() { | ||
| given: 'a collection with maxSize 2 of values that are longer than two characters' | ||
| new EnumCollectionThing(kinds: [EnumLongKind.FIRST_VALUE, EnumLongKind.SECOND_VALUE]).save(flush: true, failOnError: true) | ||
| session.clear() | ||
|
|
||
| expect: 'both values are stored and read back' | ||
| EnumCollectionThing.list()[0].kinds as Set == [EnumLongKind.FIRST_VALUE, EnumLongKind.SECOND_VALUE] as Set | ||
|
|
||
| and: 'a third value is refused by the constraint of the collection' | ||
| !new EnumCollectionThing(kinds: [EnumLongKind.FIRST_VALUE, EnumLongKind.SECOND_VALUE, EnumLongKind.THIRD_VALUE]).validate() | ||
| } | ||
|
|
||
| void "an enum inList that does not allow a value rejects it"() { | ||
| expect: | ||
| !new EnumListedThing(kind: EnumSizedKind.TWO).validate() | ||
| } | ||
| } | ||
|
|
||
| enum EnumSizedKind { ONE, TWO } | ||
|
|
||
| @Entity | ||
| class EnumSizedThing { | ||
| String stringField | ||
| EnumSizedKind enumField | ||
|
|
||
| static constraints = { | ||
| stringField nullable: true, maxSize: 100 | ||
| enumField nullable: true, maxSize: 100 | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class EnumListedThing { | ||
| EnumSizedKind kind | ||
|
|
||
| static constraints = { | ||
| kind inList: [EnumSizedKind.ONE] | ||
| } | ||
| } | ||
|
|
||
| enum EnumLongKind { FIRST_VALUE, SECOND_VALUE, THIRD_VALUE } | ||
|
|
||
| @Entity | ||
| class EnumCollectionThing { | ||
| Set<EnumLongKind> kinds | ||
|
|
||
| static hasMany = [kinds: EnumLongKind] | ||
|
|
||
| static constraints = { | ||
| kinds maxSize: 2 | ||
| } | ||
| } |
93 changes: 93 additions & 0 deletions
93
...te7/core/src/test/groovy/org/grails/orm/hibernate/EnumMaxSizeColumnIntegrationSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * 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 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 | ||
|
|
||
| @Issue('https://github.com/apache/grails-core/issues/14505') | ||
| @Testcontainers | ||
| @Requires({ isDockerAvailable() }) | ||
| class EnumMaxSizeColumnIntegrationSpec 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(EnumDdlThing) | ||
| } | ||
|
|
||
| void "the enum column honours maxSize 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: | ||
| Map<String, Object> column = [:] | ||
| datastore.dataSource.connection.withCloseable { conn -> | ||
| conn.createStatement().withCloseable { stmt -> | ||
| stmt.executeQuery("select data_type, character_maximum_length from information_schema.columns " + | ||
| "where lower(table_name) = 'enum_ddl_thing' and lower(column_name) = 'enum_field'").with { rs -> | ||
| rs.next() | ||
| column = [type: rs.getString(1), maxLength: rs.getObject(2)] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| then: 'a native enum has no length, any other column is as long as maxSize says' | ||
| column.type == 'enum' || (column.maxLength as Integer) == 100 | ||
|
|
||
| where: | ||
| db | container | ||
| 'postgres' | postgres | ||
| 'mysql' | mysql | ||
| 'mariadb' | mariadb | ||
| } | ||
| } | ||
|
|
||
| enum EnumDdlKind { ONE, TWO } | ||
|
|
||
| @Entity | ||
| class EnumDdlThing { | ||
| EnumDdlKind enumField | ||
|
|
||
| static constraints = { | ||
| enumField nullable: true, maxSize: 100 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
status inList: [Status.AVAILABLE], with no maxSize. Startup throws. The allowed value is the enum, not a string. Do not read it as a string.