Skip to content

Commit f616565

Browse files
authored
fix: Fix locks handling for WAIT and SKIP_LOCKED (#704)
1 parent f15ddb2 commit f616565

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

lib/data_layer.ex

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -686,16 +686,7 @@ defmodule AshPostgres.DataLayer do
686686
def can?(_, {:lock, :for_update}), do: true
687687
def can?(_, :composite_types), do: true
688688

689-
def can?(_, {:lock, string}) do
690-
string = String.trim_trailing(string, " NOWAIT")
691-
692-
String.upcase(string) in [
693-
"FOR UPDATE",
694-
"FOR NO KEY UPDATE",
695-
"FOR SHARE",
696-
"FOR KEY SHARE"
697-
]
698-
end
689+
def can?(_, {:lock, string}), do: string |> String.upcase() |> can_lock?()
699690

700691
def can?(_, :transact), do: true
701692
def can?(_, :composite_primary_key), do: true
@@ -792,6 +783,23 @@ defmodule AshPostgres.DataLayer do
792783
def can?(_, {:sort, _}), do: true
793784
def can?(_, _), do: false
794785

786+
@locks [
787+
"FOR UPDATE",
788+
"FOR NO KEY UPDATE",
789+
"FOR SHARE",
790+
"FOR KEY SHARE"
791+
]
792+
793+
for lock <- @locks do
794+
defp can_lock?(unquote(lock)), do: true
795+
796+
for suffix <- ["NOWAIT", "SKIP LOCKED"] do
797+
defp can_lock?(unquote("#{lock} #{suffix}")), do: true
798+
end
799+
end
800+
801+
defp can_lock?(_), do: false
802+
795803
@impl true
796804
def in_transaction?(resource) do
797805
AshPostgres.DataLayer.Info.repo(resource, :mutate).in_transaction?()
@@ -3575,13 +3583,6 @@ defmodule AshPostgres.DataLayer do
35753583
end
35763584
end
35773585

3578-
@locks [
3579-
"FOR UPDATE",
3580-
"FOR NO KEY UPDATE",
3581-
"FOR SHARE",
3582-
"FOR KEY SHARE"
3583-
]
3584-
35853586
for lock <- @locks do
35863587
frag = "#{lock} OF ?"
35873588

@@ -3590,16 +3591,16 @@ defmodule AshPostgres.DataLayer do
35903591
end
35913592

35923593
frag = "#{lock} OF ? NOWAIT"
3593-
lock = "#{lock} NOWAIT"
3594+
new_lock = "#{lock} NOWAIT"
35943595

3595-
def lock(query, unquote(lock), _) do
3596+
def lock(query, unquote(new_lock), _) do
35963597
{:ok, Ecto.Query.lock(query, [{^0, a}], fragment(unquote(frag), a))}
35973598
end
35983599

35993600
frag = "#{lock} OF ? SKIP LOCKED"
3600-
lock = "#{lock} SKIP LOCKED"
3601+
new_lock = "#{lock} SKIP LOCKED"
36013602

3602-
def lock(query, unquote(lock), _) do
3603+
def lock(query, unquote(new_lock), _) do
36033604
{:ok, Ecto.Query.lock(query, [{^0, a}], fragment(unquote(frag), a))}
36043605
end
36053606
end

0 commit comments

Comments
 (0)