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
Closes #4212
  • Loading branch information
B4nan committed Apr 22, 2023
1 parent 8e9e7ee commit 41277a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
9 changes: 8 additions & 1 deletion packages/knex/src/schema/SchemaComparator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inspect } from 'util';
import type { Dictionary, EntityProperty } from '@mikro-orm/core';
import { BooleanType, DateTimeType, Utils } from '@mikro-orm/core';
import { BooleanType, DateTimeType, JsonType, parseJsonSafe, Utils } from '@mikro-orm/core';
import type { Check, Column, ForeignKey, Index, SchemaDifference, TableDifference } from '../typings';
import type { DatabaseSchema } from './DatabaseSchema';
import type { DatabaseTable } from './DatabaseTable';
Expand Down 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 @@ -24,7 +24,7 @@ exports[`diffing default values (GH #2385) string defaults do not produce additi
"set names 'utf8';
set session_replication_role = 'replica';
create table "foo2" ("id" serial primary key, "bar0" varchar(255) not null default 'test', "bar1" varchar(255) not null default 'test', "bar2" timestamptz(0) not null default now(), "bar3" timestamptz(6) not null default now());
create table "foo2" ("id" serial primary key, "bar0" varchar(255) not null default 'test', "bar1" varchar(255) not null default 'test', "bar2" timestamptz(0) not null default now(), "bar3" timestamptz(6) not null default now(), "metadata" jsonb not null default '{"value":42}');
set session_replication_role = 'origin';
"
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 41277a1

Please sign in to comment.