The goal of this task is to provide automatic management of created_at and updated_at for models. it consists of three changes.
Schemas will automatically generate inserted_at and updated_at fields. @timestamps false can be set before the schema/2 to avoid generating timestamps and @timestamps Type can be used to give the generated timestamps a custom time.
Migrations will automatically generate inserted_at and updated_at fields. timestamps: false can be given to table/2 to not generate a timestamp or customize its type.
Ecto.Model.Timestamps will include a before_insert and a before_update callback into the model that automatically sets the inserted_at and updated_at values if such fields exist in the model and are not set in the changeset.
/cc @ericmj @MSch @drewolson thoughts?
@josevalim Repo.touch/2 function could be useful too
Repo.touch(records, :designed_at)
Repo.touch(records) # default to :updated_at
Might be nice to make Ecto.Model.Timestamps not only check inserted_at and updated_at but also created_at, created_on and updated_on, to match Rails behavior. Or just make the field names customizable.
I also vote for created_at/updated_at couple. These are quite common names and using them would make it easier to port existing applications without having to disable timestamp support, or worse rename fields.
Making them configurable would of course be best, but maybe unnecessary.
So we should make it configurable given that I don't think it makes sense to use created_at as nothing in Ecto uses the create terminology. :(
Yay!
Sent from my non-google-device
On Jan 15, 2015, at 10:16, José Valim [email protected] wrote:
So we should make it configurable given that I don't think it makes sense to use created_at as nothing in Ecto uses the create terminology. :(
—
Reply to this email directly or view it on GitHub.
I like this, seems like a no brainer to do it.
I prefer inserted_at to created_at, but an option to change that might be useful.
@josevalim so how can you configure Ecto to use :created_at and :updated_at to be backwards compatible with an existing Rails DB?
@tilo you can pass options to the Ecto.Schema.timestamps/1 macro or use the @timestamp_opts attribute - you can find more information in the documentation https://hexdocs.pm/ecto/Ecto.Schema.html#timestamps/1
Most helpful comment
@josevalim
Repo.touch/2function could be useful too