Skip to content

Conversation

codeconsole
Copy link
Contributor

@codeconsole codeconsole commented Oct 8, 2025

This really needs to go out in 7.0.0 as it is currently completely broken

This is the isolated part of PR #15118 that fixes #15120

All it does is mark properties annotated with @AutoTimestamp dirty so they are guaranteed to be saved during updates.

To see existing non behavior:

git clone --depth 1 --single-branch https://github.com/codeconsole/mongo-test mongo-test-RC2 && cd mongo-test-RC2 && ./gradlew bootRun

Create and Edit to see modified not updated after edit save.
http://localhost:8080/user/create
http://localhost:8080/user/edit/1

The checkout grails-core fix branch and publish it to mavenLocal

git clone --depth 1 --branch 7.0.x-autotimestamp-mongo-fix https://github.com/codeconsole/grails-core grails-core-autotimestamp-mongo-fix && cd grails-core-autotimestamp-mongo-fix && ./gradlew pTML

You can verify fix on MongoDb

git clone --depth 1 --branch mongodb-mavenLocal-7.0.0-SNAPSHOT https://github.com/codeconsole/mongo-test mongo-test-mongo-SNAPSHOT && cd mongo-test-mongo-SNAPSHOT && ./gradlew bootRun --args='--server.port=8081'

http://localhost:8081/user/create
http://localhost:8081/user/edit/1

You can verify fix also works on Hibernate

git clone --depth 1 --branch h2-mavenLocal-7.0.0-SNAPSHOT https://github.com/codeconsole/mongo-test mongo-test-hibernate-SNAPSHOT && cd mongo-test-hibernate-SNAPSHOT && ./gradlew bootRun --args='--server.port=8082'

http://localhost:8082/user/create
http://localhost:8082/user/edit/1

@github-actions github-actions bot added the bug label Oct 8, 2025
@codeconsole codeconsole force-pushed the 7.0.x-autotimestamp-mongo-fix branch from 9391558 to bc1808a Compare October 8, 2025 18:33
@codeconsole codeconsole changed the title 7.0.0-RC2 - @Autotimestamp Persistence Bug Fix 7.0.0-RC2 BUG FIX - @Autotimestamp Mongo Persistence Oct 8, 2025
@codeconsole
Copy link
Contributor Author

codeconsole commented Oct 9, 2025

In Summary

All this does is make @AutoTimestamp properties dirty if there are changes so they will be persisted. It doesn't affect anything else. You can verify everything working on both hibernate and mongo in just 3 commands.

Publish fix to mavenLocal()

git clone --depth 1 --branch 7.0.x-autotimestamp-mongo-fix https://github.com/codeconsole/grails-core grails-core-autotimestamp-mongo-fix && cd grails-core-autotimestamp-mongo-fix && ./gradlew pTML

Run fix on port 8081 for mongo and 8082 for hibernate in 2 command

git clone --depth 1 --branch mongodb-mavenLocal-7.0.0-SNAPSHOT https://github.com/codeconsole/mongo-test mongo-test-mongo-SNAPSHOT && cd mongo-test-mongo-SNAPSHOT && ./gradlew bootRun --args='--server.port=8081'

git clone --depth 1 --branch h2-mavenLocal-7.0.0-SNAPSHOT https://github.com/codeconsole/mongo-test mongo-test-hibernate-SNAPSHOT && cd mongo-test-hibernate-SNAPSHOT && ./gradlew bootRun --args='--server.port=8082'

http://localhost:8081/user/create
http://localhost:8082/user/create

@codeconsole codeconsole changed the title 7.0.0-RC2 BUG FIX - @Autotimestamp Mongo Persistence 7.0.0-RC2 BUG FIX - @AutoTimestamp Mongo Persistence Oct 10, 2025
}
else {
// schedule lastUpdated if necessary
if (entity.getPropertyByName(GormProperties.LAST_UPDATED) != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you just trying to minimize updates by removing this statement?

Copy link
Contributor Author

@codeconsole codeconsole Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdaugherty yes, updates should be uniform in 1 location.

Set<String> props = getLastUpdatedPropertyNames(entity.getName());
if (props != null) {
Object entityObject = ea.getEntity();
boolean isDirtyCheckable = entityObject instanceof DirtyCheckable;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instanceof is incompatible with Hibernate Proxies

Class<?> lastUpdateType = ea.getPropertyType(prop);
Object timestamp = timestampProvider.createTimestamp(lastUpdateType);
ea.setProperty(prop, timestamp);
// Mark property as dirty for datastores that use dirty checking (e.g., MongoDB)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this is a mongo specific, but hibernate does have dirty checking and works. What makes hibernate different here?

storeTimestampAvailability(entitiesWithLastUpdated, persistentEntity, property);
} else {
storeTimestampAvailability(entitiesWithDateCreated, persistentEntity, property);
Field field = ReflectionUtils.findField(persistentEntity.getJavaClass(), property.getName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're trying to target a bug fix, why switch to a different method of resolution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdaugherty because it is does exactly same thing.

@matrei
Copy link
Contributor

matrei commented Oct 14, 2025

I think this is a possible candidate for 7.0.1.
We need to ship 7.0.0 to lay some stable ground so we can continue moving from there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7.0.0-RC2: @AutoTimestamp and lastUpdated not persisting correctly with Mongo DB

3 participants