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

fix: handle regex match correctly #460

Merged
merged 2 commits into from
Jan 14, 2025
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
228 changes: 170 additions & 58 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@
case Ash.Resource.Info.primary_key(query.resource) do
[] ->
case Ash.Resource.Info.identities(query.resource) do
%{keys: keys} -> keys

Check warning on line 1300 in lib/data_layer.ex

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix dialyzer

pattern_match

The pattern can never match the type [

Check warning on line 1300 in lib/data_layer.ex

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix dialyzer

pattern_match

The pattern can never match the type [

Check warning on line 1300 in lib/data_layer.ex

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix dialyzer

pattern_match

The pattern can never match the type [
_ -> []
end

Expand Down Expand Up @@ -2468,11 +2468,21 @@
constraint.attribute
|> List.wrap()
|> Enum.reduce(changeset, fn attribute, changeset ->
Ecto.Changeset.check_constraint(changeset, attribute,
name: constraint.name,
message: constraint.message || "is invalid",
match: repo.default_constraint_match_type(:check, constraint.name)
)
case repo.default_constraint_match_type(:check, constraint.name) do
{:regex, regex} ->
Ecto.Changeset.check_constraint(changeset, attribute,
name: regex,
message: constraint.message || "is invalid",
match: :exact
)

match ->
Ecto.Changeset.check_constraint(changeset, attribute,
name: constraint.name,
message: constraint.message || "is invalid",
match: match
)
end
end)
end)
end
Expand All @@ -2483,17 +2493,36 @@
|> Enum.reduce(changeset, fn constraint, changeset ->
case constraint do
{key, name} ->
Ecto.Changeset.exclusion_constraint(changeset, key,
name: name,
match: repo.default_constraint_match_type(:exclusion, constraint.name)
)
case repo.default_constraint_match_type(:check, name) do
{:regex, regex} ->
Ecto.Changeset.check_constraint(changeset, key,
name: regex,
match: :exact
)

match ->
Ecto.Changeset.check_constraint(changeset, key,
name: name,
match: match
)
end

{key, name, message} ->
Ecto.Changeset.exclusion_constraint(changeset, key,
name: name,
message: message,
match: repo.default_constraint_match_type(:exclusion, constraint.name)
)
case repo.default_constraint_match_type(:check, name) do
{:regex, regex} ->
Ecto.Changeset.check_constraint(changeset, key,
name: regex,
message: message,
match: :exact
)

match ->
Ecto.Changeset.check_constraint(changeset, key,
name: name,
message: message,
match: match
)
end
end
end)
end
Expand All @@ -2520,20 +2549,40 @@
changeset ->
case AshPostgres.DataLayer.Info.reference(resource, relationship_name) do
%{name: name} when not is_nil(name) ->
Ecto.Changeset.foreign_key_constraint(changeset, destination_attribute,
name: name,
match: repo.default_constraint_match_type(:foreign, name),
message: "would leave records behind"
)
case repo.default_constraint_match_type(:foreign, name) do
{:regex, regex} ->
Ecto.Changeset.foreign_key_constraint(changeset, destination_attribute,
name: regex,
message: "would leave records behind",
match: :exact
)

match ->
Ecto.Changeset.foreign_key_constraint(changeset, destination_attribute,
name: name,
message: "would leave records behind",
match: match
)
end

_ ->
name = "#{AshPostgres.DataLayer.Info.table(source)}_#{source_attribute}_fkey"

Ecto.Changeset.foreign_key_constraint(changeset, destination_attribute,
name: name,
match: repo.default_constraint_match_type(:foreign, name),
message: "would leave records behind"
)
case repo.default_constraint_match_type(:foreign, name) do
{:regex, regex} ->
Ecto.Changeset.foreign_key_constraint(changeset, destination_attribute,
name: regex,
message: "would leave records behind",
match: :exact
)

match ->
Ecto.Changeset.foreign_key_constraint(changeset, destination_attribute,
name: name,
message: "would leave records behind",
match: match
)
end
end
end)
end
Expand All @@ -2545,10 +2594,19 @@
name =
"#{AshPostgres.DataLayer.Info.table(resource)}_#{relationship.source_attribute}_fkey"

Ecto.Changeset.foreign_key_constraint(changeset, relationship.source_attribute,
name: name,
match: repo.default_constraint_match_type(:foreign, name)
)
case repo.default_constraint_match_type(:foreign, name) do
{:regex, regex} ->
Ecto.Changeset.foreign_key_constraint(changeset, relationship.source_attribute,
name: regex,
match: :exact
)

match ->
Ecto.Changeset.foreign_key_constraint(changeset, relationship.source_attribute,
name: name,
match: match
)
end
end)
end

