-
-
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(schema): respect length of default value of datetime columns
Closes #4782
- Loading branch information
Showing
3 changed files
with
152 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { MikroORM } from '@mikro-orm/mysql'; | ||
import { Entity, PrimaryKey, Property, t } from '@mikro-orm/core'; | ||
|
||
@Entity({ tableName: 'user' }) | ||
class User0 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'user' }) | ||
class User1 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ defaultRaw: 'current_timestamp(3)', columnType: 'timestamp(3)' }) | ||
bar!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'user' }) | ||
class User2 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ defaultRaw: 'current_timestamp(3)', columnType: 'timestamp(3)' }) | ||
bar!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'user' }) | ||
class User3 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ defaultRaw: 'current_timestamp(6)', columnType: 'timestamp(6)' }) | ||
bar!: Date; | ||
|
||
} | ||
|
||
@Entity({ tableName: 'user' }) | ||
class User4 { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [User0], | ||
dbName: 'mikro_orm_test_gh_4782', | ||
port: 3308, | ||
}); | ||
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. add timestamp(3) column')); | ||
down.push(await testMigration(User1, User2, '2. no changes')); | ||
down.push(await testMigration(User2, User3, '3. change to timestamp(6)')); | ||
down.push(await testMigration(User3, User4, '4. remove timestamp column')); | ||
|
||
for (const sql of down.reverse()) { | ||
await orm.schema.execute(sql); | ||
} | ||
}); |
52 changes: 52 additions & 0 deletions
52
tests/features/schema-generator/__snapshots__/GH4782.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,52 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`4782: 0. create schema 1`] = ` | ||
{ | ||
"down": "drop table if exists \`user\`; | ||
", | ||
"up": "create table \`user\` (\`id\` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB; | ||
", | ||
} | ||
`; | ||
|
||
exports[`4782: 1. add timestamp(3) column 1`] = ` | ||
{ | ||
"down": "alter table \`user\` drop \`bar\`; | ||
", | ||
"up": "alter table \`user\` add \`bar\` timestamp(3) not null default current_timestamp(3); | ||
", | ||
} | ||
`; | ||
|
||
exports[`4782: 2. no changes 1`] = ` | ||
{ | ||
"down": "", | ||
"up": "", | ||
} | ||
`; | ||
|
||
exports[`4782: 3. change to timestamp(6) 1`] = ` | ||
{ | ||
"down": "alter table \`user\` modify \`bar\` timestamp(3) not null default current_timestamp(3); | ||
", | ||
"up": "alter table \`user\` modify \`bar\` timestamp(6) not null default current_timestamp(6); | ||
", | ||
} | ||
`; | ||
|
||
exports[`4782: 4. remove timestamp column 1`] = ` | ||
{ | ||
"down": "alter table \`user\` add \`bar\` timestamp(6) not null default current_timestamp(6); | ||
", | ||
"up": "alter table \`user\` drop \`bar\`; | ||
", | ||
} | ||
`; |