Skip to content

refactor: use multiline string templates to preserve escapes in checks #578

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

Merged
merged 1 commit into from
Jun 29, 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
28 changes: 24 additions & 4 deletions lib/migration_generator/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1351,10 +1351,20 @@ defmodule AshPostgres.MigrationGenerator.Operation do
},
table: table
}) do
prefix = if schema, do: ", " <> option(:prefix, schema), else: ""

if base_filter do
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"(#{check}) OR NOT (#{base_filter})\")", option(:prefix, schema)])}"
~s'''
create constraint(:#{as_atom(table)}, :#{as_atom(name)}, check: """
(#{check}) OR NOT (#{base_filter})
"""#{prefix})
'''
else
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"#{check}\")", option(:prefix, schema)])}"
~s'''
create constraint(:#{as_atom(table)}, :#{as_atom(name)}, check: """
#{check}
"""#{prefix})
'''
end
end

Expand Down Expand Up @@ -1386,10 +1396,20 @@ defmodule AshPostgres.MigrationGenerator.Operation do
schema: schema,
table: table
}) do
prefix = if schema, do: ", " <> option(:prefix, schema), else: ""

if base_filter do
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"#{base_filter} AND #{check}\")", option(:prefix, schema)])}"
~s'''
create constraint(:#{as_atom(table)}, :#{as_atom(name)}, check: """
#{base_filter} AND #{check}
"""#{prefix})
'''
else
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"#{check}\")", option(:prefix, schema)])}"
~s'''
create constraint(:#{as_atom(table)}, :#{as_atom(name)}, check: """
#{check}
"""#{prefix})
'''
end
end
end
Expand Down
32 changes: 28 additions & 4 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: ~S[title ~= '("\"\\"\\\"\\\\"\\\\\")']
)
end
end
end
Expand All @@ -2268,7 +2273,18 @@ 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 =~
~S'''
create constraint(:posts, :title_must_conform_to_format, check: """
title ~= '("\"\\"\\\"\\\\"\\\\\")'
""")
'''

defposts do
attributes do
Expand Down Expand Up @@ -2307,7 +2323,11 @@ 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 Expand Up @@ -2349,7 +2369,11 @@ defmodule AshPostgres.MigrationGeneratorTest do
|> File.read!()

assert file =~
~S[create constraint(:posts, :price_must_be_positive, check: "(price > 0) OR NOT (price > -10)")]
~S'''
create constraint(:posts, :price_must_be_positive, check: """
(price > 0) OR NOT (price > -10)
""")
'''
end

test "when removed, the constraint is dropped before modification" do
Expand Down
Loading