Expand All @@ -2561,17 +2619,36 @@
end
|> Enum.reduce(changeset, fn
{key, name}, changeset ->
Ecto.Changeset.foreign_key_constraint(changeset, key,
name: name,
match: repo.default_constraint_match_type(:foreign, name)
)
case repo.default_constraint_match_type(:foreign, name) do
{:regex, regex} ->
Ecto.Changeset.foreign_key_constraint(changeset, key,
name: regex,
match: :exact
)

match ->
Ecto.Changeset.foreign_key_constraint(changeset, key,
name: name,
match: match
)
end

{key, name, message}, changeset ->
Ecto.Changeset.foreign_key_constraint(changeset, key,
name: name,
message: message,
match: repo.default_constraint_match_type(:foreign, name)
)
case repo.default_constraint_match_type(:foreign, name) do
{:regex, regex} ->
Ecto.Changeset.foreign_key_constraint(changeset, key,
name: regex,
message: message,
match: :exact
)

match ->
Ecto.Changeset.foreign_key_constraint(changeset, key,
name: name,
message: message,
match: match
)
end
end)
end

Expand All @@ -2587,13 +2664,21 @@
AshPostgres.DataLayer.Info.identity_index_names(resource)[identity.name] ||
"#{table}_#{identity.name}_index"

index_match_type = repo.default_constraint_match_type(:unique, name)

opts =
if Map.get(identity, :message) do
[name: name, message: identity.message, match: index_match_type]
else
[name: name, match: index_match_type]
case repo.default_constraint_match_type(:unique, name) do
{:regex, regex} ->
if Map.get(identity, :message) do
[name: regex, message: identity.message, match: :exact]
else
[name: regex, match: :exact]
end

index_match_type ->
if Map.get(identity, :message) do
[name: name, message: identity.message, match: index_match_type]
else
[name: name, match: index_match_type]
end
end

fields =
Expand All @@ -2614,13 +2699,21 @@
|> Enum.reduce(changeset, fn index, changeset ->
name = index.name || AshPostgres.CustomIndex.name(table, index)

index_match_type = repo.default_constraint_match_type(:custom, name)

opts =
if index.message do
[name: name, message: index.message, match: index_match_type]
else
[name: name, match: index_match_type]
case repo.default_constraint_match_type(:custom, name) do
{:regex, regex} ->
if Map.get(index, :message) do
[name: regex, message: index.message, match: :exact]
else
[name: regex, match: :exact]
end

index_match_type ->
if Map.get(index, :message) do
[name: name, message: index.message, match: index_match_type]
else
[name: name, match: index_match_type]
end
end

fields =
Expand Down Expand Up @@ -2662,17 +2755,36 @@

Enum.reduce(names, changeset, fn
{keys, name}, changeset ->
Ecto.Changeset.unique_constraint(changeset, List.wrap(keys),
name: name,
match: repo.default_constraint_match_type(:unique, name)
)
case repo.default_constraint_match_type(:unique, name) do
{:regex, regex} ->
Ecto.Changeset.unique_constraint(changeset, List.wrap(keys),
name: regex,
match: :exact
)

match ->
Ecto.Changeset.unique_constraint(changeset, List.wrap(keys),
name: name,
match: match
)
end

{keys, name, message}, changeset ->
Ecto.Changeset.unique_constraint(changeset, List.wrap(keys),
name: name,
message: message,
match: repo.default_constraint_match_type(:unique, name)
)
case repo.default_constraint_match_type(:unique, name) do
{:regex, regex} ->
Ecto.Changeset.unique_constraint(changeset, List.wrap(keys),
name: regex,
message: message,
match: :exact
)

match ->
Ecto.Changeset.unique_constraint(changeset, List.wrap(keys),
name: name,
message: message,
match: match
)
end
end)
end

Expand Down
1 change: 1 addition & 0 deletions test/support/resources/comedian.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule AshPostgres.Test.Comedian do
end

defmodule AshPostgres.Test.Comedian.HasJokes do
@moduledoc false
use Ash.Resource.Calculation

@impl true
Expand Down
Loading