diff --git a/lib/migration_generator/operation.ex b/lib/migration_generator/operation.ex index 5c8bfbc0..3b266494 100644 --- a/lib/migration_generator/operation.ex +++ b/lib/migration_generator/operation.ex @@ -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 @@ -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 diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index bd8f2c92..e284e847 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -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 @@ -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 @@ -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 @@ -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