Skip to content
Merged
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
43 changes: 22 additions & 21 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -686,16 +686,7 @@ defmodule AshPostgres.DataLayer do
def can?(_, {:lock, :for_update}), do: true
def can?(_, :composite_types), do: true

def can?(_, {:lock, string}) do
string = String.trim_trailing(string, " NOWAIT")

String.upcase(string) in [
"FOR UPDATE",
"FOR NO KEY UPDATE",
"FOR SHARE",
"FOR KEY SHARE"
]
end
def can?(_, {:lock, string}), do: string |> String.upcase() |> can_lock?()

def can?(_, :transact), do: true
def can?(_, :composite_primary_key), do: true
Expand Down Expand Up @@ -792,6 +783,23 @@ defmodule AshPostgres.DataLayer do
def can?(_, {:sort, _}), do: true
def can?(_, _), do: false

@locks [
"FOR UPDATE",
"FOR NO KEY UPDATE",
"FOR SHARE",
"FOR KEY SHARE"
]

for lock <- @locks do
defp can_lock?(unquote(lock)), do: true

for suffix <- ["NOWAIT", "SKIP LOCKED"] do
defp can_lock?(unquote("#{lock} #{suffix}")), do: true
end
end

defp can_lock?(_), do: false

@impl true
def in_transaction?(resource) do
AshPostgres.DataLayer.Info.repo(resource, :mutate).in_transaction?()
Expand Down Expand Up @@ -3575,13 +3583,6 @@ defmodule AshPostgres.DataLayer do
end
end

@locks [
"FOR UPDATE",
"FOR NO KEY UPDATE",
"FOR SHARE",
"FOR KEY SHARE"
]

for lock <- @locks do
frag = "#{lock} OF ?"

Expand All @@ -3590,16 +3591,16 @@ defmodule AshPostgres.DataLayer do
end

frag = "#{lock} OF ? NOWAIT"
lock = "#{lock} NOWAIT"
new_lock = "#{lock} NOWAIT"

def lock(query, unquote(lock), _) do
def lock(query, unquote(new_lock), _) do
{:ok, Ecto.Query.lock(query, [{^0, a}], fragment(unquote(frag), a))}
end

frag = "#{lock} OF ? SKIP LOCKED"
lock = "#{lock} SKIP LOCKED"
new_lock = "#{lock} SKIP LOCKED"

def lock(query, unquote(lock), _) do
def lock(query, unquote(new_lock), _) do
{:ok, Ecto.Query.lock(query, [{^0, a}], fragment(unquote(frag), a))}
end
end
Expand Down