Lazarus.Runtime (lazarus v1.0.0)

Copy Markdown View Source

Lower-level soft-delete operations that accept an explicit Repo module.

These functions power the Repo API injected by use Lazarus, but can also be called directly when you want Lazarus behavior without going through Repo-injected functions.

Most applications should use the functions injected into their Repo or the delegators on Lazarus. This module is mainly useful in tests, helper modules, or advanced integrations.

Common options

  • :reason - persists a deletion reason when the schema includes the field
  • :cascade - cascades through eligible associations when true
  • :reload_after_delete - reloads the soft-deleted struct from the database when true
  • :skip_associations - skips selected association fields during cascade
  • :cascade_depth - caps recursive cascade depth

Cascading requires a schema-aware query root because association metadata is read from the Ecto schema definitions. For the behavior of cascades and on_replace, see the "Cascade Soft-Deletes" and "Assoc Replace" guides.

Summary

Functions

Soft-deletes a single Ecto schema struct or changeset by setting its deleted-at field.

Soft-deletes a single struct or changeset and raises on failure.

Soft-deletes all records matching the given query.

Functions

soft_delete(repo, struct_or_changeset, opts \\ [])

Soft-deletes a single Ecto schema struct or changeset by setting its deleted-at field.

Options:

  • :reason - sets the deletion-reason field when the schema supports it; accepts a string or nil (default: nil)
  • :cascade - cascades to associations when true (default: false)
  • :reload_after_delete - loads the soft-deleted struct from the database on success (default: repo config :reload_after_delete, then false). The default in-memory return avoids an additional database call and is faster, but may be less accurate than a fresh load; associations on the returned struct are reset to Ecto.Association.NotLoaded. Loading after deletion is more accurate, but costs one additional database call.
  • :cascade_depth - max cascade depth (default: 10)
  • :skip_associations - associations to skip when cascading

Returns {:ok, struct} on success. By default Lazarus returns an updated in-memory struct with associations reset to Ecto.Association.NotLoaded; when reload_after_delete: true is enabled per-call or via repo config, it reloads the row instead. Returns {:error, :not_found} when the row is already soft-deleted or no longer exists.

soft_delete!(repo, struct, opts \\ [])

Soft-deletes a single struct or changeset and raises on failure.

Returns

  • the deleted struct on success

Options

Accepts the same options as soft_delete/3.

Raises

  • ArgumentError when the row was already soft-deleted or no longer exists

soft_delete_all(repo, queryable, opts \\ [])

Soft-deletes all records matching the given query.

Returns

  • {count, nil} when the query does not include a select
  • {count, returned} when the query includes a select, matching the usual Ecto bulk-update return shape

Options

  • :reason - string reason persisted to deletion_reason when the schema includes that field, or nil (default: nil)
  • :cascade - boolean controlling whether eligible associations are cascaded (accepted values: true, false; default: false)
  • :skip_associations - list of association field names to skip during cascading, such as [:comments, :ratings]
  • :cascade_depth - positive integer recursion cap for cascading (default: 10)
  • :allow_raw_sql - boolean controlling whether fragments are allowed in the bulk soft-delete query (accepted values: true, false; default: false)

Query Requirements

With cascade: true, the query root must be schema-aware. Schema-less and subquery roots are rejected for soft-delete writes.