undeexcepturi
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
150 lines (104 loc) • 3.09 kB
text/typescript
import { Entity, PrimaryKey, Property, t } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/mysql';
({ tableName: 'book' })
export class Book0 {
()
id!: number;
()
name!: string;
()
length!: number;
({ type: t.decimal })
price!: string;
({ length: 2 })
createdAt!: Date;
}
({ tableName: 'book' })
export class Book1 {
({ type: t.bigint })
id!: string;
({ length: 100 })
name!: string;
({ unsigned: true })
length!: number;
({ type: t.decimal, precision: 16 })
price!: string;
({ length: 3 })
createdAt!: Date;
}
({ tableName: 'book' })
export class Book2 {
({ type: t.bigint })
id!: string;
({ length: 150 })
name!: string;
({ unsigned: true })
length!: number;
({ type: t.decimal, precision: 16, scale: 4 })
price!: number;
({ length: 3 })
createdAt!: Date;
}
({ tableName: 'book' })
export class Book3 {
({ type: t.bigint })
id!: string;
({ length: 100 })
name!: string;
({ unsigned: true })
length!: number;
({ columnType: 'decimal(16,4)' })
price!: number;
({ length: 3 })
createdAt!: Date;
}
({ tableName: 'book' })
export class Book4 {
({ type: t.bigint })
id!: string;
({ columnType: 'varchar(100)' })
name!: string;
({ unsigned: true })
length!: number;
({ columnType: 'decimal(16,4)' })
price!: number;
({ length: 3 })
createdAt!: Date;
}
describe('length diffing in mysql', () => {
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Book0],
dbName: `mikro_orm_test_length_diffing`,
port: 3308,
});
await orm.schema.ensureDatabase();
await orm.schema.execute('drop table if exists book');
await orm.schema.createSchema();
});
afterAll(() => orm.close(true));
test('schema generator 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: '',
});
});
});