Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 6, 2025

Conversation

Nezteb
Copy link
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

solnic commented Apr 2, 2025

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

@josevalim
Copy link
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?

@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
@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
@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
@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
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