-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GORM should not check Entity's dirtiness on formula and transient typ…
…e of properties (#1697)
- Loading branch information
Showing
4 changed files
with
146 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,73 @@ | ||
import grails.gorm.annotation.Entity | ||
import grails.gorm.tests.GormDatastoreSpec | ||
|
||
class DirtyCheckingSpec extends GormDatastoreSpec { | ||
|
||
void "When marking whole class dirty, then derived and transient properties are still not dirty"() { | ||
when: | ||
TestBook book = new TestBook() | ||
book.title = "Test" | ||
and: "mark class as not dirty - to clear previous dirty tracking" | ||
book.trackChanges() | ||
|
||
then: | ||
!book.hasChanged() | ||
|
||
when: "Mark whole class as dirty" | ||
book.markDirty() | ||
|
||
then: "whole class is dirty" | ||
book.hasChanged() | ||
|
||
and: "The formula and transient properties are not dirty" | ||
!book.hasChanged('formulaProperty') | ||
!book.hasChanged('transientProperty') | ||
|
||
and: "Other properties are" | ||
book.hasChanged('id') | ||
book.hasChanged('title') | ||
|
||
} | ||
|
||
void "Test that dirty tracking doesn't apply on Entity's transient properties"() { | ||
when: | ||
TestBook book = new TestBook() | ||
book.title = "Test" | ||
and: "mark class as not dirty, clear previous dirty tracking" | ||
book.trackChanges() | ||
|
||
then: | ||
!book.hasChanged() | ||
|
||
when: "update transient property" | ||
book.transientProperty = "new transient value" | ||
|
||
then: "class is not dirty" | ||
!book.hasChanged() | ||
|
||
and: "transient properties are not dirty" | ||
!book.hasChanged('transientProperty') | ||
} | ||
|
||
@Override | ||
List getDomainClasses() { | ||
[TestBook] | ||
} | ||
} | ||
|
||
@Entity | ||
class TestBook implements Serializable { | ||
|
||
Long id | ||
String title | ||
|
||
String formulaProperty | ||
|
||
String transientProperty | ||
|
||
static mapping = { | ||
formulaProperty(formula: 'name || \' (formula)\'') | ||
} | ||
|
||
static transients = ['transientProperty'] | ||
} |
This file contains 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 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
61 changes: 61 additions & 0 deletions
61
...-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/GormEntityDirtyCheckable.groovy
This file contains 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,61 @@ | ||
/* | ||
* Copyright 2014 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* http: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.datastore.gorm | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Slf4j | ||
import org.grails.datastore.mapping.config.Property | ||
import org.grails.datastore.mapping.dirty.checking.DirtyCheckable | ||
import org.grails.datastore.mapping.model.PersistentEntity | ||
import org.grails.datastore.mapping.model.PersistentProperty | ||
|
||
import javax.annotation.Generated | ||
|
||
/** | ||
* | ||
* Special trait meant only for GORM entities to override default behaviour of DirtyCheckable. | ||
* Applied manually during GormEntity transformation | ||
* | ||
* @since 7.3 | ||
*/ | ||
@CompileStatic | ||
trait GormEntityDirtyCheckable extends DirtyCheckable { | ||
|
||
@Override | ||
@Generated | ||
boolean hasChanged(String propertyName) { | ||
PersistentEntity entity = currentGormInstanceApi().persistentEntity | ||
|
||
PersistentProperty persistentProperty = entity.getPropertyByName(propertyName) | ||
if (!persistentProperty) { | ||
// Not persistent property, transient. We don't track changes for transients | ||
return false | ||
} | ||
|
||
Property propertyMapping = persistentProperty.getMapping().getMappedForm() | ||
if (propertyMapping.derived) { | ||
// Derived property cannot be changed, ex. sql formula | ||
return false | ||
} | ||
|
||
return super.hasChanged(propertyName) | ||
} | ||
|
||
@Generated | ||
private GormInstanceApi currentGormInstanceApi() { | ||
(GormInstanceApi) GormEnhancer.findInstanceApi(getClass()) | ||
} | ||
} |