Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,7 @@ defmodule Ecto.Schema do

if inserted_at do
opts = if source = timestamps[:inserted_at_source], do: [source: source], else: []
opts = Keyword.put(opts, :writable, :insert)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this isn't 100% backwards compatible as the behavior will change subtly if you have @on_writable_violation set to warn or raise attempts to updated inserted_at will warn or raise whereas they previously would be silently ignored, but 🤷‍♂️

To be fully backwards compatible, I guess we would need to not default this and instead allow the user to specify it via an option like inserted_at_writable

Ecto.Schema.__field__(mod, inserted_at, type, opts)
end

Expand Down
29 changes: 29 additions & 0 deletions test/ecto/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,35 @@ defmodule Ecto.SchemaTest do
assert InlineEmbeddedSchema.Many.__schema__(:fields) == [:id, :y]
end

defmodule TimestampsWritableDefault do
use Ecto.Schema

schema "timestamps" do
timestamps()
end
end

defmodule TimestampsWritableRaise do
use Ecto.Schema
@on_writable_violation :raise

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is currently, the only way to actually change the on_writable_violation behavior of inserted_at is via the module-wide @on_writable_violation attribute. There is prior-art with inserted_at_source if we want to add inserted_at_on_writable_violation in a similar manner to let this be configured on the field-level.


schema "timestamps" do
timestamps()
end
end

test "timestamps writable" do
assert TimestampsWritableDefault.__schema__(:insertable_fields) == {[:updated_at, :inserted_at, :id], []}
assert TimestampsWritableDefault.__schema__(:updatable_fields) == {[:updated_at, :id], [:inserted_at]}
assert TimestampsWritableDefault.__schema__(:on_writable_violation, :inserted_at) == :nothing
assert TimestampsWritableDefault.__schema__(:on_writable_violation, :updated_at) == :nothing

assert TimestampsWritableRaise.__schema__(:insertable_fields) == {[:updated_at, :inserted_at, :id], []}
assert TimestampsWritableRaise.__schema__(:updatable_fields) == {[:updated_at, :id], [:inserted_at]}
assert TimestampsWritableRaise.__schema__(:on_writable_violation, :inserted_at) == :raise
assert TimestampsWritableRaise.__schema__(:on_writable_violation, :updated_at) == :raise
end

defmodule TimestampsAutoGen do
use Ecto.Schema

Expand Down
Loading