Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: EXPOSED-403 Implement EntityClass.restriction - a means of providing soft deletes #2114

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bystam
Copy link
Contributor

@bystam bystam commented Jun 5, 2024

This is my attempt at showcasing/drafting a version of "DAO restrictions", similarly to how @SQLRestriction works in Hibernate (which can be used for soft deletes), but without the magic annotation setup.

Youtrack: https://youtrack.jetbrains.com/newIssue?project=EXPOSED&draftId=25-5413167

Usage:

object BlogPosts : LongIdTable() {
  val text = text("text")
  val isDeleted = bool("is_deleted").default(false)
}

class BlogPost(id: EntityID<Long>) : LongIdEntity(id) {
  var text: String by BlogPosts.text

  override fun delete() {
      // instead of regular hard delete, override it to simply "mark as deleted"
      BlogPosts.update({ BlogPosts.id eq id }) {
          it[isDeleted] = true
      }
  }

  companion object : LongIdEntityClass<BlogPost>(BlogPosts) {
    // this will automatically make sure only non-deleted BlogPost entities show up
    override val restriction: Op<Boolean> = BlogPosts.isDeleted eq false
  }
}

val post = BlogPost.new {
  text = "Nice blog"
}
post.delete()

BlogPost.all() // 0 elements

val actual = PostPosts.selectAll().single() // but will show up in DB

Why "restriction"?

  • It matches what the Hibernate equivalent is called
  • Anything called "base predicate" or "base filter" doesn't really capture that it will always be applied in an AND <restriction> fashion, in my mind

@bystam bystam force-pushed the feature/dao-restriction branch 2 times, most recently from 1e71160 to fe4268b Compare June 5, 2024 13:13
- A means to apply a broad filter for an entire entity/entity class
- can typically be used for soft delete scenarios
@bog-walk bog-walk self-assigned this Jun 6, 2024
@bystam
Copy link
Contributor Author

bystam commented Jun 7, 2024

I have one test failure on Sqlite, and looking more closely at it, it feels light it might be a separate SQLite-oriented bug perhaps?

The table/entity pair is declared like this - where requestId is the id column of the entity
image

I noticed when running SQL logging that the following:
image
seems to perform soft delete on the wrong ID:

SQL: INSERT INTO Requests (requestId) VALUES ('123')
SQL: UPDATE Requests SET deleted_at='2024-06-07 07:11:38.703' WHERE Requests.requestId = '1'

and indeed, the Entity instance seems to have been given the wrong ID:
image

@obabichevjb
Copy link
Collaborator

Thank you for PR, it looks interesting, I'll create an issue in YouTrack for the discussion of it,

Also thank you for describing the bug, I checked another PR and related YT issue, here (/pull/2119) is a PR that should fix it.

@bog-walk bog-walk self-requested a review June 10, 2024 13:26
@bog-walk bog-walk changed the title Implement EntityClass.restriction - a means of providing soft deletes feat: EXPOSED-403 Implement EntityClass.restriction - a means of providing soft deletes Jun 10, 2024
@bystam
Copy link
Contributor Author

bystam commented Jun 13, 2024

Thank you for PR, it looks interesting, I'll create an issue in YouTrack for the discussion of it,

I have one here: https://youtrack.jetbrains.com/issue/EXPOSED-403/DAO-soft-delete-restriction-support
Unless you meant something else :)

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.

3 participants