ash_archival/documentation/topics/upserts-and-identities.md
Zach Daniel f8e405c34f improvement: deprecate exclude_upsert_actions, as it had unintended side effects
docs: document how to handle upserts & identities with `ash_archival`
2024-08-16 11:30:57 -04:00

1.1 KiB

Upserts & Identities

Its important to consider identities when using AshArchival without a base_filter set up.

If you are using a base_filter, then all identities implicitly include that base_filter in their where (handled by the data layer).

Take the following identities, for example:

identities do
  identity :unique_email, [:email], where: expr(is_nil(archived_at))
  # and
  identity :unique_email, [:email]
end

With is_nil(archived_at)

Using this identity allows multiple archived records with the same email, but only one non-archived record per email. It enables reuse of archived email addresses for new active records, maintaining data integrity by preventing duplicate active records while preserving archived data.

When you upsert a record using this identity, it will only consider active records.

Without is_nil(archived_at)

This identity configuration enforces strict email uniqueness across all records. Once an email is used, it can't be used again, even after that record is archived.

When you upsert a record using this identity, it will consider all records.