feature - add @DatabaseCleanup test annoation to facilicate cleaning up after a test or tests#15436
Merged
jdaugherty merged 22 commits into7.0.xfrom Feb 25, 2026
Merged
feature - add @DatabaseCleanup test annoation to facilicate cleaning up after a test or tests#15436jdaugherty merged 22 commits into7.0.xfrom
@DatabaseCleanup test annoation to facilicate cleaning up after a test or tests#15436jdaugherty merged 22 commits into7.0.xfrom
Conversation
730122d to
47dfa5f
Compare
bb469e5 to
6e7494d
Compare
Contributor
Author
|
The AI didn't do a great job on the postgres implementation. I've cleaned it up a lot and I think this should all be working / passing now. It should be ready for review. |
...ort-cleanup-h2/src/main/groovy/org/apache/grails/testing/cleanup/h2/H2DatabaseCleaner.groovy
Outdated
Show resolved
Hide resolved
...ain/groovy/org/apache/grails/testing/cleanup/postgresql/PostgresDatabaseCleanupHelper.groovy
Outdated
Show resolved
Hide resolved
|
Yes, that's a valid concern. The query filters tables based on grails-testing-support-cleanup-h2/src/main/groovy/org/apache/grails/testing/cleanup/h2/H2DatabaseCleaner.groovy |
...anup-core/src/main/groovy/org/apache/grails/testing/cleanup/core/DatabaseCleanupStats.groovy
Outdated
Show resolved
Hide resolved
jamesfredley
approved these changes
Feb 24, 2026
Database cleanup feature feedback
matrei
approved these changes
Feb 25, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
After reviewing several of the grails-data PRs submitted by @jamesfredley I realized that a pattern we implemented in our Grails app would be beneficial for everyone: rather than use
@Rollback, etc we use a clean up process that truncates all tables with data. I've extracted our internal logic into a generic spock extension & then enhanced it with the assistance of AI to be a little more generic. Only H2 & Postgres (AI did this one, while the H2 one was lifted from our internal implementation) are implemented for now.There aren't really many dependencies with this feature. I intentionally did not depend on many grails projects to ensure it can be used to test in grails-core too. The spock extension requires
@SpringBootTestto use theDefaultApplicationContextResolverto find the application context - this is added automatically by using@Integration. We could relax this requirement if a custom resolver is provided instead.The annotation can be placed at the class level to clear all data after each test, or individually on each method - only the methods annotated will truncate the database after running. It should be rather performant since it issues truncates instead of iterating over every row (FK's etc are disabled during the cleanup process too). There's also some optional debug that can be turned on.
I'm adding this to 7.0.x since it doesn't impact any of the existing code & we can then make use of it in the various grails-data tests to be DRY. Simply annotate the test, and the database is always clean now at the start of every test - regardless of new transaction testing, etc. This also means with the combination of
@Stepwiseyou can now test multi-transaction processes and roll back the data reliably.Assisted-by: OpenCode <opencode@opencode.ai>
Assisted-by: Claude <claude@anthropic.com>