-
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(postgres): respect column length in down migrations
Closes #5048
- Loading branch information
Showing
6 changed files
with
254 additions
and
21 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
27 changes: 21 additions & 6 deletions
27
tests/features/schema-generator/__snapshots__/length-diffing.mysql.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 |
---|---|---|
@@ -1,19 +1,34 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`length diffing in mysql schema generator updates column types when length changes (varchar, decimal, ...) 1`] = ` | ||
"alter table \`book\` modify \`id\` bigint unsigned not null auto_increment, modify \`name\` varchar(100) not null, modify \`length\` int unsigned not null, modify \`price\` numeric(16,0) not null; | ||
{ | ||
"down": "alter table \`book\` modify \`id\` int unsigned not null auto_increment, modify \`name\` varchar(255) not null, modify \`length\` int not null, modify \`price\` decimal(10,0) not null, modify \`created_at\` datetime(2) not null; | ||
" | ||
", | ||
"up": "alter table \`book\` modify \`id\` bigint unsigned not null auto_increment, modify \`name\` varchar(100) not null, modify \`length\` int unsigned not null, modify \`price\` numeric(16,0) not null, modify \`created_at\` datetime(3) not null; | ||
", | ||
} | ||
`; | ||
|
||
exports[`length diffing in mysql schema generator updates column types when length changes (varchar, decimal, ...) 2`] = ` | ||
"alter table \`book\` modify \`name\` varchar(150) not null, modify \`price\` numeric(16,4) not null; | ||
{ | ||
"down": "alter table \`book\` modify \`name\` varchar(100) not null, modify \`price\` decimal(16,0) not null; | ||
", | ||
"up": "alter table \`book\` modify \`name\` varchar(150) not null, modify \`price\` numeric(16,4) not null; | ||
" | ||
", | ||
} | ||
`; | ||
|
||
exports[`length diffing in mysql schema generator updates column types when length changes (varchar, decimal, ...) 3`] = ` | ||
"alter table \`book\` modify \`name\` varchar(100) not null; | ||
{ | ||
"down": "alter table \`book\` modify \`name\` varchar(150) not null; | ||
", | ||
"up": "alter table \`book\` modify \`name\` varchar(100) not null; | ||
" | ||
", | ||
} | ||
`; |
42 changes: 42 additions & 0 deletions
42
tests/features/schema-generator/__snapshots__/length-diffing.postgres.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,42 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`length diffing in postgres schema orm.schema updates column types when length changes (varchar, decimal, ...) 1`] = ` | ||
{ | ||
"down": "alter table "book" alter column "id" type int4 using ("id"::int4); | ||
alter table "book" alter column "name" type varchar(255) using ("name"::varchar(255)); | ||
alter table "book" alter column "price" type numeric(10,0) using ("price"::numeric(10,0)); | ||
alter table "book" alter column "created_at" type timestamptz(2) using ("created_at"::timestamptz(2)); | ||
", | ||
"up": "alter table "book" alter column "id" type bigint using ("id"::bigint); | ||
alter table "book" alter column "name" type varchar(100) using ("name"::varchar(100)); | ||
alter table "book" alter column "price" type numeric(16,0) using ("price"::numeric(16,0)); | ||
alter table "book" alter column "created_at" type timestamptz(3) using ("created_at"::timestamptz(3)); | ||
", | ||
} | ||
`; | ||
|
||
exports[`length diffing in postgres schema orm.schema updates column types when length changes (varchar, decimal, ...) 2`] = ` | ||
{ | ||
"down": "alter table "book" alter column "name" type varchar(100) using ("name"::varchar(100)); | ||
alter table "book" alter column "price" type numeric(16,0) using ("price"::numeric(16,0)); | ||
", | ||
"up": "alter table "book" alter column "name" type varchar(150) using ("name"::varchar(150)); | ||
alter table "book" alter column "price" type numeric(16,4) using ("price"::numeric(16,4)); | ||
", | ||
} | ||
`; | ||
|
||
exports[`length diffing in postgres schema orm.schema updates column types when length changes (varchar, decimal, ...) 3`] = ` | ||
{ | ||
"down": "alter table "book" alter column "name" type varchar(150) using ("name"::varchar(150)); | ||
", | ||
"up": "alter table "book" alter column "name" type varchar(100) using ("name"::varchar(100)); | ||
", | ||
} | ||
`; |
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
148 changes: 148 additions & 0 deletions
148
tests/features/schema-generator/length-diffing.postgres.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,148 @@ | ||
import { Entity, PrimaryKey, Property, t } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/postgresql'; | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book0 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
name!: string; | ||
|
||
@Property() | ||
length!: number; | ||
|
||
@Property({ type: t.decimal }) | ||
price!: string; | ||
|
||
@Property({ length: 2 }) | ||
createdAt!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book1 { | ||
|
||
@PrimaryKey({ type: t.bigint }) | ||
id!: string; | ||
|
||
@Property({ length: 100 }) | ||
name!: string; | ||
|
||
@Property({ unsigned: true }) | ||
length!: number; | ||
|
||
@Property({ type: t.decimal, precision: 16 }) | ||
price!: string; | ||
|
||
@Property({ length: 3 }) | ||
createdAt!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book2 { | ||
|
||
@PrimaryKey({ type: t.bigint }) | ||
id!: string; | ||
|
||
@Property({ length: 150 }) | ||
name!: string; | ||
|
||
@Property({ unsigned: true }) | ||
length!: number; | ||
|
||
@Property({ type: t.decimal, precision: 16, scale: 4 }) | ||
price!: number; | ||
|
||
@Property({ length: 3 }) | ||
createdAt!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book3 { | ||
|
||
@PrimaryKey({ type: t.bigint }) | ||
id!: string; | ||
|
||
@Property({ length: 100 }) | ||
name!: string; | ||
|
||
@Property({ unsigned: true }) | ||
length!: number; | ||
|
||
@Property({ columnType: 'decimal(16,4)' }) | ||
price!: number; | ||
|
||
@Property({ length: 3 }) | ||
createdAt!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'book' }) | ||
export class Book4 { | ||
|
||
@PrimaryKey({ type: t.bigint }) | ||
id!: string; | ||
|
||
@Property({ columnType: 'varchar(100)' }) | ||
name!: string; | ||
|
||
@Property({ unsigned: true }) | ||
length!: number; | ||
|
||
@Property({ columnType: 'decimal(16,4)' }) | ||
price!: number; | ||
|
||
@Property({ length: 3 }) | ||
createdAt!: Date; | ||
|
||
} | ||
|
||
describe('length diffing in postgres', () => { | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Book0], | ||
dbName: `mikro_orm_test_length_diffing`, | ||
}); | ||
await orm.schema.ensureDatabase(); | ||
await orm.schema.execute('drop table if exists book'); | ||
await orm.schema.createSchema(); | ||
}); | ||
|
||
afterAll(() => orm.close(true)); | ||
|
||
test('schema orm.schema updates column types when length changes (varchar, decimal, ...)', async () => { | ||
orm.getMetadata().reset('Book0'); | ||
await orm.discoverEntity(Book1); | ||
const diff1 = await orm.schema.getUpdateSchemaMigrationSQL({ wrap: false }); | ||
expect(diff1).toMatchSnapshot(); | ||
await orm.schema.execute(diff1.up); | ||
|
||
orm.getMetadata().reset('Book1'); | ||
await orm.discoverEntity(Book2); | ||
const diff2 = await orm.schema.getUpdateSchemaMigrationSQL({ wrap: false }); | ||
expect(diff2).toMatchSnapshot(); | ||
await orm.schema.execute(diff2.up); | ||
|
||
orm.getMetadata().reset('Book2'); | ||
await orm.discoverEntity(Book3); | ||
const diff3 = await orm.schema.getUpdateSchemaMigrationSQL({ wrap: false }); | ||
expect(diff3).toMatchSnapshot(); | ||
await orm.schema.execute(diff3.up); | ||
|
||
orm.getMetadata().reset('Book3'); | ||
await orm.discoverEntity(Book4); | ||
|
||
await expect(orm.schema.getUpdateSchemaMigrationSQL({ wrap: false })).resolves.toEqual({ | ||
down: '', | ||
up: '', | ||
}); | ||
}); | ||
|
||
}); |