Repository navigation
Add soft deletes to models - #399
Open
TharinduWijayarathna wants to merge 1 commit into
Open
TharinduWijayarathna wants to merge 1 commit into
TharinduWijayarathna wants to merge 1 commit into
Conversation
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.
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 limitsrestoreon a query to trashed rows. Please close whichever one you don't want.What changes
#[model(soft_deletes)]is opt-in, so other models behave exactly as before. It requires adeleted_atfield of anOptiontype, and a compile error says so when the field is missing or isn't anOption. It implements a newSoftDeletesmarker trait and addstrashed,restore, andforce_deleteto the model. Fields with those names are rejected only on soft deleting models.__query::<M>(), as a table-qualifiedposts.deleted_at is nullfilter. Every model query goes through__query, sofind, route model binding, relationship queries, eager loads,with_count, andhas/existsfilters all leave out trashed rows without changes to each relation type.with_trashedandonly_trashedexist only onBuilder<M: SoftDeletes>. The builder records that its first filter is the scope in a newtrashed_scopeflag, whichwith_trasheduses to remove it.readingclears the flag because it replaces the filters.query_of, whichupdate!,refresh,fresh,delete, andrestoreuse, drops the scope.deleteon a soft deleting model or its query setsdeleted_atandupdated_atto the same timestamp. A model'sdeletestill runsdeleting, dispatchesDeleted(withtrashed() == true), and also updates both fields in memory.force_deleteremoves the row, on a model (with thedeletinghook andDeleted) or on a query.restoreappliesonly_trasheditself. It reaches trashed rows without the caller removing the scope and never touchesupdated_aton rows that aren't deleted. Model and query restores run no hooks and dispatch no events.restoring/Restoredcould follow in a later PR.docs/models.mdand thesoft_deletesrow in the#[model]options table.Verification
tests/model_soft_deletes.rscovers the instance delete, restore, refresh, and force delete, plusupdated_attouching and theDeletedevent. It also covers query delete, restore, and force delete, and the relationship scope through eager loads,with_count,exists, andpost.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 on0.xwith my localsqlite3.cargo test --features database-sqlite --test http: 655 passed.soft_deletedoctests pass.cargo fmt --all -- --check,cargo clippy --workspace --all-targets --all-features -- -D warnings, andcargo check --no-default-features --all-targetsare clean.