Skip to content
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

Add :allow_stale option for Repo.{insert, update, delete} #4482

Merged
merged 5 commits into from
Aug 11, 2024
Merged
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
16 changes: 16 additions & 0 deletions lib/ecto/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,10 @@ defmodule Ecto.Repo do
* `:stale_error_message` - The message to add to the configured
`:stale_error_field` when stale errors happen, defaults to "is stale".

* `:allow_stale` - Doesn't error if insert is stale. Defaults to `false`.
This may happen if there are rules or triggers in the database that
rejects the insert operation.

See the ["Shared options"](#module-shared-options) section at the module
documentation for more options.

Expand Down Expand Up @@ -1847,6 +1851,11 @@ defmodule Ecto.Repo do
* `:stale_error_message` - The message to add to the configured
`:stale_error_field` when stale errors happen, defaults to "is stale".

* `:allow_stale` - Doesn't error if update is stale. Defaults to `false`.
This may happen if the struct has been deleted from the database before
the update or if there is a rule or a trigger on the database that rejects
the update operation.

See the ["Shared options"](#module-shared-options) section at the module
documentation for more options.

Expand Down Expand Up @@ -1891,6 +1900,8 @@ defmodule Ecto.Repo do
* `:stale_error_message` - The message to add to the configured
`:stale_error_field` when stale errors happen, defaults to "is stale".
Only applies to updates.
* `:allow_stale` - Doesn't error if delete is stale. Defaults to `false`.
Only applies to updates.

See the ["Shared options"](#module-shared-options) section at the module
documentation for more options.
Expand Down Expand Up @@ -1951,6 +1962,11 @@ defmodule Ecto.Repo do
* `:stale_error_message` - The message to add to the configured
`:stale_error_field` when stale errors happen, defaults to "is stale".

* `:allow_stale` - Doesn't error if delete is stale. Defaults to `false`.
v0idpwn marked this conversation as resolved.
Show resolved Hide resolved
This may happen if the struct has been deleted from the database before
this deletion or if there is a rule or a trigger on the database that rejects
the delete operation.

See the ["Shared options"](#module-shared-options) section at the module
documentation for more options.

Expand Down
20 changes: 12 additions & 8 deletions lib/ecto/repo/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -841,14 +841,18 @@ defmodule Ecto.Repo.Schema do
{:error, :stale} ->
opts = List.last(args)

case Keyword.fetch(opts, :stale_error_field) do
{:ok, stale_error_field} when is_atom(stale_error_field) ->
stale_message = Keyword.get(opts, :stale_error_message, "is stale")
user_changeset = Changeset.add_error(user_changeset, stale_error_field, stale_message, [stale: true])
{:error, user_changeset}

_other ->
raise Ecto.StaleEntryError, changeset: user_changeset, action: action
if Keyword.get(opts, :allow_stale, false) do
{:ok, []}
else
case Keyword.fetch(opts, :stale_error_field) do
{:ok, stale_error_field} when is_atom(stale_error_field) ->
stale_message = Keyword.get(opts, :stale_error_message, "is stale")
user_changeset = Changeset.add_error(user_changeset, stale_error_field, stale_message, [stale: true])
{:error, user_changeset}

_other ->
raise Ecto.StaleEntryError, changeset: user_changeset, action: action
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/ecto/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,17 @@ defmodule Ecto.RepoTest do
assert changeset.errors == [id: {"is old", [stale: true]}]
end

test "insert, update, and delete allows stale with :allow_stale option" do
my_schema = %MySchema{id: 1}
my_schema = put_in(my_schema.__meta__.context, {:error, :stale})

stale = Ecto.Changeset.cast(my_schema, %{x: "foo"}, [:x])

assert {:ok, _} = TestRepo.insert(stale, allow_stale: true)
assert {:ok, _} = TestRepo.update(stale, allow_stale: true)
assert {:ok, _} = TestRepo.delete(stale, allow_stale: true)
end

test "insert and delete sets schema prefix with struct" do
valid = %MySchema{id: 1}

Expand Down
Loading