Skip to content

Commit

Permalink
fix(schema): respect length of default value of datetime columns
Browse files Browse the repository at this point in the history
Closes #4782
  • Loading branch information
B4nan committed Oct 4, 2023
1 parent 606d633 commit cbc0c50
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/knex/src/schema/DatabaseTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ export class DatabaseTable {

/* istanbul ignore else */
if (match) {
prop.precision = +match[1];
prop.scale = +match[2];
prop.precision ??= +match[1];
prop.scale ??= +match[2];
prop.length = undefined;
}
}

if (mappedType instanceof DateTimeType) {
prop.length ??= this.platform.getDefaultDateTimeLength();
const match = prop.columnTypes[idx].match(/\w+\((\d+)\)/);

if (match) {
prop.length ??= +match[1];
} else {
prop.length ??= this.platform.getDefaultDateTimeLength();
}
}

const primary = !meta.compositePK && !!prop.primary && prop.reference === ReferenceType.SCALAR && this.platform.isNumericColumn(mappedType);
Expand Down
91 changes: 91 additions & 0 deletions tests/features/schema-generator/GH4782.test.ts
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 tests/features/schema-generator/__snapshots__/GH4782.test.ts.snap
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\`;
",
}
`;

0 comments on commit cbc0c50

Please sign in to comment.