Skip to content

Commit 83abe4f

Browse files
authored
refactor: use multiline string templates to preserve escapes in checks (#578)
1 parent 844f7ba commit 83abe4f

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

lib/migration_generator/operation.ex

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,10 +1351,20 @@ defmodule AshPostgres.MigrationGenerator.Operation do
13511351
},
13521352
table: table
13531353
}) do
1354+
prefix = if schema, do: ", " <> option(:prefix, schema), else: ""
1355+
13541356
if base_filter do
1355-
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"(#{check}) OR NOT (#{base_filter})\")", option(:prefix, schema)])}"
1357+
~s'''
1358+
create constraint(:#{as_atom(table)}, :#{as_atom(name)}, check: """
1359+
(#{check}) OR NOT (#{base_filter})
1360+
"""#{prefix})
1361+
'''
13561362
else
1357-
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"#{check}\")", option(:prefix, schema)])}"
1363+
~s'''
1364+
create constraint(:#{as_atom(table)}, :#{as_atom(name)}, check: """
1365+
#{check}
1366+
"""#{prefix})
1367+
'''
13581368
end
13591369
end
13601370

@@ -1386,10 +1396,20 @@ defmodule AshPostgres.MigrationGenerator.Operation do
13861396
schema: schema,
13871397
table: table
13881398
}) do
1399+
prefix = if schema, do: ", " <> option(:prefix, schema), else: ""
1400+
13891401
if base_filter do
1390-
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"#{base_filter} AND #{check}\")", option(:prefix, schema)])}"
1402+
~s'''
1403+
create constraint(:#{as_atom(table)}, :#{as_atom(name)}, check: """
1404+
#{base_filter} AND #{check}
1405+
"""#{prefix})
1406+
'''
13911407
else
1392-
"create constraint(:#{as_atom(table)}, :#{as_atom(name)}, #{join(["check: \"#{check}\")", option(:prefix, schema)])}"
1408+
~s'''
1409+
create constraint(:#{as_atom(table)}, :#{as_atom(name)}, check: """
1410+
#{check}
1411+
"""#{prefix})
1412+
'''
13931413
end
13941414
end
13951415
end

test/migration_generator_test.exs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,11 +2240,16 @@ defmodule AshPostgres.MigrationGeneratorTest do
22402240
attributes do
22412241
uuid_primary_key(:id)
22422242
attribute(:price, :integer, public?: true)
2243+
attribute(:title, :string, public?: true)
22432244
end
22442245

22452246
postgres do
22462247
check_constraints do
2247-
check_constraint(:price, "price_must_be_positive", check: "price > 0")
2248+
check_constraint(:price, "price_must_be_positive", check: ~S["price" > 0])
2249+
2250+
check_constraint(:title, "title_must_conform_to_format",
2251+
check: ~S[title ~= '("\"\\"\\\"\\\\"\\\\\")']
2252+
)
22482253
end
22492254
end
22502255
end
@@ -2268,7 +2273,18 @@ defmodule AshPostgres.MigrationGeneratorTest do
22682273
|> File.read!()
22692274

22702275
assert file =~
2271-
~S[create constraint(:posts, :price_must_be_positive, check: "price > 0")]
2276+
~S'''
2277+
create constraint(:posts, :price_must_be_positive, check: """
2278+
"price" > 0
2279+
""")
2280+
'''
2281+
2282+
assert file =~
2283+
~S'''
2284+
create constraint(:posts, :title_must_conform_to_format, check: """
2285+
title ~= '("\"\\"\\\"\\\\"\\\\\")'
2286+
""")
2287+
'''
22722288

22732289
defposts do
22742290
attributes do
@@ -2307,7 +2323,11 @@ defmodule AshPostgres.MigrationGeneratorTest do
23072323
String.split(down, "drop_if_exists constraint(:posts, :price_must_be_positive)")
23082324

23092325
assert remaining =~
2310-
~S[create constraint(:posts, :price_must_be_positive, check: "price > 0")]
2326+
~S'''
2327+
create constraint(:posts, :price_must_be_positive, check: """
2328+
"price" > 0
2329+
""")
2330+
'''
23112331
end
23122332

23132333
test "base filters are taken into account, negated" do
@@ -2349,7 +2369,11 @@ defmodule AshPostgres.MigrationGeneratorTest do
23492369
|> File.read!()
23502370

23512371
assert file =~
2352-
~S[create constraint(:posts, :price_must_be_positive, check: "(price > 0) OR NOT (price > -10)")]
2372+
~S'''
2373+
create constraint(:posts, :price_must_be_positive, check: """
2374+
(price > 0) OR NOT (price > -10)
2375+
""")
2376+
'''
23532377
end
23542378

23552379
test "when removed, the constraint is dropped before modification" do

0 commit comments

Comments
 (0)