Skip to content

Add soft deletes to models - #399

Open
TharinduWijayarathna wants to merge 1 commit into
laravel:0.xfrom
TharinduWijayarathna:soft-deletes
Open

TharinduWijayarathna wants to merge 1 commit into
laravel:0.xfrom
TharinduWijayarathna:soft-deletes

Conversation

@TharinduWijayarathna

Copy link
Copy Markdown

Adds opt-in soft deletes to laravel::model, one of the later slices that #217 mentions. #387 covers the same feature with a similar design. This one keeps the change smaller (11 files) and limits restore on a query to trashed rows. Please close whichever one you don't want.

#[model(soft_deletes)]
pub struct Post {
    pub id: i64,
    pub title: String,
    pub deleted_at: Option<Timestamp>,
}

post.delete().await?;                         // sets `deleted_at` and `updated_at`
Post::query().get().await?;                   // leaves out soft deleted rows
Post::query().with_trashed().get().await?;    // includes them
Post::query().only_trashed().get().await?;    // only them

let mut post = Post::query().only_trashed().find_or_fail(id).await?;
post.trashed();                               // true
post.restore().await?;                        // clears `deleted_at`
post.force_delete().await?;                   // removes the row

What changes

  • #[model(soft_deletes)] is opt-in, so other models behave exactly as before. It requires a deleted_at field of an Option type, and a compile error says so when the field is missing or isn't an Option. It implements a new SoftDeletes marker trait and adds trashed, restore, and force_delete to the model. Fields with those names are rejected only on soft deleting models.
  • The scope is added once, in __query::<M>(), as a table-qualified posts.deleted_at is null filter. Every model query goes through __query, so find, route model binding, relationship queries, eager loads, with_count, and has/exists filters all leave out trashed rows without changes to each relation type.
  • with_trashed and only_trashed exist only on Builder<M: SoftDeletes>. The builder records that its first filter is the scope in a new trashed_scope flag, which with_trashed uses to remove it. reading clears the flag because it replaces the filters.
  • A model's own writes and reloads reach its row. query_of, which update!, refresh, fresh, delete, and restore use, drops the scope.
  • delete on a soft deleting model or its query sets deleted_at and updated_at to the same timestamp. A model's delete still runs deleting, dispatches Deleted (with trashed() == true), and also updates both fields in memory.
  • force_delete removes the row, on a model (with the deleting hook and Deleted) or on a query.
  • A query's restore applies only_trashed itself. It reaches trashed rows without the caller removing the scope and never touches updated_at on rows that aren't deleted. Model and query restores run no hooks and dispatch no events. restoring/Restored could follow in a later PR.
  • Docs: a "Soft deleting" section in docs/models.md and the soft_deletes row in the #[model] options table.

Verification

  • New tests/model_soft_deletes.rs covers the instance delete, restore, refresh, and force delete, plus updated_at touching and the Deleted event. It also covers query delete, restore, and force delete, and the relationship scope through eager loads, with_count, exists, and post.comments().with_trashed().
  • cargo test --features database-sqlite --test database: 362 passed. The one failure, schema_dump_writes_the_sqlite_schema_with_the_migrations_rows, also fails on 0.x with my local sqlite3.
  • cargo test --features database-sqlite --test http: 655 passed.
  • The model doctests and the new soft_delete doctests pass.
  • cargo fmt --all -- --check, cargo clippy --workspace --all-targets --all-features -- -D warnings, and cargo check --no-default-features --all-targets are clean.

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.

1 participant