Skip to content

Commit

Permalink
fix(mysql): improve diffing of defaults for JSON columns
Browse files Browse the repository at this point in the history
Closes #4926
  • Loading branch information
B4nan committed Nov 17, 2023
1 parent a7e2291 commit d92a440
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/knex/src/schema/SchemaComparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ export class SchemaComparator {
}

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

return Utils.equals(defaultValueFrom, defaultValueTo);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/features/schema-generator/json-diffing.mysql.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/mysql';

@Entity()
class User {

@PrimaryKey()
id!: number;

@Property()
name!: string;

@Property({ type: 'json', defaultRaw: `('{"a": 1}')` })
data!: any;

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [User],
dbName: `4926`,
port: 3308,
});
await orm.schema.refreshDatabase();
});

afterAll(() => orm.close(true));

test('default values on json columns', async () => {
const diff1 = await orm.schema.getUpdateSchemaSQL({ wrap: false });
expect(diff1).toBe('');
});

0 comments on commit d92a440

Please sign in to comment.