Packages
ecto_sql
3.3.3
3.14.0
3.13.5
3.13.4
3.13.3
3.13.2
3.13.1
3.13.0
3.12.1
3.12.0
3.11.3
3.11.2
3.11.1
3.11.0
3.10.2
3.10.1
3.10.0
3.9.2
3.9.1
3.9.0
3.8.3
3.8.2
3.8.1
3.8.0
3.7.2
3.7.1
3.7.0
3.6.2
3.6.1
3.6.0
3.5.4
3.5.3
3.5.2
3.5.1
3.5.0
3.5.0-rc.1
3.5.0-rc.0
3.4.5
3.4.4
3.4.3
3.4.2
3.4.1
3.4.0
3.3.4
3.3.3
3.3.2
3.3.1
3.3.0
3.2.2
3.2.1
3.2.0
3.1.6
3.1.5
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
3.0.0-rc.1
3.0.0-rc.0
SQL-based adapters for Ecto and database migrations
Current section
Files
Jump to
Current section
Files
integration_test/sql/lock.exs
defmodule Ecto.Integration.LockTest do
# We can keep this test async as long as it
# is the only one accessing the lock_test table.
use ExUnit.Case, async: true
import Ecto.Query
alias Ecto.Integration.PoolRepo
defmodule LockCounter do
use Ecto.Schema
schema "lock_counters" do
field :count, :integer
end
end
setup do
PoolRepo.delete_all(LockCounter)
:ok
end
test "lock for update" do
%{id: id} = PoolRepo.insert!(%LockCounter{count: 1})
pid = self()
lock_for_update =
Application.get_env(:ecto_sql, :lock_for_update) ||
raise ":lock_for_update not set in :ecto application"
# Here we are manually inserting the lock in the query
# to test multiple adapters. Never do this in actual
# application code: it is not safe and not public.
query = from(lc in LockCounter, where: lc.id == ^id)
query = %{query | lock: lock_for_update}
{:ok, new_pid} =
Task.start_link fn ->
assert_receive :select_for_update, 5000
PoolRepo.transaction(fn ->
[post] = PoolRepo.all(query) # this should block until the other trans. commit
post |> Ecto.Changeset.change(count: post.count + 1) |> PoolRepo.update!
end)
send pid, :updated
end
PoolRepo.transaction(fn ->
[post] = PoolRepo.all(query) # select and lock the row
send new_pid, :select_for_update # signal second process to begin a transaction
post |> Ecto.Changeset.change(count: post.count + 1) |> PoolRepo.update!
end)
assert_receive :updated, 5000
# Final count will be 3 if SELECT ... FOR UPDATE worked and 2 otherwise
assert [%LockCounter{count: 3}] = PoolRepo.all(LockCounter)
end
end