Skip to content

Commit

Permalink
fix(postgres): compare only simplified versions of check constraints
Browse files Browse the repository at this point in the history
Closes #3827
  • Loading branch information
B4nan committed Dec 12, 2022
1 parent b27578f commit 0fd8530
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/knex/src/schema/SchemaComparator.ts
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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);
"
`;
Expand Down
Expand Up @@ -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 });
Expand Down

0 comments on commit 0fd8530

Please sign in to comment.