-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(schema): improve diffing of default values for strings and dates
Closes #2385
- Loading branch information
Showing
15 changed files
with
199 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { MySqlPlatform } from '@mikro-orm/mysql-base'; | ||
import { MariaDbSchemaHelper } from './MariaDbSchemaHelper'; | ||
|
||
export class MariaDbPlatform extends MySqlPlatform { | ||
|
||
protected readonly schemaHelper: MariaDbSchemaHelper = new MariaDbSchemaHelper(this); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Type } from '@mikro-orm/core'; | ||
import { MySqlSchemaHelper } from '@mikro-orm/mysql-base'; | ||
|
||
export class MariaDbSchemaHelper extends MySqlSchemaHelper { | ||
|
||
protected wrap(val: string | undefined, _type: Type<unknown>): string | undefined { | ||
return val; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from '@mikro-orm/mysql-base'; | ||
export * from './MariaDbConnection'; | ||
export * from './MariaDbSchemaHelper'; | ||
export * from './MariaDbPlatform'; | ||
export * from './MariaDbDriver'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
tests/features/schema-generator/__snapshots__/diffing-default-values.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`diffing default values (GH #2385) string defaults do not produce additional diffs [mariadb] 1`] = ` | ||
"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; | ||
set foreign_key_checks = 1; | ||
" | ||
`; | ||
|
||
exports[`diffing default values (GH #2385) string defaults do not produce additional diffs [mysql] 1`] = ` | ||
"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; | ||
set foreign_key_checks = 1; | ||
" | ||
`; | ||
|
||
exports[`diffing default values (GH #2385) string defaults do not produce additional diffs [postgres] 1`] = ` | ||
"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()); | ||
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); | ||
pragma foreign_keys = on; | ||
" | ||
`; |
104 changes: 104 additions & 0 deletions
104
tests/features/schema-generator/diffing-default-values.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Entity, MikroORM, PrimaryKey, Property } from '@mikro-orm/core'; | ||
|
||
export class Foo { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ defaultRaw: "'test'" }) | ||
bar0!: string; | ||
|
||
@Property({ default: 'test' }) | ||
bar1!: string; | ||
|
||
} | ||
|
||
@Entity() | ||
export class Foo1 extends Foo { | ||
|
||
@Property({ defaultRaw: 'now()' }) | ||
bar2!: Date; | ||
|
||
@Property({ defaultRaw: 'now(6)', length: 6 }) | ||
bar3!: Date; | ||
|
||
} | ||
|
||
@Entity() | ||
export class Foo2 extends Foo { | ||
|
||
@Property({ defaultRaw: 'now()' }) | ||
bar2!: Date; | ||
|
||
@Property({ defaultRaw: 'now()', length: 6 }) | ||
bar3!: Date; | ||
|
||
} | ||
|
||
@Entity() | ||
export class Foo3 extends Foo { | ||
|
||
@Property({ defaultRaw: 'now' }) | ||
bar2!: Date; | ||
|
||
} | ||
|
||
describe('diffing default values (GH #2385)', () => { | ||
|
||
test('string defaults do not produce additional diffs [mysql]', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Foo1], | ||
dbName: 'mikro_orm_test_gh_2385', | ||
type: 'mysql', | ||
port: 3307, | ||
}); | ||
await orm.getSchemaGenerator().ensureDatabase(); | ||
await orm.getSchemaGenerator().dropSchema(); | ||
await orm.getSchemaGenerator().createSchema(); | ||
expect(await orm.getSchemaGenerator().getCreateSchemaSQL()).toMatchSnapshot(); | ||
await expect(orm.getSchemaGenerator().getUpdateSchemaSQL({ wrap: false })).resolves.toBe(''); | ||
await orm.close(); | ||
}); | ||
|
||
test('string defaults do not produce additional diffs [mariadb]', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Foo1], | ||
dbName: 'mikro_orm_test_gh_2385', | ||
type: 'mariadb', | ||
port: 3309, | ||
}); | ||
await orm.getSchemaGenerator().ensureDatabase(); | ||
await orm.getSchemaGenerator().dropSchema(); | ||
await orm.getSchemaGenerator().createSchema(); | ||
expect(await orm.getSchemaGenerator().getCreateSchemaSQL()).toMatchSnapshot(); | ||
await expect(orm.getSchemaGenerator().getUpdateSchemaSQL({ wrap: false })).resolves.toBe(''); | ||
await orm.close(); | ||
}); | ||
|
||
test('string defaults do not produce additional diffs [postgres]', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Foo2], | ||
dbName: 'mikro_orm_test_gh_2385', | ||
type: 'postgresql', | ||
}); | ||
await orm.getSchemaGenerator().ensureDatabase(); | ||
await orm.getSchemaGenerator().dropSchema(); | ||
await orm.getSchemaGenerator().createSchema(); | ||
expect(await orm.getSchemaGenerator().getCreateSchemaSQL()).toMatchSnapshot(); | ||
await expect(orm.getSchemaGenerator().getUpdateSchemaSQL({ wrap: false })).resolves.toBe(''); | ||
await orm.close(); | ||
}); | ||
|
||
test('string defaults do not produce additional diffs [sqlite]', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Foo3], | ||
dbName: ':memory:', | ||
type: 'sqlite', | ||
}); | ||
await orm.getSchemaGenerator().createSchema(); | ||
expect(await orm.getSchemaGenerator().getCreateSchemaSQL()).toMatchSnapshot(); | ||
await expect(orm.getSchemaGenerator().getUpdateSchemaSQL({ wrap: false })).resolves.toBe(''); | ||
await orm.close(); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters