ecto schema created_by and last_modified_by

Hi,

I have a requirement to track the user who created the record and the user who last updated the record for audit purposes.

I have added two fields to the schema (created_by and last_modified_by).

I was checking the timestamps() code in ecto git repository to understand how I could update the values in these attributes where I noticed two module attributes (ecto_autogenerate and ecto_autoupdate).

My understanding is that ecto_autogenerate is triggered when autogenerate is set to true or a {module, function, attributes} tuple is set for the field in schema definition.

Also, another thought was autogenerate will be triggered at insert while autoupdate shall be trigged at update, However, I could not find any documenration on ecto_autoupdate except for these three lines of code in schema.ex

find . -name “*.ex” -print | xargs grep -in ecto_autoupdate
./lib/ecto/schema.ex:438: Module.register_attribute(MODULE, :ecto_autoupdate, accumulate: true)
./lib/ecto/schema.ex:522: autoupdate = @ecto_autoupdate |> Enum.reverse
./lib/ecto/schema.ex:654: Module.put_attribute(MODULE, :ecto_autoupdate, {[updated_at], autogen})

Can someone explain how the ecto_autoupdate attribute is expected to work?

Thank you!

3 Likes