Skip to content

fix: escape " in check constraint generator #576

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions lib/migration_generator/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
},
table: table
}) do
check = String.replace(check, ~r/((?:\\)*)"/, ~S[\1\"])

if base_filter do
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"(#{check}) OR NOT (#{base_filter})\")", option(:prefix, schema)])}"
else
Expand Down Expand Up @@ -1386,6 +1388,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
schema: schema,
table: table
}) do
check = String.replace(check, ~r/((?:\\)*)"/, ~S[\1\"])

if base_filter do
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"#{base_filter} AND #{check}\")", option(:prefix, schema)])}"
else
Expand Down
14 changes: 11 additions & 3 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2240,11 +2240,16 @@ defmodule AshPostgres.MigrationGeneratorTest do
attributes do
uuid_primary_key(:id)
attribute(:price, :integer, public?: true)
attribute(:title, :string, public?: true)
end

postgres do
check_constraints do
check_constraint(:price, "price_must_be_positive", check: "price > 0")
check_constraint(:price, "price_must_be_positive", check: ~S["price" > 0])

check_constraint(:title, "title_must_conform_to_format",
check: "title ~= '(\"\\\"\\\\\")'"
)
end
end
end
Expand All @@ -2268,7 +2273,10 @@ defmodule AshPostgres.MigrationGeneratorTest do
|> File.read!()

assert file =~
~S[create constraint(:posts, :price_must_be_positive, check: "price > 0")]
~S[create constraint(:posts, :price_must_be_positive, check: "\"price\" > 0")]

assert file =~
"create constraint(:posts, :title_must_conform_to_format, check: \"title ~= '(\\\"\\\\\"\\\\\\\")'\""

defposts do
attributes do
Expand Down Expand Up @@ -2307,7 +2315,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
String.split(down, "drop_if_exists constraint(:posts, :price_must_be_positive)")

assert remaining =~
~S[create constraint(:posts, :price_must_be_positive, check: "price > 0")]
~S[create constraint(:posts, :price_must_be_positive, check: "\"price\" > 0")]
end

test "base filters are taken into account, negated" do
Expand Down
Loading