diff --git a/packages/knex/src/schema/SchemaComparator.ts b/packages/knex/src/schema/SchemaComparator.ts index e2270849d89d..a9ccae6f34aa 100644 --- a/packages/knex/src/schema/SchemaComparator.ts +++ b/packages/knex/src/schema/SchemaComparator.ts @@ -524,8 +524,10 @@ export class SchemaComparator { } diffCheck(check1: Check, check2: Check): boolean { - const unquote = (str?: string) => str?.replace(/['"`]/g, ''); - return unquote(check1.expression as string) !== unquote(check2.expression as string); + // check constraint definition might be normalized by the driver, + // e.g. quotes might be added (https://github.com/mikro-orm/mikro-orm/issues/3827) + const simplify = (str?: string) => str?.replace(/['"`()]/g, '').toLowerCase(); + return simplify(check1.expression as string) !== simplify(check2.expression as string); } hasSameDefaultValue(from: Column, to: Column): boolean { diff --git a/tests/features/schema-generator/__snapshots__/check-constraint.postgres.test.ts.snap b/tests/features/schema-generator/__snapshots__/check-constraint.postgres.test.ts.snap index 382b14a61388..737e89a38163 100644 --- a/tests/features/schema-generator/__snapshots__/check-constraint.postgres.test.ts.snap +++ b/tests/features/schema-generator/__snapshots__/check-constraint.postgres.test.ts.snap @@ -20,7 +20,7 @@ exports[`check constraint [postgres] check constraint diff [postgres]: postgres- `; exports[`check constraint [postgres] check constraint diff [postgres]: postgres-check-constraint-diff-4 1`] = ` -"alter table "new_table" add constraint bar check(price > 0); +"alter table "new_table" add constraint bar check(price > 0 and price < 123); " `; diff --git a/tests/features/schema-generator/check-constraint.postgres.test.ts b/tests/features/schema-generator/check-constraint.postgres.test.ts index b203ec5b77e1..1a6f212a1234 100644 --- a/tests/features/schema-generator/check-constraint.postgres.test.ts +++ b/tests/features/schema-generator/check-constraint.postgres.test.ts @@ -83,10 +83,12 @@ describe('check constraint [postgres]', () => { await generator.execute(diff); // Add new check - newTableMeta.checks = [{ name: 'bar', expression: 'price > 0' }]; + newTableMeta.checks = [{ name: 'bar', expression: 'price > 0 and price < 123' }]; diff = await orm.schema.getUpdateSchemaSQL({ wrap: false }); expect(diff).toMatchSnapshot('postgres-check-constraint-diff-4'); await generator.execute(diff); + diff = await orm.schema.getUpdateSchemaSQL({ wrap: false }); + expect(diff).toBe(''); // Skip existing check diff = await orm.schema.getUpdateSchemaSQL({ wrap: false });