Skip to content

Add @schema_redact: :all_except_primary_keys module attribute to Ecto Schema#4599

Merged
josevalim merged 4 commits intoelixir-ecto:masterfrom
Nezteb:redact-entire-schema
Apr 6, 2025
Merged

Add @schema_redact: :all_except_primary_keys module attribute to Ecto Schema#4599
josevalim merged 4 commits intoelixir-ecto:masterfrom
Nezteb:redact-entire-schema

Conversation

@Nezteb
Copy link
Copy Markdown
Contributor

@Nezteb Nezteb commented Apr 2, 2025

Initial proposal: https://groups.google.com/g/elixir-ecto/c/kQJN-7HPn6E

@josevalim initially approved of the second of my two proposed options:

@schema_redact :all_except_primary_keys
schema "users" do
  # ...
end

While implementing, I realized that it's probably not best to redact all fields. I opted to keep the primary keys of a schema even if @schema_redact is set to something like :all, hence the rename to :all_except_primary_keys.

TODO:

  • Document new property once implementation/naming is settled

Future ideas:

  • Actually add @schema_redact: :all, but would that be useful?
  • Add separate attributes only deriving inspect for a list of specific fields?

@solnic
Copy link
Copy Markdown

solnic commented Apr 2, 2025

Fantastic! PKs by default sounds very reasonable. Thank you for working on this 💜

@josevalim
Copy link
Copy Markdown
Member

Hi @Nezteb, in this case, I would call it :all_except_primary_keys.

Btw, implementation wise, I'd implement it here:

https://github.com/elixir-ecto/ecto/blob/master/lib/ecto/schema.ex#L1995-L1997

So it does something like this:

Keyword.get(opts, :redact, fn -> compute_default_redact_from_module_attribute(...) end)

This way, you don't need to change changesets nor nothing else, we reuse the existing redact infrastructure. WDYT?

Comment thread lib/ecto/changeset.ex Outdated
Comment thread lib/ecto/schema.ex Outdated
@Nezteb Nezteb changed the title Add @redact: :all module attribute to Ecto Schema Add @redact: :all_except_primary_keys module attribute to Ecto Schema Apr 3, 2025
Comment thread lib/ecto/schema.ex Outdated
@Nezteb Nezteb force-pushed the redact-entire-schema branch from b3638a7 to 6229c00 Compare April 3, 2025 15:10
@Nezteb Nezteb force-pushed the redact-entire-schema branch from 88b45ee to d2642e8 Compare April 3, 2025 15:14
Comment thread lib/ecto/schema.ex Outdated
@Nezteb Nezteb changed the title Add @redact: :all_except_primary_keys module attribute to Ecto Schema Add @schema_redact: :all_except_primary_keys module attribute to Ecto Schema Apr 3, 2025
Comment thread lib/ecto/schema.ex Outdated
Comment thread lib/ecto/schema.ex Outdated
Comment thread lib/ecto/schema.ex Outdated
@Nezteb Nezteb requested a review from josevalim April 6, 2025 18:23
@josevalim josevalim merged commit 4f19ff0 into elixir-ecto:master Apr 6, 2025
7 checks passed
@josevalim
Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

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.

3 participants