Skip to content

Commit

Permalink
fix(schema): fix comparing default value of JSON properties
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan authored and jsprw committed May 7, 2023
1 parent a9ef5a4 commit d32843b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/knex/src/schema/SchemaComparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ export class SchemaComparator {
return defaultValueFrom === defaultValueTo;
}

if (to.mappedType instanceof JsonType) {
const defaultValueFrom = parseJsonSafe(from.default.replace(/^'(.*)'$/, '$1'));
const defaultValueTo = parseJsonSafe(to.default?.replace(/^'(.*)'$/, '$1'));

return Utils.equals(defaultValueFrom, defaultValueTo);
}

if (to.mappedType instanceof DateTimeType && from.default && to.default) {
// normalize now/current_timestamp defaults, also remove `()` from the end of default expression
const defaultValueFrom = from.default.toLowerCase().replace('current_timestamp', 'now').replace(/\(\)$/, '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`diffing default values (GH #2385) string defaults do not produce additi
"set names utf8mb4;
set foreign_key_checks = 0;
create table \`foo1\` (\`id\` int unsigned not null auto_increment primary key, \`bar0\` varchar(255) not null default 'test', \`bar1\` varchar(255) not null default 'test', \`bar2\` datetime not null default now(), \`bar3\` datetime(6) not null default now(6)) default character set utf8mb4 engine = InnoDB;
create table \`foo1\` (\`id\` int unsigned not null auto_increment primary key, \`bar0\` varchar(255) not null default 'test', \`bar1\` varchar(255) not null default 'test', \`bar2\` datetime not null default now(), \`bar3\` datetime(6) not null default now(6), \`metadata\` json not null default '{"value":42}') default character set utf8mb4 engine = InnoDB;
set foreign_key_checks = 1;
"
Expand All @@ -14,7 +14,7 @@ exports[`diffing default values (GH #2385) string defaults do not produce additi
"set names utf8mb4;
set foreign_key_checks = 0;
create table \`foo1\` (\`id\` int unsigned not null auto_increment primary key, \`bar0\` varchar(255) not null default 'test', \`bar1\` varchar(255) not null default 'test', \`bar2\` datetime not null default now(), \`bar3\` datetime(6) not null default now(6)) default character set utf8mb4 engine = InnoDB;
create table \`foo0\` (\`id\` int unsigned not null auto_increment primary key, \`bar0\` varchar(255) not null default 'test', \`bar1\` varchar(255) not null default 'test', \`bar2\` datetime not null default now(), \`bar3\` datetime(6) not null default now(6)) default character set utf8mb4 engine = InnoDB;
set foreign_key_checks = 1;
"
Expand All @@ -33,7 +33,7 @@ set session_replication_role = 'origin';
exports[`diffing default values (GH #2385) string defaults do not produce additional diffs [sqlite] 1`] = `
"pragma foreign_keys = off;
create table \`foo3\` (\`id\` integer not null primary key autoincrement, \`bar0\` text not null default 'test', \`bar1\` text not null default 'test', \`bar2\` datetime not null default now);
create table \`foo3\` (\`id\` integer not null primary key autoincrement, \`bar0\` text not null default 'test', \`bar1\` text not null default 'test', \`bar2\` datetime not null default now, \`metadata\` json not null default '{"value":43}');
pragma foreign_keys = on;
"
Expand Down
22 changes: 21 additions & 1 deletion tests/features/schema-generator/diffing-default-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ export class Foo {

}

@Entity()
export class Foo0 extends Foo {

@Property({ defaultRaw: 'now()' })
bar2!: Date;

@Property({ defaultRaw: 'now(6)', length: 6 })
bar3!: Date;

}

@Entity()
export class Foo1 extends Foo {

Expand All @@ -26,6 +37,9 @@ export class Foo1 extends Foo {
@Property({ defaultRaw: 'now(6)', length: 6 })
bar3!: Date;

@Property({ type: 'json', default: JSON.stringify({ value: 42 }) })
metadata!: any;

}

@Entity()
Expand All @@ -37,6 +51,9 @@ export class Foo2 extends Foo {
@Property({ defaultRaw: 'now()', length: 6 })
bar3!: Date;

@Property({ type: 'json', default: JSON.stringify({ value: 42 }) })
metadata!: any;

}

@Entity()
Expand All @@ -45,13 +62,16 @@ export class Foo3 extends Foo {
@Property({ defaultRaw: 'now' })
bar2!: Date;

@Property({ type: 'json', default: JSON.stringify({ value: 43 }) })
metadata!: any;

}

describe('diffing default values (GH #2385)', () => {

test('string defaults do not produce additional diffs [mysql]', async () => {
const orm = await MikroORM.init({
entities: [Foo1],
entities: [Foo0],
dbName: 'mikro_orm_test_gh_2385',
driver: MySqlDriver,
port: 3308,
Expand Down

0 comments on commit d32843b

Please sign in to comment.