Skip to content

Support a deletedAt timestamp as the logical delete marker - #17

Merged
matrei merged 2 commits into
4.0.xfrom
feat/16-logical-delete-timestamp
Oct 6, 2026
Merged

matrei merged 2 commits into
4.0.xfrom
feat/16-logical-delete-timestamp

Conversation

@matrei

@matrei matrei commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Closes #16

Adds a LogicalDeleteTimestamp trait, which records when an entity was logically deleted, in a nullable deletedAt timestamp, instead of the boolean deleted flag.

class Customer implements LogicalDeleteTimestamp<Customer> {
    String name
}
  • delete() sets deletedAt to the current time, undelete() clears it, and delete(hard: true) still removes the row.
  • Records with deletedAt set are excluded from get/read/load/proxy, list/findAll, dynamic finders, where queries and criteria (including projections).
  • withDeleted { } and @WithDeleted, including on Data Service methods, include them.
  • deletedAt can be mapped like any other property, e.g. deletedAt(column: 'removed_at').

The existing LogicalDelete trait is unchanged.

Changes

  • LogicalDeleteTimestamp: new trait mirroring LogicalDelete, with isNull('deletedAt') in place of deleted == false.
  • PreQueryListener: picks the "not deleted" condition per trait (deleted = false or deletedAt IS NULL).
  • Example app: a Customer domain class, CustomersDataService and LogicalDeleteTimestampSpec. The spec runs against both GORM for Hibernate 5 (app1) and Hibernate 7 (app1-hibernate7).
  • Docs: new "Recording When an Entity Was Deleted" section.

Decisions on the open questions in #16

  • Naming: LogicalDeleteTimestamp / deletedAt.
  • Type: java.time.Instant, as an unambiguous point in time. Not configurable through a type parameter.
  • Generalization: limited to timestamps. A general "nullable marker" trait can be added later if needed.
  • Clock: Instant.now(). No configurable Clock bean.

Notes

  • Persistent properties are nullable by default in Grails 8, so deletedAt needs no constraint. Apps that make properties non-nullable by default need deletedAt(nullable: true), which the docs mention. A trait cannot contribute constraints itself.
  • The trait repeats a few small helpers from LogicalDelete (withDeleted, the flag accessors, the static API lookup), to leave LogicalDelete untouched. They could move to a shared internal class later.

Add the LogicalDeleteTimestamp trait, which records when an entity was
logically deleted in a nullable deletedAt Instant instead of a boolean flag.

- delete() sets deletedAt to the current time, undelete() clears it and
  delete(hard: true) still removes the entity
- PreQueryListener excludes entities with deletedAt set from query results,
  and withDeleted and @WithDeleted include them, as for LogicalDelete
- Add a Customer example with integration tests, run against both GORM for
  Hibernate 5 and Hibernate 7, and document the trait

Closes #16
@matrei
matrei requested review from sbglasius and a balanced review from Copilot October 5, 2026 16:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

No blocking issues were identified, and integration coverage addresses the main behaviors, though tests were not executed during review.

Review effort: Balanced
Findings: None

What changed in this PR

Adds timestamp-based logical deletion alongside the existing boolean-based LogicalDelete trait, addressing #16 without changing its behavior.

Changes:

  • Adds LogicalDeleteTimestamp with a nullable Instant deletedAt marker.
  • Extends query filtering to exclude timestamp-deleted records.
  • Adds documentation, examples, and integration coverage.
File Description
plugin/​src/​main/​groovy/​grails/​logical/​delete/​PreQueryListener.groovy Selects filtering criteria for either deletion marker.
plugin/​src/​main/​groovy/​grails/​logical/​delete/​LogicalDeleteTimestamp.groovy Implements timestamp deletion, restoration, and filtered queries.
plugin/​src/​docs/​index.adoc Documents timestamp usage and nullability requirements.
examples/​app1/​src/​integration-test/​groovy/​grails/​logical/​delete/​LogicalDeleteTimestampSpec.groovy Covers deletion, filtering, restoration, mapping, and Data Services.
examples/​app1/​grails-app/​services/​app/​CustomersDataService.groovy Demonstrates Data Service queries with @WithDeleted.
examples/​app1/​grails-app/​domain/​app/​Customer.groovy Demonstrates the trait and custom column mapping.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@sbglasius

Copy link
Copy Markdown
Contributor

Review finding (low confidence) — plugin/src/main/groovy/grails/logical/delete/LogicalDeleteTimestamp.groovy (delete() / delete(Map))

delete() unconditionally overwrites deletedAt, even if the entity is already logically deleted. If an entity is soft-deleted at T1, loaded via withDeleted { Customer.get(id) }, and delete() is called again, deletedAt becomes T2 and the original deletion time is lost. With the boolean marker this was idempotent, but with a timestamp it is not. Consider only setting deletedAt when it is currently null.

Deleting an entity that is already logically deleted no longer overwrites
deletedAt, so the original deletion time is not lost.
@matrei

matrei commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Good catch, thanks! With a timestamp, overwriting the original deletion time on a repeated delete() loses information that the boolean flag never had, so delete() and delete(Map) now only set deletedAt when it is null. Deleting an already deleted entity keeps its original deletion time, while undelete() followed by delete() still records a new one.

Added a test for it and a note in the docs in d51395e.

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed. The earlier finding (repeated delete() overwriting deletedAt) is fixed in d51395e, with a test and docs. No further issues found.

@matrei matrei changed the title feat: support a deletedAt timestamp as the logical delete marker Support a deletedAt timestamp as the logical delete marker Oct 6, 2026
@matrei
matrei merged commit a981a5a into 4.0.x Oct 6, 2026
2 checks passed
@matrei
matrei deleted the feat/16-logical-delete-timestamp branch October 6, 2026 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support a deletedAt timestamp as the logical delete marker

3 participants