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.
215 lines (156 loc) • 4.91 kB
text/typescript
import { Entity, Ref, Index, ManyToOne, MikroORM, PrimaryKey, Property, Unique } from '@mikro-orm/core';
import { MySqlDriver } from '@mikro-orm/mysql';
export class Author {
id!: number;
name!: string;
}
export class Book1 {
id!: number;
author1!: Ref<Author>;
author2!: Ref<Author>;
author3!: Author;
author4!: Author;
author5!: Author;
title!: string;
isbn!: string;
metaData: any;
}
export class Book2 {
id!: number;
author1!: Ref<Author>;
author2!: Ref<Author>;
author3!: Author;
author4!: Author;
author5!: Author;
title!: string;
isbn!: string;
metaData: any;
}
export class Book3 {
id!: number;
author1!: Ref<Author>;
author2!: Ref<Author>;
author3!: Author;
author4!: Author;
author5!: Author;
title!: string;
isbn!: string;
metaData: any;
}
export class Book4 {
id!: number;
author1!: Ref<Author>;
author2!: Ref<Author>;
author3!: Author;
author4!: Author;
author5!: Author;
title!: string;
isbn!: string;
metaData: any;
}
describe('indexes on FKs in mysql (GH 1518)', () => {
let orm: MikroORM<MySqlDriver>;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Author],
dbName: `mikro_orm_test_gh_1518`,
driver: MySqlDriver,
port: 3308,
});
await orm.schema.ensureDatabase();
await orm.schema.execute('set foreign_key_checks = 0');
await orm.schema.execute('drop table if exists author');
await orm.schema.execute('drop table if exists book');
await orm.schema.execute('set foreign_key_checks = 1');
await orm.schema.createSchema();
});
afterAll(() => orm.close(true));
test('schema generator respect indexes on FKs on column update', async () => {
await orm.discoverEntity(Book1);
orm.getMetadata().reset('Book0');
const diff1 = await orm.schema.getUpdateSchemaSQL({ wrap: false });
expect(diff1).toMatchSnapshot();
await orm.schema.execute(diff1);
orm.getMetadata().reset('Book1');
await orm.discoverEntity(Book2);
const diff2 = await orm.schema.getUpdateSchemaSQL({ wrap: false });
expect(diff2).toMatchSnapshot();
await orm.schema.execute(diff2);
orm.getMetadata().reset('Book2');
await orm.discoverEntity(Book3);
const diff3 = await orm.schema.getUpdateSchemaSQL({ wrap: false });
expect(diff3).toMatchSnapshot();
await orm.schema.execute(diff3);
orm.getMetadata().reset('Book3');
await orm.discoverEntity(Book4);
const diff4 = await orm.schema.getUpdateSchemaSQL({ wrap: false });
expect(diff4).toMatchSnapshot();
await orm.schema.execute(diff4);
});
});