# `AshVault.Transformers.SetupEncryption`
[🔗](https://github.com/jakeschurch/ash_vault/blob/main/lib/ash_vault/transformers/setup_encryption.ex#L1)

Replaces each encrypted attribute with a private ciphertext attribute plus a decrypt
calculation, and rewrites the actions that used to accept it.

## Ordering is load-bearing

This transformer runs **after** `Ash.Resource.Transformers.DefaultAccept`, which is what
expands `:*`/`nil` accept lists into concrete attribute names. Running before it would
make `attr.name in action.accept` false everywhere and rewrite zero actions — a silent,
total failure in which nothing is ever encrypted.

## Per field

  1. remove the plaintext attribute entity — *this* is the non-persistence mechanism.
     There is no plaintext column, so no data layer can write one. `private?` and
     scrubbing are not relied on for that.
  2. add `encrypted_<name>` as a `:binary` attribute, `public?: false`,
     `sensitive?: true`, `allow_nil?: true`
  3. add a calculation of the original name, type and constraints backed by
     `AshVault.Calculations.Decrypt`
  4. for every create/update/destroy action that accepted the attribute: add an
     argument of the original type, prepend `AshVault.Changes.Encrypt`, and drop the
     attribute from `accept`

## Per searchable field, additionally

  5. add `<name>_lookup` as a `:binary` attribute — `public?: false`, `sensitive?: true`,
     `allow_nil?: true`, `filterable?: true`, and out of the default select wherever the
     data layer can select
  6. for `unique?: true`, an identity named `<name>_lookup_unique` on `[<name>_lookup]`.
     The multitenancy attribute is deliberately NOT listed: Ash adds it itself, both to
     the generated unique index and to the eager/pre-check query, whenever
     `all_tenants?` is false. See the comment on `add_lookup_identity/3`.

     On a data layer that cannot enforce an identity natively — ETS, Mnesia — this is a
     DSL error unless the field also carries `pre_check_with:`. See
     `verify_uniqueness_enforceable!/2`.
  7. a `:by_<name>` read action taking the plaintext as a `sensitive?: true` argument,
     backed by `AshVault.Preparations.FilterByLookup`

## Deviation from ash_cloak: `allow_nil?` on the backing attribute

ash_cloak copies the original `attribute.allow_nil?` onto the ciphertext column. That is
wrong whenever `encrypt_nil?: false`, because a `nil` value then legitimately stores SQL
NULL in an `allow_nil?: false` column and the write fails. AshVault always sets the
backing attribute to `allow_nil?: true` and enforces real nullability through the
calculation's `allow_nil?` and the action argument's `allow_nil?` instead.

# `after_compile?`

# `before?`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
