-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(schema): respect up migration when detecting column renaming in d…
…own migration Closes #4919
- Loading branch information
Showing
4 changed files
with
106 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { MikroORM } from '@mikro-orm/postgresql'; | ||
import { Entity, PrimaryKey, Property } from '@mikro-orm/core'; | ||
|
||
@Entity({ tableName: 'user' }) | ||
class User0 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
deliveredAt!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'user' }) | ||
class User1 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
completedAt!: Date; | ||
|
||
@Property() | ||
canceledAt!: Date; | ||
|
||
@Property() | ||
arrivedAt!: Date; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [User0], | ||
dbName: 'mikro_orm_test_gh_4919', | ||
}); | ||
await orm.schema.ensureDatabase(); | ||
await orm.schema.dropSchema(); | ||
}); | ||
|
||
afterAll(() => orm.close(true)); | ||
|
||
test('4782', async () => { | ||
const testMigration = async (e1: any, e2: any, snap: string) => { | ||
if (e2) { | ||
orm.getMetadata().reset(e1.name); | ||
await orm.discoverEntity(e2); | ||
} | ||
|
||
const diff = await orm.schema.getUpdateSchemaMigrationSQL({ wrap: false }); | ||
expect(diff).toMatchSnapshot(snap); | ||
await orm.schema.execute(diff.up); | ||
|
||
return diff.down; | ||
}; | ||
|
||
const down: string[] = []; | ||
down.push(await testMigration(User0, undefined, '0. create schema')); | ||
down.push(await testMigration(User0, User1, '1. rename column')); | ||
|
||
for (const sql of down.reverse()) { | ||
await orm.schema.execute(sql); | ||
} | ||
}); |
27 changes: 27 additions & 0 deletions
27
tests/features/schema-generator/__snapshots__/GH4919.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,27 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`4782: 0. create schema 1`] = ` | ||
{ | ||
"down": "drop table if exists "user" cascade; | ||
", | ||
"up": "create table "user" ("id" serial primary key, "delivered_at" timestamptz not null); | ||
", | ||
} | ||
`; | ||
|
||
exports[`4782: 1. rename column 1`] = ` | ||
{ | ||
"down": "alter table "user" drop column "canceled_at"; | ||
alter table "user" drop column "arrived_at"; | ||
alter table "user" rename column "completed_at" to "delivered_at"; | ||
", | ||
"up": "alter table "user" add column "canceled_at" timestamptz not null, add column "arrived_at" timestamptz not null; | ||
alter table "user" rename column "delivered_at" to "completed_at"; | ||
", | ||
} | ||
`; |