diff --git a/lib/migration_generator/operation.ex b/lib/migration_generator/operation.ex index 5c8bfbc0..71701376 100644 --- a/lib/migration_generator/operation.ex +++ b/lib/migration_generator/operation.ex @@ -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 @@ -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 diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index bd8f2c92..dee4b412 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: "title ~= '(\"\\\"\\\\\")'" + ) end end end @@ -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 @@ -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