Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> 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<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<String> 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<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'
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Loading
Loading