From eba7bd32384a4bbc2fcd8339de3915ca4ba41013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Mon, 23 Aug 2021 20:36:20 +0200 Subject: [PATCH] feat(sql): generate down migrations automatically Currently not supported in sqlite driver, due to how knex does complex schema diffing in sqlite (always based on current schema, which is not "in sync", as we generate the down migration in the same time as up one, which is not yet executed). --- .../src/commands/MigrationCommandFactory.ts | 13 +- packages/core/src/platforms/Platform.ts | 7 + packages/core/src/typings.ts | 1 + packages/knex/src/schema/SchemaComparator.ts | 6 +- packages/knex/src/schema/SchemaGenerator.ts | 38 +- packages/knex/src/schema/SchemaHelper.ts | 4 +- packages/migrations/src/MigrationGenerator.ts | 25 +- packages/migrations/src/Migrator.ts | 34 +- packages/migrations/src/typings.ts | 2 +- .../postgresql/src/PostgreSqlConnection.ts | 26 +- .../postgresql/src/PostgreSqlSchemaHelper.ts | 13 +- packages/sqlite/src/SqlitePlatform.ts | 4 + tests/bootstrap.ts | 1 + .../cli/CreateMigrationCommand.test.ts | 4 +- .../migrations/Migrator.postgres.test.ts | 6 +- .../migrations/Migrator.sqlite.test.ts | 322 ++++++++ tests/features/migrations/Migrator.test.ts | 5 +- .../Migrator.postgres.test.ts.snap | 511 +++++++----- .../Migrator.sqlite.test.ts.snap | 349 ++++++++ .../__snapshots__/Migrator.test.ts.snap | 759 ++++++++++-------- .../SchemaGenerator.postgres.test.ts.snap | 2 +- .../adding-composite-fk.postgres.test.ts.snap | 39 +- .../adding-fk-column.sqlite.test.ts.snap | 5 +- .../changing-pk-type.mysql.test.ts.snap | 58 +- .../changing-pk-type.postgres.test.ts.snap | 82 +- .../adding-composite-fk.postgres.test.ts | 12 +- .../adding-fk-column.sqlite.test.ts | 6 +- .../changing-pk-type.mysql.test.ts | 23 +- .../changing-pk-type.postgres.test.ts | 29 +- 29 files changed, 1710 insertions(+), 676 deletions(-) create mode 100644 tests/features/migrations/Migrator.sqlite.test.ts create mode 100644 tests/features/migrations/__snapshots__/Migrator.sqlite.test.ts.snap diff --git a/packages/cli/src/commands/MigrationCommandFactory.ts b/packages/cli/src/commands/MigrationCommandFactory.ts index 27da599c26c8..009e4a4360df 100644 --- a/packages/cli/src/commands/MigrationCommandFactory.ts +++ b/packages/cli/src/commands/MigrationCommandFactory.ts @@ -145,13 +145,22 @@ export class MigrationCommandFactory { private static async handleCreateCommand(migrator: Migrator, args: Arguments, config: Configuration): Promise { const ret = await migrator.createMigration(args.path, args.blank, args.initial); - if (ret.diff.length === 0) { + if (ret.diff.up.length === 0) { return CLIHelper.dump(c.green(`No changes required, schema is up-to-date`)); } if (args.dump) { CLIHelper.dump(c.green('Creating migration with following queries:')); - CLIHelper.dump(ret.diff.map(sql => ' ' + sql).join('\n'), config); + CLIHelper.dump(c.green('up:')); + CLIHelper.dump(ret.diff.up.map(sql => ' ' + sql).join('\n'), config); + + /* istanbul ignore else */ + if (config.getDriver().getPlatform().supportsDownMigrations()) { + CLIHelper.dump(c.green('down:')); + CLIHelper.dump(ret.diff.down.map(sql => ' ' + sql).join('\n'), config); + } else { + CLIHelper.dump(c.yellow(`(${config.getDriver().constructor.name} does not support automatic down migrations)`)); + } } CLIHelper.dump(c.green(`${ret.fileName} successfully created`)); diff --git a/packages/core/src/platforms/Platform.ts b/packages/core/src/platforms/Platform.ts index 042cfa00e4d2..e029f2f50943 100644 --- a/packages/core/src/platforms/Platform.ts +++ b/packages/core/src/platforms/Platform.ts @@ -369,4 +369,11 @@ export abstract class Platform { return [ReferenceType.SCALAR, ReferenceType.MANY_TO_ONE].includes(prop.reference) || (prop.reference === ReferenceType.ONE_TO_ONE && prop.owner); } + /** + * Currently not supported due to how knex does complex sqlite diffing (always based on current schema) + */ + supportsDownMigrations(): boolean { + return true; + } + } diff --git a/packages/core/src/typings.ts b/packages/core/src/typings.ts index c15c0f854249..6e4227b217eb 100644 --- a/packages/core/src/typings.ts +++ b/packages/core/src/typings.ts @@ -371,6 +371,7 @@ export interface ISchemaGenerator { getDropSchemaSQL(options?: { wrap?: boolean; dropMigrationsTable?: boolean }): Promise; updateSchema(options?: { wrap?: boolean; safe?: boolean; dropDb?: boolean; dropTables?: boolean }): Promise; getUpdateSchemaSQL(options?: { wrap?: boolean; safe?: boolean; dropDb?: boolean; dropTables?: boolean }): Promise; + getUpdateSchemaMigrationSQL(options?: { wrap?: boolean; safe?: boolean; dropDb?: boolean; dropTables?: boolean }): Promise<{ up: string; down: string }>; createDatabase(name: string): Promise; dropDatabase(name: string): Promise; execute(sql: string, options?: { wrap?: boolean }): Promise; diff --git a/packages/knex/src/schema/SchemaComparator.ts b/packages/knex/src/schema/SchemaComparator.ts index f151e59fd8f1..8cd368a3e77b 100644 --- a/packages/knex/src/schema/SchemaComparator.ts +++ b/packages/knex/src/schema/SchemaComparator.ts @@ -425,8 +425,10 @@ export class SchemaComparator { } if (to.mappedType instanceof BooleanType) { - const defaultValue = !['0', 'false', 'f', 'n', 'no', 'off'].includes(from.default!); - return '' + defaultValue === to.default; + const defaultValueFrom = !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + from.default!); + const defaultValueTo = !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + to.default!); + + return defaultValueFrom === defaultValueTo; } if (from.default && to.default) { diff --git a/packages/knex/src/schema/SchemaGenerator.ts b/packages/knex/src/schema/SchemaGenerator.ts index 5bca8818dda4..b9635e3f198a 100644 --- a/packages/knex/src/schema/SchemaGenerator.ts +++ b/packages/knex/src/schema/SchemaGenerator.ts @@ -1,6 +1,6 @@ import { Knex } from 'knex'; import { CommitOrderCalculator, Dictionary, EntityMetadata } from '@mikro-orm/core'; -import { Column, ForeignKey, Index, TableDifference } from '../typings'; +import { Column, ForeignKey, Index, SchemaDifference, TableDifference } from '../typings'; import { DatabaseSchema } from './DatabaseSchema'; import { DatabaseTable } from './DatabaseTable'; import { SqlEntityManager } from '../SqlEntityManager'; @@ -87,11 +87,11 @@ export class SchemaGenerator { let ret = ''; for (const meta of metadata) { - ret += await this.dump(this.dropTable(meta.collection), '\n'); + ret += await this.dump(this.dropTable(meta.collection, meta.schema), '\n'); } if (options.dropMigrationsTable) { - ret += await this.dump(this.dropTable(this.config.get('migrations').tableName!), '\n'); + ret += await this.dump(this.dropTable(this.config.get('migrations').tableName!, this.config.get('schema')), '\n'); } return this.wrapSchema(ret + '\n', { wrap }); @@ -103,13 +103,35 @@ export class SchemaGenerator { } async getUpdateSchemaSQL(options: { wrap?: boolean; safe?: boolean; dropTables?: boolean; fromSchema?: DatabaseSchema } = {}): Promise { - const wrap = options.wrap ?? true; + options.wrap = options.wrap ?? true; options.safe = options.safe ?? false; options.dropTables = options.dropTables ?? true; const toSchema = this.getTargetSchema(); + /* istanbul ignore next */ const fromSchema = options.fromSchema ?? await DatabaseSchema.create(this.connection, this.platform, this.config); const comparator = new SchemaComparator(this.platform); - const schemaDiff = comparator.compare(fromSchema, toSchema); + const diffUp = comparator.compare(fromSchema, toSchema); + + return this.diffToSQL(diffUp, options); + } + + async getUpdateSchemaMigrationSQL(options: { wrap?: boolean; safe?: boolean; dropTables?: boolean; fromSchema?: DatabaseSchema } = {}): Promise<{ up: string; down: string }> { + options.wrap = options.wrap ?? true; + options.safe = options.safe ?? false; + options.dropTables = options.dropTables ?? true; + const toSchema = this.getTargetSchema(); + const fromSchema = options.fromSchema ?? await DatabaseSchema.create(this.connection, this.platform, this.config); + const comparator = new SchemaComparator(this.platform); + const diffUp = comparator.compare(fromSchema, toSchema); + const diffDown = comparator.compare(toSchema, fromSchema); + + return { + up: await this.diffToSQL(diffUp, options), + down: this.platform.supportsDownMigrations() ? await this.diffToSQL(diffDown, options) : '', + }; + } + + async diffToSQL(schemaDiff: SchemaDifference, options: { wrap?: boolean; safe?: boolean; dropTables?: boolean }): Promise { let ret = ''; if (this.platform.supportsSchemas()) { @@ -139,13 +161,13 @@ export class SchemaGenerator { } for (const changedTable of Object.values(schemaDiff.changedTables)) { - for (const builder of this.preAlterTable(changedTable, options.safe)) { + for (const builder of this.preAlterTable(changedTable, options.safe!)) { ret += await this.dump(builder); } } for (const changedTable of Object.values(schemaDiff.changedTables)) { - for (const builder of this.alterTable(changedTable, options.safe)) { + for (const builder of this.alterTable(changedTable, options.safe!)) { ret += await this.dump(builder); } } @@ -156,7 +178,7 @@ export class SchemaGenerator { } } - return this.wrapSchema(ret, { wrap }); + return this.wrapSchema(ret, options); } private createForeignKey(table: Knex.CreateTableBuilder, foreignKey: ForeignKey) { diff --git a/packages/knex/src/schema/SchemaHelper.ts b/packages/knex/src/schema/SchemaHelper.ts index e9ff30433505..d111d18d7648 100644 --- a/packages/knex/src/schema/SchemaHelper.ts +++ b/packages/knex/src/schema/SchemaHelper.ts @@ -109,9 +109,9 @@ export abstract class SchemaHelper { const guard = (key: string) => !changedProperties || changedProperties.has(key); if (changedProperties) { - Utils.runIfNotEmpty(() => col.defaultTo(column.default === undefined ? null : knex.raw(column.default)), guard('default')); + Utils.runIfNotEmpty(() => col.defaultTo(column.default == null ? null : knex.raw(column.default)), guard('default')); } else { - Utils.runIfNotEmpty(() => col.defaultTo(knex.raw(column.default!)), column.default !== undefined); + Utils.runIfNotEmpty(() => col.defaultTo(column.default == null ? null : knex.raw(column.default)), column.default !== undefined); } return col; diff --git a/packages/migrations/src/MigrationGenerator.ts b/packages/migrations/src/MigrationGenerator.ts index 06018d696447..3fe3e7cf5494 100644 --- a/packages/migrations/src/MigrationGenerator.ts +++ b/packages/migrations/src/MigrationGenerator.ts @@ -8,7 +8,7 @@ export class MigrationGenerator { protected readonly namingStrategy: NamingStrategy, protected readonly options: MigrationsOptions) { } - async generate(diff: string[], path?: string): Promise<[string, string]> { + async generate(diff: { up: string[]; down: string[] }, path?: string): Promise<[string, string]> { path = Utils.normalizePath(path || this.options.path!); await ensureDir(path); const timestamp = new Date().toISOString().replace(/[-T:]|\.\d{3}z$/ig, ''); @@ -36,26 +36,41 @@ export class MigrationGenerator { return '\n'; } - generateJSMigrationFile(className: string, diff: string[]): string { + generateJSMigrationFile(className: string, diff: { up: string[]; down: string[] }): string { let ret = `'use strict';\n`; ret += `Object.defineProperty(exports, '__esModule', { value: true });\n`; ret += `const Migration = require('@mikro-orm/migrations').Migration;\n\n`; ret += `class ${className} extends Migration {\n\n`; ret += ` async up() {\n`; - diff.forEach(sql => ret += this.createStatement(sql, 4)); + diff.up.forEach(sql => ret += this.createStatement(sql, 4)); ret += ` }\n\n`; + + /* istanbul ignore else */ + if (diff.down.length > 0) { + ret += ` async down() {\n`; + diff.down.forEach(sql => ret += this.createStatement(sql, 4)); + ret += ` }\n\n`; + } + ret += `}\n`; ret += `exports.${className} = ${className};\n`; return ret; } - generateTSMigrationFile(className: string, diff: string[]): string { + generateTSMigrationFile(className: string, diff: { up: string[]; down: string[] }): string { let ret = `import { Migration } from '@mikro-orm/migrations';\n\n`; ret += `export class ${className} extends Migration {\n\n`; ret += ` async up(): Promise {\n`; - diff.forEach(sql => ret += this.createStatement(sql, 4)); + diff.up.forEach(sql => ret += this.createStatement(sql, 4)); ret += ` }\n\n`; + + if (diff.down.length > 0) { + ret += ` async down(): Promise {\n`; + diff.down.forEach(sql => ret += this.createStatement(sql, 4)); + ret += ` }\n\n`; + } + ret += `}\n`; return ret; diff --git a/packages/migrations/src/Migrator.ts b/packages/migrations/src/Migrator.ts index b17f83cc399c..bbdbfc63252c 100644 --- a/packages/migrations/src/Migrator.ts +++ b/packages/migrations/src/Migrator.ts @@ -49,7 +49,7 @@ export class Migrator { await this.ensureMigrationsDirExists(); const diff = await this.getSchemaDiff(blank, initial); - if (diff.length === 0) { + if (diff.up.length === 0) { return { fileName: '', code: '', diff }; } @@ -200,33 +200,39 @@ export class Migrator { }; } - private async getSchemaDiff(blank: boolean, initial: boolean): Promise { - const lines: string[] = []; + private async getSchemaDiff(blank: boolean, initial: boolean): Promise<{ up: string[]; down: string[] }> { + const up: string[] = []; + const down: string[] = []; if (blank) { - lines.push('select 1'); + up.push('select 1'); } else if (initial) { const dump = await this.schemaGenerator.getCreateSchemaSQL({ wrap: false }); - lines.push(...dump.split('\n')); + up.push(...dump.split('\n')); } else { - const dump = await this.schemaGenerator.getUpdateSchemaSQL({ + const diff = await this.schemaGenerator.getUpdateSchemaMigrationSQL({ wrap: false, safe: this.options.safe, dropTables: this.options.dropTables, fromSchema: await this.getCurrentSchema(), }); - lines.push(...dump.split('\n')); + up.push(...diff.up.split('\n')); + down.push(...diff.down.split('\n')); } - for (let i = lines.length - 1; i >= 0; i--) { - if (lines[i]) { - break; - } + const cleanUp = (diff: string[]) => { + for (let i = diff.length - 1; i >= 0; i--) { + if (diff[i]) { + break; + } - lines.splice(i, 1); - } + diff.splice(i, 1); + } + }; + cleanUp(up); + cleanUp(down); - return lines; + return { up, down }; } private prefix(options?: T): T { diff --git a/packages/migrations/src/typings.ts b/packages/migrations/src/typings.ts index 4519096b1e10..8639c62b5f18 100644 --- a/packages/migrations/src/typings.ts +++ b/packages/migrations/src/typings.ts @@ -2,5 +2,5 @@ import { Transaction } from '@mikro-orm/core'; export type UmzugMigration = { name?: string; path?: string; file: string }; export type MigrateOptions = { from?: string | number; to?: string | number; migrations?: string[]; transaction?: Transaction }; -export type MigrationResult = { fileName: string; code: string; diff: string[] }; +export type MigrationResult = { fileName: string; code: string; diff: { up: string[]; down: string[] } }; export type MigrationRow = { name: string; executed_at: Date }; diff --git a/packages/postgresql/src/PostgreSqlConnection.ts b/packages/postgresql/src/PostgreSqlConnection.ts index 934f7b456279..e24ac872821e 100644 --- a/packages/postgresql/src/PostgreSqlConnection.ts +++ b/packages/postgresql/src/PostgreSqlConnection.ts @@ -68,15 +68,20 @@ export class PostgreSqlConnection extends AbstractSqlConnection { const colName = this.client.wrapIdentifier(col.getColumnName(), col.columnBuilder.queryContext()); const constraintName = `${this.tableNameRaw}_${col.getColumnName()}_check`; this.pushQuery({ sql: `alter table ${quotedTableName} drop constraint if exists "${constraintName}"`, bindings: [] }); + that.dropColumnDefault.call(this, col, colName); if (col.type === 'enu') { this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} type text using (${colName}::text)`, bindings: [] }); this.pushQuery({ sql: `alter table ${quotedTableName} add constraint "${constraintName}" ${type.replace(/^text /, '')}`, bindings: [] }); + } else if (type === 'uuid') { + // we need to drop the default as it would be invalid + this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} drop default`, bindings: [] }); + this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} type ${type} using (${colName}::text::uuid)`, bindings: [] }); } else { this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} type ${type} using (${colName}::${type})`, bindings: [] }); } - that.alterColumnDefault.call(this, col, colName); + that.addColumnDefault.call(this, col, colName); that.alterColumnNullable.call(this, col, colName); } @@ -95,7 +100,7 @@ export class PostgreSqlConnection extends AbstractSqlConnection { } } - private alterColumnDefault(this: any, col: Dictionary, colName: string): void { + private addColumnDefault(this: any, col: Dictionary, colName: string): void { const quotedTableName = this.tableName(); const defaultTo = col.modified.defaultTo; @@ -103,12 +108,23 @@ export class PostgreSqlConnection extends AbstractSqlConnection { return; } - if (defaultTo[0] === null) { - this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} drop default`, bindings: [] }); - } else { + if (defaultTo[0] !== null) { const modifier = col.defaultTo(...defaultTo); this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} set ${modifier}`, bindings: [] }); } } + private dropColumnDefault(this: any, col: Dictionary, colName: string): void { + const quotedTableName = this.tableName(); + const defaultTo = col.modified.defaultTo; + + if (!defaultTo) { + return; + } + + if (defaultTo[0] === null) { + this.pushQuery({ sql: `alter table ${quotedTableName} alter column ${colName} drop default`, bindings: [] }); + } + } + } diff --git a/packages/postgresql/src/PostgreSqlSchemaHelper.ts b/packages/postgresql/src/PostgreSqlSchemaHelper.ts index f07abae1e39a..4dd5f62fdecc 100644 --- a/packages/postgresql/src/PostgreSqlSchemaHelper.ts +++ b/packages/postgresql/src/PostgreSqlSchemaHelper.ts @@ -1,5 +1,5 @@ import { BigIntType, Dictionary, EnumType, Utils } from '@mikro-orm/core'; -import { AbstractSqlConnection, SchemaHelper, Column, Index, DatabaseTable } from '@mikro-orm/knex'; +import { AbstractSqlConnection, SchemaHelper, Column, Index, DatabaseTable, TableDifference } from '@mikro-orm/knex'; import { Knex } from 'knex'; export class PostgreSqlSchemaHelper extends SchemaHelper { @@ -159,6 +159,17 @@ export class PostgreSqlSchemaHelper extends SchemaHelper { return col; } + getPreAlterTable(tableDiff: TableDifference, safe: boolean): string { + // changing uuid column type requires to cast it to text first + const uuid = Object.values(tableDiff.changedColumns).find(col => col.changedProperties.has('type') && col.fromColumn.type === 'uuid'); + + if (!uuid) { + return ''; + } + + return `alter table "${tableDiff.name}" alter column "${uuid.column.name}" type text using ("${uuid.column.name}"::text)`; + } + getAlterColumnAutoincrement(tableName: string, column: Column): string { const ret: string[] = []; const quoted = (val: string) => this.platform.quoteIdentifier(val); diff --git a/packages/sqlite/src/SqlitePlatform.ts b/packages/sqlite/src/SqlitePlatform.ts index 00eca9c6492c..209f3b8c28d4 100644 --- a/packages/sqlite/src/SqlitePlatform.ts +++ b/packages/sqlite/src/SqlitePlatform.ts @@ -119,4 +119,8 @@ export class SqlitePlatform extends AbstractSqlPlatform { return super.getIndexName(tableName, columns, type); } + supportsDownMigrations(): boolean { + return false; + } + } diff --git a/tests/bootstrap.ts b/tests/bootstrap.ts index ec9174538090..f755167f34c7 100644 --- a/tests/bootstrap.ts +++ b/tests/bootstrap.ts @@ -148,6 +148,7 @@ export async function initORMSqlite2() { forceUndefined: true, logger: i => i, cache: { pretty: true }, + migrations: { path: BASE_DIR + '/../temp/migrations', snapshot: false }, }); const schemaGenerator = new SchemaGenerator(orm.em); await schemaGenerator.dropSchema(); diff --git a/tests/features/cli/CreateMigrationCommand.test.ts b/tests/features/cli/CreateMigrationCommand.test.ts index b88f6bf809a1..4dd286008aa5 100644 --- a/tests/features/cli/CreateMigrationCommand.test.ts +++ b/tests/features/cli/CreateMigrationCommand.test.ts @@ -11,7 +11,7 @@ const close = jest.fn(); jest.spyOn(MikroORM.prototype, 'close').mockImplementation(close); jest.spyOn(require('yargs'), 'showHelp').mockReturnValue(''); const createMigrationMock = jest.spyOn(Migrator.prototype, 'createMigration'); -createMigrationMock.mockResolvedValue({ fileName: '1', code: '2', diff: ['3'] }); +createMigrationMock.mockResolvedValue({ fileName: '1', code: '2', diff: { up: ['3'], down: [] } }); const dumpMock = jest.spyOn(CLIHelper, 'dump'); dumpMock.mockImplementation(() => void 0); @@ -45,7 +45,7 @@ describe('CreateMigrationCommand', () => { expect(close.mock.calls.length).toBe(2); expect(dumpMock).toHaveBeenLastCalledWith('1 successfully created'); - createMigrationMock.mockImplementationOnce(async () => ({ fileName: '', code: '', diff: [] })); + createMigrationMock.mockImplementationOnce(async () => ({ fileName: '', code: '', diff: { up: [], down: [] } })); await expect(cmd.handler({} as any)).resolves.toBeUndefined(); expect(createMigrationMock.mock.calls.length).toBe(3); expect(close.mock.calls.length).toBe(3); diff --git a/tests/features/migrations/Migrator.postgres.test.ts b/tests/features/migrations/Migrator.postgres.test.ts index bf0827824de8..f49dfce1fe0b 100644 --- a/tests/features/migrations/Migrator.postgres.test.ts +++ b/tests/features/migrations/Migrator.postgres.test.ts @@ -99,7 +99,7 @@ describe('Migrator (postgres)', () => { // will use the snapshot, so should be empty const migration2 = await migrator.createMigration(); - expect(migration2.diff).toEqual([]); + expect(migration2.diff).toEqual({ down: [], up: [] }); expect(migration2).toMatchSnapshot('migration-snapshot-dump-2'); migrations.snapshot = false; @@ -153,9 +153,9 @@ describe('Migrator (postgres)', () => { test('migration is skipped when no diff', async () => { const migrator = new Migrator(orm.em); const getSchemaDiffMock = jest.spyOn(Migrator.prototype, 'getSchemaDiff'); - getSchemaDiffMock.mockResolvedValueOnce([]); + getSchemaDiffMock.mockResolvedValueOnce({ up: [], down: [] }); const migration = await migrator.createMigration(); - expect(migration).toEqual({ fileName: '', code: '', diff: [] }); + expect(migration).toEqual({ fileName: '', code: '', diff: { up: [], down: [] } }); }); test('run schema migration', async () => { diff --git a/tests/features/migrations/Migrator.sqlite.test.ts b/tests/features/migrations/Migrator.sqlite.test.ts new file mode 100644 index 000000000000..380c749a5255 --- /dev/null +++ b/tests/features/migrations/Migrator.sqlite.test.ts @@ -0,0 +1,322 @@ +(global as any).process.env.FORCE_COLOR = 0; +import umzug from 'umzug'; +import { Logger, MetadataStorage, MikroORM } from '@mikro-orm/core'; +import { Migration, MigrationStorage, Migrator } from '@mikro-orm/migrations'; +import { DatabaseSchema, DatabaseTable, SqliteDriver } from '@mikro-orm/sqlite'; +import { remove } from 'fs-extra'; +import { initORMSqlite2 } from '../../bootstrap'; + +class MigrationTest1 extends Migration { + + async up(): Promise { + this.addSql('select 1 + 1'); + } + +} + +class MigrationTest2 extends Migration { + + async up(): Promise { + this.addSql('select 1 + 1'); + const knex = this.getKnex(); + this.addSql(knex.raw('select 1 + 1')); + this.addSql(knex.select(knex.raw('2 + 2 as count2'))); + const res = await this.execute('select 1 + 1 as count1'); + expect(res).toEqual([{ count1: 2 }]); + } + + isTransactional(): boolean { + return false; + } + +} + +describe('Migrator (sqlite)', () => { + + let orm: MikroORM; + + beforeAll(async () => { + orm = await initORMSqlite2(); + await remove(process.cwd() + '/temp/migrations'); + }); + afterAll(async () => orm.close(true)); + + test('generate js schema migration', async () => { + const dateMock = jest.spyOn(Date.prototype, 'toISOString'); + dateMock.mockReturnValue('2019-10-13T21:48:13.382Z'); + const migrationsSettings = orm.config.get('migrations'); + orm.config.set('migrations', { ...migrationsSettings, emit: 'js' }); // Set migration type to js + const migrator = orm.getMigrator(); + const migration = await migrator.createMigration(); + expect(migration).toMatchSnapshot('migration-js-dump'); + orm.config.set('migrations', migrationsSettings); // Revert migration config changes + await remove(process.cwd() + '/temp/migrations/' + migration.fileName); + }); + + test('generate migration with custom name', async () => { + const dateMock = jest.spyOn(Date.prototype, 'toISOString'); + dateMock.mockReturnValue('2019-10-13T21:48:13.382Z'); + const migrationsSettings = orm.config.get('migrations'); + orm.config.set('migrations', { ...migrationsSettings, fileName: time => `migration-${time}` }); + const migrator = orm.getMigrator(); + const migration = await migrator.createMigration(); + expect(migration).toMatchSnapshot('migration-dump'); + const upMock = jest.spyOn(umzug.prototype, 'up'); + upMock.mockImplementation(() => void 0); + const downMock = jest.spyOn(umzug.prototype, 'down'); + downMock.mockImplementation(() => void 0); + await migrator.up(); + await migrator.down(migration.fileName.replace('.ts', '')); + await migrator.up(); + await migrator.down(migration.fileName); + await migrator.up(); + await migrator.down(migration.fileName.replace('migration-', '').replace('.ts', '')); + orm.config.set('migrations', migrationsSettings); // Revert migration config changes + await remove(process.cwd() + '/temp/migrations/' + migration.fileName); + upMock.mockRestore(); + downMock.mockRestore(); + }); + + test('generate schema migration', async () => { + const dateMock = jest.spyOn(Date.prototype, 'toISOString'); + dateMock.mockReturnValue('2019-10-13T21:48:13.382Z'); + const migrator = new Migrator(orm.em); + const migration = await migrator.createMigration(); + expect(migration).toMatchSnapshot('migration-dump'); + await remove(process.cwd() + '/temp/migrations/' + migration.fileName); + }); + + test('generate migration with snapshot', async () => { + const migrations = orm.config.get('migrations'); + migrations.snapshot = true; + + const dateMock = jest.spyOn(Date.prototype, 'toISOString'); + dateMock.mockReturnValue('2019-10-13T21:48:13.382Z'); + const migrator = new Migrator(orm.em); + const migration1 = await migrator.createMigration(); + expect(migration1).toMatchSnapshot('migration-snapshot-dump-1'); + await remove(process.cwd() + '/temp/migrations/' + migration1.fileName); + + // will use the snapshot, so should be empty + const migration2 = await migrator.createMigration(); + expect(migration2.diff).toEqual({ down: [], up: [] }); + expect(migration2).toMatchSnapshot('migration-snapshot-dump-2'); + + migrations.snapshot = false; + }); + + test('generate initial migration', async () => { + await orm.em.getKnex().schema.dropTableIfExists(orm.config.get('migrations').tableName!); + const getExecutedMigrationsMock = jest.spyOn(Migrator.prototype, 'getExecutedMigrations'); + const getPendingMigrationsMock = jest.spyOn(Migrator.prototype, 'getPendingMigrations'); + getExecutedMigrationsMock.mockResolvedValueOnce(['test.ts']); + const migrator = new Migrator(orm.em); + const err = 'Initial migration cannot be created, as some migrations already exist'; + await expect(migrator.createMigration(undefined, false, true)).rejects.toThrowError(err); + + getExecutedMigrationsMock.mockResolvedValueOnce([]); + const logMigrationMock = jest.spyOn(MigrationStorage.prototype, 'logMigration'); + logMigrationMock.mockImplementationOnce(i => i); + const dateMock = jest.spyOn(Date.prototype, 'toISOString'); + dateMock.mockReturnValue('2019-10-13T21:48:13.382Z'); + + const metadataMock = jest.spyOn(MetadataStorage.prototype, 'getAll'); + const schemaMock = jest.spyOn(DatabaseSchema.prototype, 'getTables'); + schemaMock.mockReturnValueOnce([{ name: 'author4' } as DatabaseTable, { name: 'book4' } as DatabaseTable]); + getPendingMigrationsMock.mockResolvedValueOnce([]); + const err2 = `Some tables already exist in your schema, remove them first to create the initial migration: author4, book4`; + await expect(migrator.createInitialMigration(undefined)).rejects.toThrowError(err2); + + metadataMock.mockReturnValueOnce({}); + const err3 = `No entities found`; + await expect(migrator.createInitialMigration(undefined)).rejects.toThrowError(err3); + + schemaMock.mockReturnValueOnce([]); + getPendingMigrationsMock.mockResolvedValueOnce([]); + const migration1 = await migrator.createInitialMigration(undefined); + expect(logMigrationMock).not.toBeCalledWith('Migration20191013214813.ts'); + expect(migration1).toMatchSnapshot('initial-migration-dump'); + await remove(process.cwd() + '/temp/migrations/' + migration1.fileName); + + await orm.em.getKnex().schema.dropTableIfExists(orm.config.get('migrations').tableName!); + const migration2 = await migrator.createInitialMigration(undefined); + expect(logMigrationMock).toBeCalledWith('Migration20191013214813.ts'); + expect(migration2).toMatchSnapshot('initial-migration-dump'); + await remove(process.cwd() + '/temp/migrations/' + migration2.fileName); + }); + + test('migration storage getter', async () => { + const migrator = new Migrator(orm.em); + expect(migrator.getStorage()).toBeInstanceOf(MigrationStorage); + }); + + test('migration is skipped when no diff', async () => { + const migrator = new Migrator(orm.em); + const getSchemaDiffMock = jest.spyOn(Migrator.prototype, 'getSchemaDiff'); + getSchemaDiffMock.mockResolvedValueOnce({ up: [], down: [] }); + const migration = await migrator.createMigration(); + expect(migration).toEqual({ fileName: '', code: '', diff: { up: [], down: [] } }); + }); + + test('run schema migration', async () => { + const upMock = jest.spyOn(umzug.prototype, 'up'); + const downMock = jest.spyOn(umzug.prototype, 'down'); + upMock.mockImplementationOnce(() => void 0); + downMock.mockImplementationOnce(() => void 0); + const migrator = new Migrator(orm.em); + await migrator.up(); + expect(upMock).toBeCalledTimes(1); + expect(downMock).toBeCalledTimes(0); + await migrator.down(); + expect(upMock).toBeCalledTimes(1); + expect(downMock).toBeCalledTimes(1); + upMock.mockRestore(); + downMock.mockRestore(); + }); + + test('run schema migration without existing migrations folder (GH #907)', async () => { + await remove(process.cwd() + '/temp/migrations'); + const migrator = new Migrator(orm.em); + await migrator.up(); + }); + + test('ensureTable and list executed migrations', async () => { + await orm.em.getKnex().schema.dropTableIfExists(orm.config.get('migrations').tableName!); + const migrator = new Migrator(orm.em); + // @ts-ignore + const storage = migrator.storage; + + await storage.ensureTable(); // creates the table + await storage.logMigration('test'); + await expect(storage.getExecutedMigrations()).resolves.toMatchObject([{ name: 'test' }]); + await expect(storage.executed()).resolves.toEqual(['test']); + + await storage.ensureTable(); // table exists, no-op + await storage.unlogMigration('test'); + await expect(storage.executed()).resolves.toEqual([]); + + await expect(migrator.getPendingMigrations()).resolves.toEqual([]); + }); + + test('runner', async () => { + await orm.em.getKnex().schema.dropTableIfExists(orm.config.get('migrations').tableName!); + const migrator = new Migrator(orm.em); + // @ts-ignore + await migrator.storage.ensureTable(); + // @ts-ignore + const runner = migrator.runner; + + const mock = jest.fn(); + const logger = new Logger(mock, ['query']); + Object.assign(orm.config, { logger }); + + const migration1 = new MigrationTest1(orm.em.getDriver(), orm.config); + const spy1 = jest.spyOn(Migration.prototype, 'addSql'); + mock.mock.calls.length = 0; + await runner.run(migration1, 'up'); + expect(spy1).toBeCalledWith('select 1 + 1'); + expect(mock.mock.calls).toHaveLength(5); + expect(mock.mock.calls[0][0]).toMatch('begin'); + expect(mock.mock.calls[1][0]).toMatch('pragma foreign_keys = off;'); + expect(mock.mock.calls[2][0]).toMatch('select 1 + 1'); + expect(mock.mock.calls[3][0]).toMatch('pragma foreign_keys = on;'); + expect(mock.mock.calls[4][0]).toMatch('commit'); + mock.mock.calls.length = 0; + + await expect(runner.run(migration1, 'down')).rejects.toThrowError('This migration cannot be reverted'); + const executed = await migrator.getExecutedMigrations(); + expect(executed).toEqual([]); + + mock.mock.calls.length = 0; + // @ts-ignore + migrator.options.disableForeignKeys = false; + const migration2 = new MigrationTest2(orm.em.getDriver(), orm.config); + await runner.run(migration2, 'up'); + expect(mock.mock.calls.length).toBe(4); + expect(mock.mock.calls[0][0]).toMatch('select 1 + 1 as count1'); + expect(mock.mock.calls[1][0]).toMatch('select 1 + 1'); + expect(mock.mock.calls[2][0]).toMatch('select 1 + 1'); + expect(mock.mock.calls[3][0]).toMatch('select 2 + 2 as count2'); + }); + + test('up/down params [all or nothing enabled]', async () => { + await orm.em.getKnex().schema.dropTableIfExists(orm.config.get('migrations').tableName!); + const migrator = new Migrator(orm.em); + // @ts-ignore + migrator.options.disableForeignKeys = false; + const path = process.cwd() + '/temp/migrations'; + + const migration = await migrator.createMigration(path, true); + const migratorMock = jest.spyOn(Migration.prototype, 'down'); + migratorMock.mockImplementation(async () => void 0); + + const mock = jest.fn(); + const logger = new Logger(mock, ['query']); + Object.assign(orm.config, { logger }); + + await migrator.up(migration.fileName); + await migrator.down(migration.fileName.replace('Migration', '').replace('.ts', '')); + await migrator.up({ migrations: [migration.fileName] }); + await migrator.down({ from: 0, to: 0 } as any); + await migrator.up({ to: migration.fileName }); + await migrator.up({ from: migration.fileName } as any); + await migrator.down(); + + await remove(path + '/' + migration.fileName); + const calls = mock.mock.calls.map(call => { + return call[0] + .replace(/ \[took \d+ ms]/, '') + .replace(/\[query] /, '') + .replace(/ trx\d+/, 'trx\\d+'); + }); + expect(calls).toMatchSnapshot('all-or-nothing'); + }); + + test('up/down params [all or nothing disabled]', async () => { + await orm.em.getKnex().schema.dropTableIfExists(orm.config.get('migrations').tableName!); + const migrator = new Migrator(orm.em); + // @ts-ignore + migrator.options.disableForeignKeys = false; + // @ts-ignore + migrator.options.allOrNothing = false; + const path = process.cwd() + '/temp/migrations'; + + const migration = await migrator.createMigration(path, true); + const migratorMock = jest.spyOn(Migration.prototype, 'down'); + migratorMock.mockImplementation(async () => void 0); + + const mock = jest.fn(); + const logger = new Logger(mock, ['query']); + Object.assign(orm.config, { logger }); + + await migrator.up(migration.fileName); + await migrator.down(migration.fileName.replace('Migration', '')); + await migrator.up({ migrations: [migration.fileName] }); + await migrator.down({ from: 0, to: 0 } as any); + await migrator.up({ to: migration.fileName }); + await migrator.up({ from: migration.fileName } as any); + await migrator.down(); + + await remove(path + '/' + migration.fileName); + const calls = mock.mock.calls.map(call => { + return call[0] + .replace(/ \[took \d+ ms]/, '') + .replace(/\[query] /, '') + .replace(/ trx\d+/, 'trx_xx'); + }); + expect(calls).toMatchSnapshot('all-or-nothing-disabled'); + }); + + test('generate js schema migration', async () => { + const dateMock = jest.spyOn(Date.prototype, 'toISOString'); + dateMock.mockReturnValue('2019-10-13T21:48:13.382Z'); + const migrationsSettings = orm.config.get('migrations'); + orm.config.set('migrations', { ...migrationsSettings, emit: 'js' }); // Set migration type to js + const migrator = orm.getMigrator(); + const migration = await migrator.createMigration(); + expect(migration).toMatchSnapshot('migration-js-dump'); + orm.config.set('migrations', migrationsSettings); // Revert migration config changes + await remove(process.cwd() + '/temp/migrations/' + migration.fileName); + }); + +}); diff --git a/tests/features/migrations/Migrator.test.ts b/tests/features/migrations/Migrator.test.ts index 96239b41f935..e61747ccf955 100644 --- a/tests/features/migrations/Migrator.test.ts +++ b/tests/features/migrations/Migrator.test.ts @@ -159,11 +159,12 @@ describe('Migrator', () => { }); test('migration is skipped when no diff', async () => { + await orm.getSchemaGenerator().updateSchema(); const migrator = new Migrator(orm.em); const schemaGeneratorMock = jest.spyOn(SchemaGenerator.prototype, 'getUpdateSchemaSQL'); - schemaGeneratorMock.mockResolvedValue(''); + schemaGeneratorMock.mockResolvedValueOnce({ up: [], down: [] }); const migration = await migrator.createMigration(); - expect(migration).toEqual({ fileName: '', code: '', diff: [] }); + expect(migration).toEqual({ fileName: '', code: '', diff: { up: [], down: [] } }); }); test('run schema migration', async () => { diff --git a/tests/features/migrations/__snapshots__/Migrator.postgres.test.ts.snap b/tests/features/migrations/__snapshots__/Migrator.postgres.test.ts.snap index 3ebce490bcd5..d8e62513f4d5 100644 --- a/tests/features/migrations/__snapshots__/Migrator.postgres.test.ts.snap +++ b/tests/features/migrations/__snapshots__/Migrator.postgres.test.ts.snap @@ -103,100 +103,103 @@ export class Migration20191013214813 extends Migration { } ", - "diff": Array [ - "create table \\"label2\\" (\\"uuid\\" uuid not null, \\"name\\" varchar(255) not null);", - "alter table \\"label2\\" add constraint \\"label2_pkey\\" primary key (\\"uuid\\");", - "", - "create table \\"foo_baz2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"version\\" timestamptz(3) not null default current_timestamp(3));", - "", - "create table \\"foo_bar2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"name with space\\" varchar(255) null, \\"baz_id\\" int null, \\"foo_bar_id\\" int null, \\"version\\" timestamptz(0) not null default current_timestamp(0), \\"blob\\" bytea null, \\"array\\" text[] null, \\"object_property\\" jsonb null);", - "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_unique\\" unique (\\"baz_id\\");", - "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_unique\\" unique (\\"foo_bar_id\\");", - "", - "create table \\"foo_param2\\" (\\"bar_id\\" int not null, \\"baz_id\\" int not null, \\"value\\" varchar(255) not null, \\"version\\" timestamptz(3) not null default current_timestamp(3));", - "alter table \\"foo_param2\\" add constraint \\"foo_param2_pkey\\" primary key (\\"bar_id\\", \\"baz_id\\");", - "", - "create table \\"publisher2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"type\\" text check (\\"type\\" in ('local', 'global')) not null, \\"type2\\" text check (\\"type2\\" in ('LOCAL', 'GLOBAL')) not null, \\"enum1\\" smallint null, \\"enum2\\" smallint null, \\"enum3\\" smallint null, \\"enum4\\" text check (\\"enum4\\" in ('a', 'b', 'c')) null, \\"enum5\\" text check (\\"enum5\\" in ('a')) null);", - "", - "create table \\"book_tag2\\" (\\"id\\" bigserial primary key, \\"name\\" varchar(50) not null);", - "", - "create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int null default null, \\"terms_accepted\\" boolean not null default false, \\"optional\\" boolean null, \\"identities\\" text[] null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" uuid null, \\"favourite_author_id\\" int null);", - "create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");", - "alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");", - "create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");", - "create index \\"author2_born_index\\" on \\"author2\\" (\\"born\\");", - "create index \\"born_time_idx\\" on \\"author2\\" (\\"born_time\\");", - "create index \\"custom_idx_name_123\\" on \\"author2\\" (\\"name\\");", - "create index \\"author2_name_age_index\\" on \\"author2\\" (\\"name\\", \\"age\\");", - "alter table \\"author2\\" add constraint \\"author2_name_email_unique\\" unique (\\"name\\", \\"email\\");", - "", - "create table \\"address2\\" (\\"author_id\\" int not null, \\"value\\" varchar(255) not null);", - "comment on table \\"address2\\" is 'This is address table';", - "comment on column \\"address2\\".\\"value\\" is 'This is address property';", - "alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");", - "", - "create table \\"book2\\" (\\"uuid_pk\\" uuid not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" numeric(8,2) null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int not null, \\"publisher_id\\" int null);", - "alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");", - "", - "create table \\"test2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) null, \\"book_uuid_pk\\" uuid null, \\"parent_id\\" int null, \\"version\\" int not null default 1);", - "alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_unique\\" unique (\\"book_uuid_pk\\");", - "", - "create table \\"configuration2\\" (\\"property\\" varchar(255) not null, \\"test_id\\" int not null, \\"value\\" varchar(255) not null);", - "alter table \\"configuration2\\" add constraint \\"configuration2_pkey\\" primary key (\\"property\\", \\"test_id\\");", - "", - "create table \\"publisher2_tests\\" (\\"id\\" serial primary key, \\"publisher2_id\\" int not null, \\"test2_id\\" int not null);", - "", - "create table \\"test2_bars\\" (\\"test2_id\\" int not null, \\"foo_bar2_id\\" int not null);", - "alter table \\"test2_bars\\" add constraint \\"test2_bars_pkey\\" primary key (\\"test2_id\\", \\"foo_bar2_id\\");", - "", - "create table \\"book2_tags\\" (\\"order\\" serial primary key, \\"book2_uuid_pk\\" uuid not null, \\"book_tag2_id\\" bigint not null);", - "", - "create table \\"book_to_tag_unordered\\" (\\"book2_uuid_pk\\" uuid not null, \\"book_tag2_id\\" bigint not null);", - "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_pkey\\" primary key (\\"book2_uuid_pk\\", \\"book_tag2_id\\");", - "", - "create table \\"author_to_friend\\" (\\"author2_1_id\\" int not null, \\"author2_2_id\\" int not null);", - "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_pkey\\" primary key (\\"author2_1_id\\", \\"author2_2_id\\");", - "", - "create table \\"author2_following\\" (\\"author2_1_id\\" int not null, \\"author2_2_id\\" int not null);", - "alter table \\"author2_following\\" add constraint \\"author2_following_pkey\\" primary key (\\"author2_1_id\\", \\"author2_2_id\\");", - "", - "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_foreign\\" foreign key (\\"baz_id\\") references \\"foo_baz2\\" (\\"id\\") on update cascade on delete set null;", - "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_foreign\\" foreign key (\\"foo_bar_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade on delete set null;", - "", - "alter table \\"foo_param2\\" add constraint \\"foo_param2_bar_id_foreign\\" foreign key (\\"bar_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade;", - "alter table \\"foo_param2\\" add constraint \\"foo_param2_baz_id_foreign\\" foreign key (\\"baz_id\\") references \\"foo_baz2\\" (\\"id\\") on update cascade;", - "", - "alter table \\"author2\\" add constraint \\"author2_favourite_book_uuid_pk_foreign\\" foreign key (\\"favourite_book_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update no action on delete cascade;", - "alter table \\"author2\\" add constraint \\"author2_favourite_author_id_foreign\\" foreign key (\\"favourite_author_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete set null;", - "", - "alter table \\"address2\\" add constraint \\"address2_author_id_foreign\\" foreign key (\\"author_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"book2\\" add constraint \\"book2_author_id_foreign\\" foreign key (\\"author_id\\") references \\"author2\\" (\\"id\\");", - "alter table \\"book2\\" add constraint \\"book2_publisher_id_foreign\\" foreign key (\\"publisher_id\\") references \\"publisher2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_foreign\\" foreign key (\\"book_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on delete set null;", - "alter table \\"test2\\" add constraint \\"test2_parent_id_foreign\\" foreign key (\\"parent_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete set null;", - "", - "alter table \\"configuration2\\" add constraint \\"configuration2_test_id_foreign\\" foreign key (\\"test_id\\") references \\"test2\\" (\\"id\\") on update cascade;", - "", - "alter table \\"publisher2_tests\\" add constraint \\"publisher2_tests_publisher2_id_foreign\\" foreign key (\\"publisher2_id\\") references \\"publisher2\\" (\\"id\\") on update cascade on delete cascade;", - "alter table \\"publisher2_tests\\" add constraint \\"publisher2_tests_test2_id_foreign\\" foreign key (\\"test2_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"test2_bars\\" add constraint \\"test2_bars_test2_id_foreign\\" foreign key (\\"test2_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete cascade;", - "alter table \\"test2_bars\\" add constraint \\"test2_bars_foo_bar2_id_foreign\\" foreign key (\\"foo_bar2_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"book2_tags\\" add constraint \\"book2_tags_book2_uuid_pk_foreign\\" foreign key (\\"book2_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update cascade on delete cascade;", - "alter table \\"book2_tags\\" add constraint \\"book2_tags_book_tag2_id_foreign\\" foreign key (\\"book_tag2_id\\") references \\"book_tag2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_book2_uuid_pk_foreign\\" foreign key (\\"book2_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update cascade on delete cascade;", - "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_book_tag2_id_foreign\\" foreign key (\\"book_tag2_id\\") references \\"book_tag2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_author2_1_id_foreign\\" foreign key (\\"author2_1_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_author2_2_id_foreign\\" foreign key (\\"author2_2_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"author2_following\\" add constraint \\"author2_following_author2_1_id_foreign\\" foreign key (\\"author2_1_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - "alter table \\"author2_following\\" add constraint \\"author2_following_author2_2_id_foreign\\" foreign key (\\"author2_2_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - ], + "diff": Object { + "down": Array [], + "up": Array [ + "create table \\"label2\\" (\\"uuid\\" uuid not null, \\"name\\" varchar(255) not null);", + "alter table \\"label2\\" add constraint \\"label2_pkey\\" primary key (\\"uuid\\");", + "", + "create table \\"foo_baz2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"version\\" timestamptz(3) not null default current_timestamp(3));", + "", + "create table \\"foo_bar2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"name with space\\" varchar(255) null, \\"baz_id\\" int null, \\"foo_bar_id\\" int null, \\"version\\" timestamptz(0) not null default current_timestamp(0), \\"blob\\" bytea null, \\"array\\" text[] null, \\"object_property\\" jsonb null);", + "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_unique\\" unique (\\"baz_id\\");", + "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_unique\\" unique (\\"foo_bar_id\\");", + "", + "create table \\"foo_param2\\" (\\"bar_id\\" int not null, \\"baz_id\\" int not null, \\"value\\" varchar(255) not null, \\"version\\" timestamptz(3) not null default current_timestamp(3));", + "alter table \\"foo_param2\\" add constraint \\"foo_param2_pkey\\" primary key (\\"bar_id\\", \\"baz_id\\");", + "", + "create table \\"publisher2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"type\\" text check (\\"type\\" in ('local', 'global')) not null, \\"type2\\" text check (\\"type2\\" in ('LOCAL', 'GLOBAL')) not null, \\"enum1\\" smallint null, \\"enum2\\" smallint null, \\"enum3\\" smallint null, \\"enum4\\" text check (\\"enum4\\" in ('a', 'b', 'c')) null, \\"enum5\\" text check (\\"enum5\\" in ('a')) null);", + "", + "create table \\"book_tag2\\" (\\"id\\" bigserial primary key, \\"name\\" varchar(50) not null);", + "", + "create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int null default null, \\"terms_accepted\\" boolean not null default false, \\"optional\\" boolean null, \\"identities\\" text[] null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" uuid null, \\"favourite_author_id\\" int null);", + "create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");", + "alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");", + "create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");", + "create index \\"author2_born_index\\" on \\"author2\\" (\\"born\\");", + "create index \\"born_time_idx\\" on \\"author2\\" (\\"born_time\\");", + "create index \\"custom_idx_name_123\\" on \\"author2\\" (\\"name\\");", + "create index \\"author2_name_age_index\\" on \\"author2\\" (\\"name\\", \\"age\\");", + "alter table \\"author2\\" add constraint \\"author2_name_email_unique\\" unique (\\"name\\", \\"email\\");", + "", + "create table \\"address2\\" (\\"author_id\\" int not null, \\"value\\" varchar(255) not null);", + "comment on table \\"address2\\" is 'This is address table';", + "comment on column \\"address2\\".\\"value\\" is 'This is address property';", + "alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");", + "", + "create table \\"book2\\" (\\"uuid_pk\\" uuid not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" numeric(8,2) null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int not null, \\"publisher_id\\" int null);", + "alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");", + "", + "create table \\"test2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) null, \\"book_uuid_pk\\" uuid null, \\"parent_id\\" int null, \\"version\\" int not null default 1);", + "alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_unique\\" unique (\\"book_uuid_pk\\");", + "", + "create table \\"configuration2\\" (\\"property\\" varchar(255) not null, \\"test_id\\" int not null, \\"value\\" varchar(255) not null);", + "alter table \\"configuration2\\" add constraint \\"configuration2_pkey\\" primary key (\\"property\\", \\"test_id\\");", + "", + "create table \\"publisher2_tests\\" (\\"id\\" serial primary key, \\"publisher2_id\\" int not null, \\"test2_id\\" int not null);", + "", + "create table \\"test2_bars\\" (\\"test2_id\\" int not null, \\"foo_bar2_id\\" int not null);", + "alter table \\"test2_bars\\" add constraint \\"test2_bars_pkey\\" primary key (\\"test2_id\\", \\"foo_bar2_id\\");", + "", + "create table \\"book2_tags\\" (\\"order\\" serial primary key, \\"book2_uuid_pk\\" uuid not null, \\"book_tag2_id\\" bigint not null);", + "", + "create table \\"book_to_tag_unordered\\" (\\"book2_uuid_pk\\" uuid not null, \\"book_tag2_id\\" bigint not null);", + "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_pkey\\" primary key (\\"book2_uuid_pk\\", \\"book_tag2_id\\");", + "", + "create table \\"author_to_friend\\" (\\"author2_1_id\\" int not null, \\"author2_2_id\\" int not null);", + "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_pkey\\" primary key (\\"author2_1_id\\", \\"author2_2_id\\");", + "", + "create table \\"author2_following\\" (\\"author2_1_id\\" int not null, \\"author2_2_id\\" int not null);", + "alter table \\"author2_following\\" add constraint \\"author2_following_pkey\\" primary key (\\"author2_1_id\\", \\"author2_2_id\\");", + "", + "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_foreign\\" foreign key (\\"baz_id\\") references \\"foo_baz2\\" (\\"id\\") on update cascade on delete set null;", + "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_foreign\\" foreign key (\\"foo_bar_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade on delete set null;", + "", + "alter table \\"foo_param2\\" add constraint \\"foo_param2_bar_id_foreign\\" foreign key (\\"bar_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade;", + "alter table \\"foo_param2\\" add constraint \\"foo_param2_baz_id_foreign\\" foreign key (\\"baz_id\\") references \\"foo_baz2\\" (\\"id\\") on update cascade;", + "", + "alter table \\"author2\\" add constraint \\"author2_favourite_book_uuid_pk_foreign\\" foreign key (\\"favourite_book_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update no action on delete cascade;", + "alter table \\"author2\\" add constraint \\"author2_favourite_author_id_foreign\\" foreign key (\\"favourite_author_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete set null;", + "", + "alter table \\"address2\\" add constraint \\"address2_author_id_foreign\\" foreign key (\\"author_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"book2\\" add constraint \\"book2_author_id_foreign\\" foreign key (\\"author_id\\") references \\"author2\\" (\\"id\\");", + "alter table \\"book2\\" add constraint \\"book2_publisher_id_foreign\\" foreign key (\\"publisher_id\\") references \\"publisher2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_foreign\\" foreign key (\\"book_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on delete set null;", + "alter table \\"test2\\" add constraint \\"test2_parent_id_foreign\\" foreign key (\\"parent_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete set null;", + "", + "alter table \\"configuration2\\" add constraint \\"configuration2_test_id_foreign\\" foreign key (\\"test_id\\") references \\"test2\\" (\\"id\\") on update cascade;", + "", + "alter table \\"publisher2_tests\\" add constraint \\"publisher2_tests_publisher2_id_foreign\\" foreign key (\\"publisher2_id\\") references \\"publisher2\\" (\\"id\\") on update cascade on delete cascade;", + "alter table \\"publisher2_tests\\" add constraint \\"publisher2_tests_test2_id_foreign\\" foreign key (\\"test2_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"test2_bars\\" add constraint \\"test2_bars_test2_id_foreign\\" foreign key (\\"test2_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete cascade;", + "alter table \\"test2_bars\\" add constraint \\"test2_bars_foo_bar2_id_foreign\\" foreign key (\\"foo_bar2_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"book2_tags\\" add constraint \\"book2_tags_book2_uuid_pk_foreign\\" foreign key (\\"book2_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update cascade on delete cascade;", + "alter table \\"book2_tags\\" add constraint \\"book2_tags_book_tag2_id_foreign\\" foreign key (\\"book_tag2_id\\") references \\"book_tag2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_book2_uuid_pk_foreign\\" foreign key (\\"book2_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update cascade on delete cascade;", + "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_book_tag2_id_foreign\\" foreign key (\\"book_tag2_id\\") references \\"book_tag2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_author2_1_id_foreign\\" foreign key (\\"author2_1_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_author2_2_id_foreign\\" foreign key (\\"author2_2_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"author2_following\\" add constraint \\"author2_following_author2_1_id_foreign\\" foreign key (\\"author2_1_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + "alter table \\"author2_following\\" add constraint \\"author2_following_author2_2_id_foreign\\" foreign key (\\"author2_2_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + ], + }, "fileName": "Migration20191013214813.ts", } `; @@ -304,100 +307,103 @@ export class Migration20191013214813 extends Migration { } ", - "diff": Array [ - "create table \\"label2\\" (\\"uuid\\" uuid not null, \\"name\\" varchar(255) not null);", - "alter table \\"label2\\" add constraint \\"label2_pkey\\" primary key (\\"uuid\\");", - "", - "create table \\"foo_baz2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"version\\" timestamptz(3) not null default current_timestamp(3));", - "", - "create table \\"foo_bar2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"name with space\\" varchar(255) null, \\"baz_id\\" int null, \\"foo_bar_id\\" int null, \\"version\\" timestamptz(0) not null default current_timestamp(0), \\"blob\\" bytea null, \\"array\\" text[] null, \\"object_property\\" jsonb null);", - "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_unique\\" unique (\\"baz_id\\");", - "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_unique\\" unique (\\"foo_bar_id\\");", - "", - "create table \\"foo_param2\\" (\\"bar_id\\" int not null, \\"baz_id\\" int not null, \\"value\\" varchar(255) not null, \\"version\\" timestamptz(3) not null default current_timestamp(3));", - "alter table \\"foo_param2\\" add constraint \\"foo_param2_pkey\\" primary key (\\"bar_id\\", \\"baz_id\\");", - "", - "create table \\"publisher2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"type\\" text check (\\"type\\" in ('local', 'global')) not null, \\"type2\\" text check (\\"type2\\" in ('LOCAL', 'GLOBAL')) not null, \\"enum1\\" smallint null, \\"enum2\\" smallint null, \\"enum3\\" smallint null, \\"enum4\\" text check (\\"enum4\\" in ('a', 'b', 'c')) null, \\"enum5\\" text check (\\"enum5\\" in ('a')) null);", - "", - "create table \\"book_tag2\\" (\\"id\\" bigserial primary key, \\"name\\" varchar(50) not null);", - "", - "create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int null default null, \\"terms_accepted\\" boolean not null default false, \\"optional\\" boolean null, \\"identities\\" text[] null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" uuid null, \\"favourite_author_id\\" int null);", - "create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");", - "alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");", - "create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");", - "create index \\"author2_born_index\\" on \\"author2\\" (\\"born\\");", - "create index \\"born_time_idx\\" on \\"author2\\" (\\"born_time\\");", - "create index \\"custom_idx_name_123\\" on \\"author2\\" (\\"name\\");", - "create index \\"author2_name_age_index\\" on \\"author2\\" (\\"name\\", \\"age\\");", - "alter table \\"author2\\" add constraint \\"author2_name_email_unique\\" unique (\\"name\\", \\"email\\");", - "", - "create table \\"address2\\" (\\"author_id\\" int not null, \\"value\\" varchar(255) not null);", - "comment on table \\"address2\\" is 'This is address table';", - "comment on column \\"address2\\".\\"value\\" is 'This is address property';", - "alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");", - "", - "create table \\"book2\\" (\\"uuid_pk\\" uuid not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" numeric(8,2) null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int not null, \\"publisher_id\\" int null);", - "alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");", - "", - "create table \\"test2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) null, \\"book_uuid_pk\\" uuid null, \\"parent_id\\" int null, \\"version\\" int not null default 1);", - "alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_unique\\" unique (\\"book_uuid_pk\\");", - "", - "create table \\"configuration2\\" (\\"property\\" varchar(255) not null, \\"test_id\\" int not null, \\"value\\" varchar(255) not null);", - "alter table \\"configuration2\\" add constraint \\"configuration2_pkey\\" primary key (\\"property\\", \\"test_id\\");", - "", - "create table \\"publisher2_tests\\" (\\"id\\" serial primary key, \\"publisher2_id\\" int not null, \\"test2_id\\" int not null);", - "", - "create table \\"test2_bars\\" (\\"test2_id\\" int not null, \\"foo_bar2_id\\" int not null);", - "alter table \\"test2_bars\\" add constraint \\"test2_bars_pkey\\" primary key (\\"test2_id\\", \\"foo_bar2_id\\");", - "", - "create table \\"book2_tags\\" (\\"order\\" serial primary key, \\"book2_uuid_pk\\" uuid not null, \\"book_tag2_id\\" bigint not null);", - "", - "create table \\"book_to_tag_unordered\\" (\\"book2_uuid_pk\\" uuid not null, \\"book_tag2_id\\" bigint not null);", - "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_pkey\\" primary key (\\"book2_uuid_pk\\", \\"book_tag2_id\\");", - "", - "create table \\"author_to_friend\\" (\\"author2_1_id\\" int not null, \\"author2_2_id\\" int not null);", - "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_pkey\\" primary key (\\"author2_1_id\\", \\"author2_2_id\\");", - "", - "create table \\"author2_following\\" (\\"author2_1_id\\" int not null, \\"author2_2_id\\" int not null);", - "alter table \\"author2_following\\" add constraint \\"author2_following_pkey\\" primary key (\\"author2_1_id\\", \\"author2_2_id\\");", - "", - "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_foreign\\" foreign key (\\"baz_id\\") references \\"foo_baz2\\" (\\"id\\") on update cascade on delete set null;", - "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_foreign\\" foreign key (\\"foo_bar_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade on delete set null;", - "", - "alter table \\"foo_param2\\" add constraint \\"foo_param2_bar_id_foreign\\" foreign key (\\"bar_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade;", - "alter table \\"foo_param2\\" add constraint \\"foo_param2_baz_id_foreign\\" foreign key (\\"baz_id\\") references \\"foo_baz2\\" (\\"id\\") on update cascade;", - "", - "alter table \\"author2\\" add constraint \\"author2_favourite_book_uuid_pk_foreign\\" foreign key (\\"favourite_book_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update no action on delete cascade;", - "alter table \\"author2\\" add constraint \\"author2_favourite_author_id_foreign\\" foreign key (\\"favourite_author_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete set null;", - "", - "alter table \\"address2\\" add constraint \\"address2_author_id_foreign\\" foreign key (\\"author_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"book2\\" add constraint \\"book2_author_id_foreign\\" foreign key (\\"author_id\\") references \\"author2\\" (\\"id\\");", - "alter table \\"book2\\" add constraint \\"book2_publisher_id_foreign\\" foreign key (\\"publisher_id\\") references \\"publisher2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_foreign\\" foreign key (\\"book_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on delete set null;", - "alter table \\"test2\\" add constraint \\"test2_parent_id_foreign\\" foreign key (\\"parent_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete set null;", - "", - "alter table \\"configuration2\\" add constraint \\"configuration2_test_id_foreign\\" foreign key (\\"test_id\\") references \\"test2\\" (\\"id\\") on update cascade;", - "", - "alter table \\"publisher2_tests\\" add constraint \\"publisher2_tests_publisher2_id_foreign\\" foreign key (\\"publisher2_id\\") references \\"publisher2\\" (\\"id\\") on update cascade on delete cascade;", - "alter table \\"publisher2_tests\\" add constraint \\"publisher2_tests_test2_id_foreign\\" foreign key (\\"test2_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"test2_bars\\" add constraint \\"test2_bars_test2_id_foreign\\" foreign key (\\"test2_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete cascade;", - "alter table \\"test2_bars\\" add constraint \\"test2_bars_foo_bar2_id_foreign\\" foreign key (\\"foo_bar2_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"book2_tags\\" add constraint \\"book2_tags_book2_uuid_pk_foreign\\" foreign key (\\"book2_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update cascade on delete cascade;", - "alter table \\"book2_tags\\" add constraint \\"book2_tags_book_tag2_id_foreign\\" foreign key (\\"book_tag2_id\\") references \\"book_tag2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_book2_uuid_pk_foreign\\" foreign key (\\"book2_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update cascade on delete cascade;", - "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_book_tag2_id_foreign\\" foreign key (\\"book_tag2_id\\") references \\"book_tag2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_author2_1_id_foreign\\" foreign key (\\"author2_1_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_author2_2_id_foreign\\" foreign key (\\"author2_2_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - "", - "alter table \\"author2_following\\" add constraint \\"author2_following_author2_1_id_foreign\\" foreign key (\\"author2_1_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - "alter table \\"author2_following\\" add constraint \\"author2_following_author2_2_id_foreign\\" foreign key (\\"author2_2_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", - ], + "diff": Object { + "down": Array [], + "up": Array [ + "create table \\"label2\\" (\\"uuid\\" uuid not null, \\"name\\" varchar(255) not null);", + "alter table \\"label2\\" add constraint \\"label2_pkey\\" primary key (\\"uuid\\");", + "", + "create table \\"foo_baz2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"version\\" timestamptz(3) not null default current_timestamp(3));", + "", + "create table \\"foo_bar2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"name with space\\" varchar(255) null, \\"baz_id\\" int null, \\"foo_bar_id\\" int null, \\"version\\" timestamptz(0) not null default current_timestamp(0), \\"blob\\" bytea null, \\"array\\" text[] null, \\"object_property\\" jsonb null);", + "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_unique\\" unique (\\"baz_id\\");", + "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_unique\\" unique (\\"foo_bar_id\\");", + "", + "create table \\"foo_param2\\" (\\"bar_id\\" int not null, \\"baz_id\\" int not null, \\"value\\" varchar(255) not null, \\"version\\" timestamptz(3) not null default current_timestamp(3));", + "alter table \\"foo_param2\\" add constraint \\"foo_param2_pkey\\" primary key (\\"bar_id\\", \\"baz_id\\");", + "", + "create table \\"publisher2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"type\\" text check (\\"type\\" in ('local', 'global')) not null, \\"type2\\" text check (\\"type2\\" in ('LOCAL', 'GLOBAL')) not null, \\"enum1\\" smallint null, \\"enum2\\" smallint null, \\"enum3\\" smallint null, \\"enum4\\" text check (\\"enum4\\" in ('a', 'b', 'c')) null, \\"enum5\\" text check (\\"enum5\\" in ('a')) null);", + "", + "create table \\"book_tag2\\" (\\"id\\" bigserial primary key, \\"name\\" varchar(50) not null);", + "", + "create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int null default null, \\"terms_accepted\\" boolean not null default false, \\"optional\\" boolean null, \\"identities\\" text[] null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" uuid null, \\"favourite_author_id\\" int null);", + "create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");", + "alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");", + "create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");", + "create index \\"author2_born_index\\" on \\"author2\\" (\\"born\\");", + "create index \\"born_time_idx\\" on \\"author2\\" (\\"born_time\\");", + "create index \\"custom_idx_name_123\\" on \\"author2\\" (\\"name\\");", + "create index \\"author2_name_age_index\\" on \\"author2\\" (\\"name\\", \\"age\\");", + "alter table \\"author2\\" add constraint \\"author2_name_email_unique\\" unique (\\"name\\", \\"email\\");", + "", + "create table \\"address2\\" (\\"author_id\\" int not null, \\"value\\" varchar(255) not null);", + "comment on table \\"address2\\" is 'This is address table';", + "comment on column \\"address2\\".\\"value\\" is 'This is address property';", + "alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");", + "", + "create table \\"book2\\" (\\"uuid_pk\\" uuid not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" numeric(8,2) null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int not null, \\"publisher_id\\" int null);", + "alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");", + "", + "create table \\"test2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) null, \\"book_uuid_pk\\" uuid null, \\"parent_id\\" int null, \\"version\\" int not null default 1);", + "alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_unique\\" unique (\\"book_uuid_pk\\");", + "", + "create table \\"configuration2\\" (\\"property\\" varchar(255) not null, \\"test_id\\" int not null, \\"value\\" varchar(255) not null);", + "alter table \\"configuration2\\" add constraint \\"configuration2_pkey\\" primary key (\\"property\\", \\"test_id\\");", + "", + "create table \\"publisher2_tests\\" (\\"id\\" serial primary key, \\"publisher2_id\\" int not null, \\"test2_id\\" int not null);", + "", + "create table \\"test2_bars\\" (\\"test2_id\\" int not null, \\"foo_bar2_id\\" int not null);", + "alter table \\"test2_bars\\" add constraint \\"test2_bars_pkey\\" primary key (\\"test2_id\\", \\"foo_bar2_id\\");", + "", + "create table \\"book2_tags\\" (\\"order\\" serial primary key, \\"book2_uuid_pk\\" uuid not null, \\"book_tag2_id\\" bigint not null);", + "", + "create table \\"book_to_tag_unordered\\" (\\"book2_uuid_pk\\" uuid not null, \\"book_tag2_id\\" bigint not null);", + "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_pkey\\" primary key (\\"book2_uuid_pk\\", \\"book_tag2_id\\");", + "", + "create table \\"author_to_friend\\" (\\"author2_1_id\\" int not null, \\"author2_2_id\\" int not null);", + "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_pkey\\" primary key (\\"author2_1_id\\", \\"author2_2_id\\");", + "", + "create table \\"author2_following\\" (\\"author2_1_id\\" int not null, \\"author2_2_id\\" int not null);", + "alter table \\"author2_following\\" add constraint \\"author2_following_pkey\\" primary key (\\"author2_1_id\\", \\"author2_2_id\\");", + "", + "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_foreign\\" foreign key (\\"baz_id\\") references \\"foo_baz2\\" (\\"id\\") on update cascade on delete set null;", + "alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_foreign\\" foreign key (\\"foo_bar_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade on delete set null;", + "", + "alter table \\"foo_param2\\" add constraint \\"foo_param2_bar_id_foreign\\" foreign key (\\"bar_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade;", + "alter table \\"foo_param2\\" add constraint \\"foo_param2_baz_id_foreign\\" foreign key (\\"baz_id\\") references \\"foo_baz2\\" (\\"id\\") on update cascade;", + "", + "alter table \\"author2\\" add constraint \\"author2_favourite_book_uuid_pk_foreign\\" foreign key (\\"favourite_book_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update no action on delete cascade;", + "alter table \\"author2\\" add constraint \\"author2_favourite_author_id_foreign\\" foreign key (\\"favourite_author_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete set null;", + "", + "alter table \\"address2\\" add constraint \\"address2_author_id_foreign\\" foreign key (\\"author_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"book2\\" add constraint \\"book2_author_id_foreign\\" foreign key (\\"author_id\\") references \\"author2\\" (\\"id\\");", + "alter table \\"book2\\" add constraint \\"book2_publisher_id_foreign\\" foreign key (\\"publisher_id\\") references \\"publisher2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_foreign\\" foreign key (\\"book_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on delete set null;", + "alter table \\"test2\\" add constraint \\"test2_parent_id_foreign\\" foreign key (\\"parent_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete set null;", + "", + "alter table \\"configuration2\\" add constraint \\"configuration2_test_id_foreign\\" foreign key (\\"test_id\\") references \\"test2\\" (\\"id\\") on update cascade;", + "", + "alter table \\"publisher2_tests\\" add constraint \\"publisher2_tests_publisher2_id_foreign\\" foreign key (\\"publisher2_id\\") references \\"publisher2\\" (\\"id\\") on update cascade on delete cascade;", + "alter table \\"publisher2_tests\\" add constraint \\"publisher2_tests_test2_id_foreign\\" foreign key (\\"test2_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"test2_bars\\" add constraint \\"test2_bars_test2_id_foreign\\" foreign key (\\"test2_id\\") references \\"test2\\" (\\"id\\") on update cascade on delete cascade;", + "alter table \\"test2_bars\\" add constraint \\"test2_bars_foo_bar2_id_foreign\\" foreign key (\\"foo_bar2_id\\") references \\"foo_bar2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"book2_tags\\" add constraint \\"book2_tags_book2_uuid_pk_foreign\\" foreign key (\\"book2_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update cascade on delete cascade;", + "alter table \\"book2_tags\\" add constraint \\"book2_tags_book_tag2_id_foreign\\" foreign key (\\"book_tag2_id\\") references \\"book_tag2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_book2_uuid_pk_foreign\\" foreign key (\\"book2_uuid_pk\\") references \\"book2\\" (\\"uuid_pk\\") on update cascade on delete cascade;", + "alter table \\"book_to_tag_unordered\\" add constraint \\"book_to_tag_unordered_book_tag2_id_foreign\\" foreign key (\\"book_tag2_id\\") references \\"book_tag2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_author2_1_id_foreign\\" foreign key (\\"author2_1_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + "alter table \\"author_to_friend\\" add constraint \\"author_to_friend_author2_2_id_foreign\\" foreign key (\\"author2_2_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + "", + "alter table \\"author2_following\\" add constraint \\"author2_following_author2_1_id_foreign\\" foreign key (\\"author2_1_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + "alter table \\"author2_following\\" add constraint \\"author2_following_author2_2_id_foreign\\" foreign key (\\"author2_2_id\\") references \\"author2\\" (\\"id\\") on update cascade on delete cascade;", + ], + }, "fileName": "Migration20191013214813.ts", } `; @@ -418,16 +424,33 @@ class Migration20191013214813 extends Migration { this.addSql('alter table \\"test2\\" drop column \\"path\\";'); } + async down() { + this.addSql('alter table \\"book2\\" add column \\"foo\\" varchar null default \\\\'lol\\\\';'); + this.addSql('alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";'); + this.addSql('alter table \\"book2\\" alter column \\"double\\" type numeric using (\\"double\\"::numeric);'); + + this.addSql('alter table \\"test2\\" add column \\"path\\" polygon null default null;'); + } + } exports.Migration20191013214813 = Migration20191013214813; ", - "diff": Array [ - "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", - "alter table \\"book2\\" alter column \\"double\\" type double precision using (\\"double\\"::double precision);", - "alter table \\"book2\\" drop column \\"foo\\";", - "", - "alter table \\"test2\\" drop column \\"path\\";", - ], + "diff": Object { + "down": Array [ + "alter table \\"book2\\" add column \\"foo\\" varchar null default 'lol';", + "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", + "alter table \\"book2\\" alter column \\"double\\" type numeric using (\\"double\\"::numeric);", + "", + "alter table \\"test2\\" add column \\"path\\" polygon null default null;", + ], + "up": Array [ + "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", + "alter table \\"book2\\" alter column \\"double\\" type double precision using (\\"double\\"::double precision);", + "alter table \\"book2\\" drop column \\"foo\\";", + "", + "alter table \\"test2\\" drop column \\"path\\";", + ], + }, "fileName": "Migration20191013214813.js", } `; @@ -446,15 +469,32 @@ export class Migration20191013214813 extends Migration { this.addSql('alter table \\"test2\\" drop column \\"path\\";'); } + async down(): Promise { + this.addSql('alter table \\"book2\\" add column \\"foo\\" varchar null default \\\\'lol\\\\';'); + this.addSql('alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";'); + this.addSql('alter table \\"book2\\" alter column \\"double\\" type numeric using (\\"double\\"::numeric);'); + + this.addSql('alter table \\"test2\\" add column \\"path\\" polygon null default null;'); + } + } ", - "diff": Array [ - "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", - "alter table \\"book2\\" alter column \\"double\\" type double precision using (\\"double\\"::double precision);", - "alter table \\"book2\\" drop column \\"foo\\";", - "", - "alter table \\"test2\\" drop column \\"path\\";", - ], + "diff": Object { + "down": Array [ + "alter table \\"book2\\" add column \\"foo\\" varchar null default 'lol';", + "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", + "alter table \\"book2\\" alter column \\"double\\" type numeric using (\\"double\\"::numeric);", + "", + "alter table \\"test2\\" add column \\"path\\" polygon null default null;", + ], + "up": Array [ + "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", + "alter table \\"book2\\" alter column \\"double\\" type double precision using (\\"double\\"::double precision);", + "alter table \\"book2\\" drop column \\"foo\\";", + "", + "alter table \\"test2\\" drop column \\"path\\";", + ], + }, "fileName": "migration-20191013214813.ts", } `; @@ -473,15 +513,32 @@ export class Migration20191013214813 extends Migration { this.addSql('alter table \\"test2\\" drop column \\"path\\";'); } + async down(): Promise { + this.addSql('alter table \\"book2\\" add column \\"foo\\" varchar null default \\\\'lol\\\\';'); + this.addSql('alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";'); + this.addSql('alter table \\"book2\\" alter column \\"double\\" type numeric using (\\"double\\"::numeric);'); + + this.addSql('alter table \\"test2\\" add column \\"path\\" polygon null default null;'); + } + } ", - "diff": Array [ - "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", - "alter table \\"book2\\" alter column \\"double\\" type double precision using (\\"double\\"::double precision);", - "alter table \\"book2\\" drop column \\"foo\\";", - "", - "alter table \\"test2\\" drop column \\"path\\";", - ], + "diff": Object { + "down": Array [ + "alter table \\"book2\\" add column \\"foo\\" varchar null default 'lol';", + "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", + "alter table \\"book2\\" alter column \\"double\\" type numeric using (\\"double\\"::numeric);", + "", + "alter table \\"test2\\" add column \\"path\\" polygon null default null;", + ], + "up": Array [ + "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", + "alter table \\"book2\\" alter column \\"double\\" type double precision using (\\"double\\"::double precision);", + "alter table \\"book2\\" drop column \\"foo\\";", + "", + "alter table \\"test2\\" drop column \\"path\\";", + ], + }, "fileName": "Migration20191013214813.ts", } `; @@ -489,7 +546,10 @@ export class Migration20191013214813 extends Migration { exports[`Migrator (postgres) generate migration with snapshot: migration-snapshot-dump-2 1`] = ` Object { "code": "", - "diff": Array [], + "diff": Object { + "down": Array [], + "up": Array [], + }, "fileName": "", } `; @@ -508,15 +568,32 @@ export class Migration20191013214813 extends Migration { this.addSql('alter table \\"test2\\" drop column \\"path\\";'); } + async down(): Promise { + this.addSql('alter table \\"book2\\" add column \\"foo\\" varchar null default \\\\'lol\\\\';'); + this.addSql('alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";'); + this.addSql('alter table \\"book2\\" alter column \\"double\\" type numeric using (\\"double\\"::numeric);'); + + this.addSql('alter table \\"test2\\" add column \\"path\\" polygon null default null;'); + } + } ", - "diff": Array [ - "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", - "alter table \\"book2\\" alter column \\"double\\" type double precision using (\\"double\\"::double precision);", - "alter table \\"book2\\" drop column \\"foo\\";", - "", - "alter table \\"test2\\" drop column \\"path\\";", - ], + "diff": Object { + "down": Array [ + "alter table \\"book2\\" add column \\"foo\\" varchar null default 'lol';", + "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", + "alter table \\"book2\\" alter column \\"double\\" type numeric using (\\"double\\"::numeric);", + "", + "alter table \\"test2\\" add column \\"path\\" polygon null default null;", + ], + "up": Array [ + "alter table \\"book2\\" drop constraint if exists \\"book2_double_check\\";", + "alter table \\"book2\\" alter column \\"double\\" type double precision using (\\"double\\"::double precision);", + "alter table \\"book2\\" drop column \\"foo\\";", + "", + "alter table \\"test2\\" drop column \\"path\\";", + ], + }, "fileName": "Migration20191013214813.ts", } `; diff --git a/tests/features/migrations/__snapshots__/Migrator.sqlite.test.ts.snap b/tests/features/migrations/__snapshots__/Migrator.sqlite.test.ts.snap new file mode 100644 index 000000000000..dd6b49f315a1 --- /dev/null +++ b/tests/features/migrations/__snapshots__/Migrator.sqlite.test.ts.snap @@ -0,0 +1,349 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Migrator (sqlite) generate initial migration: initial-migration-dump 1`] = ` +Object { + "code": "import { Migration } from '@mikro-orm/migrations'; + +export class Migration20191013214813 extends Migration { + + async up(): Promise { + this.addSql('create table \`foo_baz4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`version\` datetime not null default current_timestamp);'); + + this.addSql('create table \`foo_bar4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null default \\\\'asd\\\\', \`baz_id\` integer null, \`foo_bar_id\` integer null, \`version\` integer not null default 1, \`blob\` blob null, \`array\` text null, \`object\` json null, constraint \`foo_bar4_baz_id_foreign\` foreign key(\`baz_id\`) references \`foo_baz4\`(\`id\`) on delete set null on update cascade, constraint \`foo_bar4_foo_bar_id_foreign\` foreign key(\`foo_bar_id\`) references \`foo_bar4\`(\`id\`) on delete set null on update cascade);'); + this.addSql('create index \`foo_bar4_baz_id_index\` on \`foo_bar4\` (\`baz_id\`);'); + this.addSql('create unique index \`foo_bar4_baz_id_unique\` on \`foo_bar4\` (\`baz_id\`);'); + this.addSql('create index \`foo_bar4_foo_bar_id_index\` on \`foo_bar4\` (\`foo_bar_id\`);'); + this.addSql('create unique index \`foo_bar4_foo_bar_id_unique\` on \`foo_bar4\` (\`foo_bar_id\`);'); + + this.addSql('create table \`test4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text null, \`version\` integer not null default 1);'); + + this.addSql('create table \`publisher4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null default \\\\'asd\\\\', \`type\` text check (\`type\` in (\\\\'local\\\\', \\\\'global\\\\')) not null default \\\\'local\\\\', \`enum3\` integer null);'); + + this.addSql('create table \`publisher4_tests\` (\`id\` integer not null primary key autoincrement, \`publisher4_id\` integer not null, \`test4_id\` integer not null, constraint \`publisher4_tests_publisher4_id_foreign\` foreign key(\`publisher4_id\`) references \`publisher4\`(\`id\`) on delete cascade on update cascade, constraint \`publisher4_tests_test4_id_foreign\` foreign key(\`test4_id\`) references \`test4\`(\`id\`) on delete cascade on update cascade);'); + this.addSql('create index \`publisher4_tests_publisher4_id_index\` on \`publisher4_tests\` (\`publisher4_id\`);'); + this.addSql('create index \`publisher4_tests_test4_id_index\` on \`publisher4_tests\` (\`test4_id\`);'); + + this.addSql('create table \`book_tag4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`version\` datetime not null default current_timestamp);'); + + this.addSql('create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);'); + this.addSql('create unique index \`author4_email_unique\` on \`author4\` (\`email\`);'); + this.addSql('create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);'); + + this.addSql('create table \`book4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`title\` text not null, \`price\` real null, \`author_id\` integer null, \`publisher_id\` integer null, \`meta\` json null, constraint \`book4_author_id_foreign\` foreign key(\`author_id\`) references \`author4\`(\`id\`) on delete set null on update cascade, constraint \`book4_publisher_id_foreign\` foreign key(\`publisher_id\`) references \`publisher4\`(\`id\`) on delete set null on update cascade);'); + this.addSql('create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);'); + this.addSql('create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);'); + + this.addSql('create table \`tags_ordered\` (\`id\` integer not null primary key autoincrement, \`book4_id\` integer not null, \`book_tag4_id\` integer not null, constraint \`tags_ordered_book4_id_foreign\` foreign key(\`book4_id\`) references \`book4\`(\`id\`) on delete cascade on update cascade, constraint \`tags_ordered_book_tag4_id_foreign\` foreign key(\`book_tag4_id\`) references \`book_tag4\`(\`id\`) on delete cascade on update cascade);'); + this.addSql('create index \`tags_ordered_book4_id_index\` on \`tags_ordered\` (\`book4_id\`);'); + this.addSql('create index \`tags_ordered_book_tag4_id_index\` on \`tags_ordered\` (\`book_tag4_id\`);'); + + this.addSql('create table \`tags_unordered\` (\`book4_id\` integer not null, \`book_tag4_id\` integer not null, constraint \`tags_unordered_book4_id_foreign\` foreign key(\`book4_id\`) references \`book4\`(\`id\`) on delete cascade on update cascade, constraint \`tags_unordered_book_tag4_id_foreign\` foreign key(\`book_tag4_id\`) references \`book_tag4\`(\`id\`) on delete cascade on update cascade, primary key (\`book4_id\`, \`book_tag4_id\`));'); + this.addSql('create index \`tags_unordered_book4_id_index\` on \`tags_unordered\` (\`book4_id\`);'); + this.addSql('create index \`tags_unordered_book_tag4_id_index\` on \`tags_unordered\` (\`book_tag4_id\`);'); + } + +} +", + "diff": Object { + "down": Array [], + "up": Array [ + "create table \`foo_baz4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`version\` datetime not null default current_timestamp);", + "", + "create table \`foo_bar4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null default 'asd', \`baz_id\` integer null, \`foo_bar_id\` integer null, \`version\` integer not null default 1, \`blob\` blob null, \`array\` text null, \`object\` json null, constraint \`foo_bar4_baz_id_foreign\` foreign key(\`baz_id\`) references \`foo_baz4\`(\`id\`) on delete set null on update cascade, constraint \`foo_bar4_foo_bar_id_foreign\` foreign key(\`foo_bar_id\`) references \`foo_bar4\`(\`id\`) on delete set null on update cascade);", + "create index \`foo_bar4_baz_id_index\` on \`foo_bar4\` (\`baz_id\`);", + "create unique index \`foo_bar4_baz_id_unique\` on \`foo_bar4\` (\`baz_id\`);", + "create index \`foo_bar4_foo_bar_id_index\` on \`foo_bar4\` (\`foo_bar_id\`);", + "create unique index \`foo_bar4_foo_bar_id_unique\` on \`foo_bar4\` (\`foo_bar_id\`);", + "", + "create table \`test4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text null, \`version\` integer not null default 1);", + "", + "create table \`publisher4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null default 'asd', \`type\` text check (\`type\` in ('local', 'global')) not null default 'local', \`enum3\` integer null);", + "", + "create table \`publisher4_tests\` (\`id\` integer not null primary key autoincrement, \`publisher4_id\` integer not null, \`test4_id\` integer not null, constraint \`publisher4_tests_publisher4_id_foreign\` foreign key(\`publisher4_id\`) references \`publisher4\`(\`id\`) on delete cascade on update cascade, constraint \`publisher4_tests_test4_id_foreign\` foreign key(\`test4_id\`) references \`test4\`(\`id\`) on delete cascade on update cascade);", + "create index \`publisher4_tests_publisher4_id_index\` on \`publisher4_tests\` (\`publisher4_id\`);", + "create index \`publisher4_tests_test4_id_index\` on \`publisher4_tests\` (\`test4_id\`);", + "", + "create table \`book_tag4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`version\` datetime not null default current_timestamp);", + "", + "create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);", + "create unique index \`author4_email_unique\` on \`author4\` (\`email\`);", + "create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);", + "", + "create table \`book4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`title\` text not null, \`price\` real null, \`author_id\` integer null, \`publisher_id\` integer null, \`meta\` json null, constraint \`book4_author_id_foreign\` foreign key(\`author_id\`) references \`author4\`(\`id\`) on delete set null on update cascade, constraint \`book4_publisher_id_foreign\` foreign key(\`publisher_id\`) references \`publisher4\`(\`id\`) on delete set null on update cascade);", + "create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);", + "create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);", + "", + "create table \`tags_ordered\` (\`id\` integer not null primary key autoincrement, \`book4_id\` integer not null, \`book_tag4_id\` integer not null, constraint \`tags_ordered_book4_id_foreign\` foreign key(\`book4_id\`) references \`book4\`(\`id\`) on delete cascade on update cascade, constraint \`tags_ordered_book_tag4_id_foreign\` foreign key(\`book_tag4_id\`) references \`book_tag4\`(\`id\`) on delete cascade on update cascade);", + "create index \`tags_ordered_book4_id_index\` on \`tags_ordered\` (\`book4_id\`);", + "create index \`tags_ordered_book_tag4_id_index\` on \`tags_ordered\` (\`book_tag4_id\`);", + "", + "create table \`tags_unordered\` (\`book4_id\` integer not null, \`book_tag4_id\` integer not null, constraint \`tags_unordered_book4_id_foreign\` foreign key(\`book4_id\`) references \`book4\`(\`id\`) on delete cascade on update cascade, constraint \`tags_unordered_book_tag4_id_foreign\` foreign key(\`book_tag4_id\`) references \`book_tag4\`(\`id\`) on delete cascade on update cascade, primary key (\`book4_id\`, \`book_tag4_id\`));", + "create index \`tags_unordered_book4_id_index\` on \`tags_unordered\` (\`book4_id\`);", + "create index \`tags_unordered_book_tag4_id_index\` on \`tags_unordered\` (\`book_tag4_id\`);", + ], + }, + "fileName": "Migration20191013214813.ts", +} +`; + +exports[`Migrator (sqlite) generate initial migration: initial-migration-dump 2`] = ` +Object { + "code": "import { Migration } from '@mikro-orm/migrations'; + +export class Migration20191013214813 extends Migration { + + async up(): Promise { + this.addSql('create table \`foo_baz4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`version\` datetime not null default current_timestamp);'); + + this.addSql('create table \`foo_bar4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null default \\\\'asd\\\\', \`baz_id\` integer null, \`foo_bar_id\` integer null, \`version\` integer not null default 1, \`blob\` blob null, \`array\` text null, \`object\` json null, constraint \`foo_bar4_baz_id_foreign\` foreign key(\`baz_id\`) references \`foo_baz4\`(\`id\`) on delete set null on update cascade, constraint \`foo_bar4_foo_bar_id_foreign\` foreign key(\`foo_bar_id\`) references \`foo_bar4\`(\`id\`) on delete set null on update cascade);'); + this.addSql('create index \`foo_bar4_baz_id_index\` on \`foo_bar4\` (\`baz_id\`);'); + this.addSql('create unique index \`foo_bar4_baz_id_unique\` on \`foo_bar4\` (\`baz_id\`);'); + this.addSql('create index \`foo_bar4_foo_bar_id_index\` on \`foo_bar4\` (\`foo_bar_id\`);'); + this.addSql('create unique index \`foo_bar4_foo_bar_id_unique\` on \`foo_bar4\` (\`foo_bar_id\`);'); + + this.addSql('create table \`test4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text null, \`version\` integer not null default 1);'); + + this.addSql('create table \`publisher4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null default \\\\'asd\\\\', \`type\` text check (\`type\` in (\\\\'local\\\\', \\\\'global\\\\')) not null default \\\\'local\\\\', \`enum3\` integer null);'); + + this.addSql('create table \`publisher4_tests\` (\`id\` integer not null primary key autoincrement, \`publisher4_id\` integer not null, \`test4_id\` integer not null, constraint \`publisher4_tests_publisher4_id_foreign\` foreign key(\`publisher4_id\`) references \`publisher4\`(\`id\`) on delete cascade on update cascade, constraint \`publisher4_tests_test4_id_foreign\` foreign key(\`test4_id\`) references \`test4\`(\`id\`) on delete cascade on update cascade);'); + this.addSql('create index \`publisher4_tests_publisher4_id_index\` on \`publisher4_tests\` (\`publisher4_id\`);'); + this.addSql('create index \`publisher4_tests_test4_id_index\` on \`publisher4_tests\` (\`test4_id\`);'); + + this.addSql('create table \`book_tag4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`version\` datetime not null default current_timestamp);'); + + this.addSql('create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);'); + this.addSql('create unique index \`author4_email_unique\` on \`author4\` (\`email\`);'); + this.addSql('create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);'); + + this.addSql('create table \`book4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`title\` text not null, \`price\` real null, \`author_id\` integer null, \`publisher_id\` integer null, \`meta\` json null, constraint \`book4_author_id_foreign\` foreign key(\`author_id\`) references \`author4\`(\`id\`) on delete set null on update cascade, constraint \`book4_publisher_id_foreign\` foreign key(\`publisher_id\`) references \`publisher4\`(\`id\`) on delete set null on update cascade);'); + this.addSql('create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);'); + this.addSql('create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);'); + + this.addSql('create table \`tags_ordered\` (\`id\` integer not null primary key autoincrement, \`book4_id\` integer not null, \`book_tag4_id\` integer not null, constraint \`tags_ordered_book4_id_foreign\` foreign key(\`book4_id\`) references \`book4\`(\`id\`) on delete cascade on update cascade, constraint \`tags_ordered_book_tag4_id_foreign\` foreign key(\`book_tag4_id\`) references \`book_tag4\`(\`id\`) on delete cascade on update cascade);'); + this.addSql('create index \`tags_ordered_book4_id_index\` on \`tags_ordered\` (\`book4_id\`);'); + this.addSql('create index \`tags_ordered_book_tag4_id_index\` on \`tags_ordered\` (\`book_tag4_id\`);'); + + this.addSql('create table \`tags_unordered\` (\`book4_id\` integer not null, \`book_tag4_id\` integer not null, constraint \`tags_unordered_book4_id_foreign\` foreign key(\`book4_id\`) references \`book4\`(\`id\`) on delete cascade on update cascade, constraint \`tags_unordered_book_tag4_id_foreign\` foreign key(\`book_tag4_id\`) references \`book_tag4\`(\`id\`) on delete cascade on update cascade, primary key (\`book4_id\`, \`book_tag4_id\`));'); + this.addSql('create index \`tags_unordered_book4_id_index\` on \`tags_unordered\` (\`book4_id\`);'); + this.addSql('create index \`tags_unordered_book_tag4_id_index\` on \`tags_unordered\` (\`book_tag4_id\`);'); + } + +} +", + "diff": Object { + "down": Array [], + "up": Array [ + "create table \`foo_baz4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`version\` datetime not null default current_timestamp);", + "", + "create table \`foo_bar4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null default 'asd', \`baz_id\` integer null, \`foo_bar_id\` integer null, \`version\` integer not null default 1, \`blob\` blob null, \`array\` text null, \`object\` json null, constraint \`foo_bar4_baz_id_foreign\` foreign key(\`baz_id\`) references \`foo_baz4\`(\`id\`) on delete set null on update cascade, constraint \`foo_bar4_foo_bar_id_foreign\` foreign key(\`foo_bar_id\`) references \`foo_bar4\`(\`id\`) on delete set null on update cascade);", + "create index \`foo_bar4_baz_id_index\` on \`foo_bar4\` (\`baz_id\`);", + "create unique index \`foo_bar4_baz_id_unique\` on \`foo_bar4\` (\`baz_id\`);", + "create index \`foo_bar4_foo_bar_id_index\` on \`foo_bar4\` (\`foo_bar_id\`);", + "create unique index \`foo_bar4_foo_bar_id_unique\` on \`foo_bar4\` (\`foo_bar_id\`);", + "", + "create table \`test4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text null, \`version\` integer not null default 1);", + "", + "create table \`publisher4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null default 'asd', \`type\` text check (\`type\` in ('local', 'global')) not null default 'local', \`enum3\` integer null);", + "", + "create table \`publisher4_tests\` (\`id\` integer not null primary key autoincrement, \`publisher4_id\` integer not null, \`test4_id\` integer not null, constraint \`publisher4_tests_publisher4_id_foreign\` foreign key(\`publisher4_id\`) references \`publisher4\`(\`id\`) on delete cascade on update cascade, constraint \`publisher4_tests_test4_id_foreign\` foreign key(\`test4_id\`) references \`test4\`(\`id\`) on delete cascade on update cascade);", + "create index \`publisher4_tests_publisher4_id_index\` on \`publisher4_tests\` (\`publisher4_id\`);", + "create index \`publisher4_tests_test4_id_index\` on \`publisher4_tests\` (\`test4_id\`);", + "", + "create table \`book_tag4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`version\` datetime not null default current_timestamp);", + "", + "create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);", + "create unique index \`author4_email_unique\` on \`author4\` (\`email\`);", + "create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);", + "", + "create table \`book4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`title\` text not null, \`price\` real null, \`author_id\` integer null, \`publisher_id\` integer null, \`meta\` json null, constraint \`book4_author_id_foreign\` foreign key(\`author_id\`) references \`author4\`(\`id\`) on delete set null on update cascade, constraint \`book4_publisher_id_foreign\` foreign key(\`publisher_id\`) references \`publisher4\`(\`id\`) on delete set null on update cascade);", + "create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);", + "create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);", + "", + "create table \`tags_ordered\` (\`id\` integer not null primary key autoincrement, \`book4_id\` integer not null, \`book_tag4_id\` integer not null, constraint \`tags_ordered_book4_id_foreign\` foreign key(\`book4_id\`) references \`book4\`(\`id\`) on delete cascade on update cascade, constraint \`tags_ordered_book_tag4_id_foreign\` foreign key(\`book_tag4_id\`) references \`book_tag4\`(\`id\`) on delete cascade on update cascade);", + "create index \`tags_ordered_book4_id_index\` on \`tags_ordered\` (\`book4_id\`);", + "create index \`tags_ordered_book_tag4_id_index\` on \`tags_ordered\` (\`book_tag4_id\`);", + "", + "create table \`tags_unordered\` (\`book4_id\` integer not null, \`book_tag4_id\` integer not null, constraint \`tags_unordered_book4_id_foreign\` foreign key(\`book4_id\`) references \`book4\`(\`id\`) on delete cascade on update cascade, constraint \`tags_unordered_book_tag4_id_foreign\` foreign key(\`book_tag4_id\`) references \`book_tag4\`(\`id\`) on delete cascade on update cascade, primary key (\`book4_id\`, \`book_tag4_id\`));", + "create index \`tags_unordered_book4_id_index\` on \`tags_unordered\` (\`book4_id\`);", + "create index \`tags_unordered_book_tag4_id_index\` on \`tags_unordered\` (\`book_tag4_id\`);", + ], + }, + "fileName": "Migration20191013214813.ts", +} +`; + +exports[`Migrator (sqlite) generate js schema migration: migration-js-dump 1`] = ` +Object { + "code": "", + "diff": Object { + "down": Array [], + "up": Array [], + }, + "fileName": "", +} +`; + +exports[`Migrator (sqlite) generate js schema migration: migration-js-dump 2`] = ` +Object { + "code": "", + "diff": Object { + "down": Array [], + "up": Array [], + }, + "fileName": "", +} +`; + +exports[`Migrator (sqlite) generate migration with custom name: migration-dump 1`] = ` +Object { + "code": "", + "diff": Object { + "down": Array [], + "up": Array [], + }, + "fileName": "", +} +`; + +exports[`Migrator (sqlite) generate migration with snapshot: migration-snapshot-dump-1 1`] = ` +Object { + "code": "", + "diff": Object { + "down": Array [], + "up": Array [], + }, + "fileName": "", +} +`; + +exports[`Migrator (sqlite) generate migration with snapshot: migration-snapshot-dump-2 1`] = ` +Object { + "code": "", + "diff": Object { + "down": Array [], + "up": Array [], + }, + "fileName": "", +} +`; + +exports[`Migrator (sqlite) generate schema migration: migration-dump 1`] = ` +Object { + "code": "", + "diff": Object { + "down": Array [], + "up": Array [], + }, + "fileName": "", +} +`; + +exports[`Migrator (sqlite) up/down params [all or nothing disabled]: all-or-nothing-disabled 1`] = ` +Array [ + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "create table \`mikro_orm_migrations\` (\`id\` integer not null primary key autoincrement, \`name\` varchar(255), \`executed_at\` datetime default current_timestamp)", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "begin", + "select 1", + "commit", + "insert into \`mikro_orm_migrations\` (\`name\`) values (?)", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "begin", + "commit", + "delete from \`mikro_orm_migrations\` where \`name\` = ?", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "begin", + "select 1", + "commit", + "insert into \`mikro_orm_migrations\` (\`name\`) values (?)", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "begin", + "commit", + "delete from \`mikro_orm_migrations\` where \`name\` = ?", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "begin", + "select 1", + "commit", + "insert into \`mikro_orm_migrations\` (\`name\`) values (?)", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "begin", + "commit", + "delete from \`mikro_orm_migrations\` where \`name\` = ?", +] +`; + +exports[`Migrator (sqlite) up/down params [all or nothing enabled]: all-or-nothing 1`] = ` +Array [ + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "create table \`mikro_orm_migrations\` (\`id\` integer not null primary key autoincrement, \`name\` varchar(255), \`executed_at\` datetime default current_timestamp)", + "begin", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "savepointtrx\\\\d+", + "select 1", + "release savepointtrx\\\\d+", + "insert into \`mikro_orm_migrations\` (\`name\`) values (?)", + "commit", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "begin", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "savepointtrx\\\\d+", + "release savepointtrx\\\\d+", + "delete from \`mikro_orm_migrations\` where \`name\` = ?", + "commit", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "begin", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "savepointtrx\\\\d+", + "select 1", + "release savepointtrx\\\\d+", + "insert into \`mikro_orm_migrations\` (\`name\`) values (?)", + "commit", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "begin", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "savepointtrx\\\\d+", + "release savepointtrx\\\\d+", + "delete from \`mikro_orm_migrations\` where \`name\` = ?", + "commit", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "begin", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "savepointtrx\\\\d+", + "select 1", + "release savepointtrx\\\\d+", + "insert into \`mikro_orm_migrations\` (\`name\`) values (?)", + "commit", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "begin", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "commit", + "select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' union all select name as table_name from sqlite_temp_master where type = 'table' order by name", + "begin", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "select * from \`mikro_orm_migrations\` order by \`id\` asc", + "savepointtrx\\\\d+", + "release savepointtrx\\\\d+", + "delete from \`mikro_orm_migrations\` where \`name\` = ?", + "commit", +] +`; diff --git a/tests/features/migrations/__snapshots__/Migrator.test.ts.snap b/tests/features/migrations/__snapshots__/Migrator.test.ts.snap index 4ad8f2c0745a..71f35a75751b 100644 --- a/tests/features/migrations/__snapshots__/Migrator.test.ts.snap +++ b/tests/features/migrations/__snapshots__/Migrator.test.ts.snap @@ -185,165 +185,168 @@ export class Migration20191013214813 extends Migration { } ", - "diff": Array [ - "create table \`sandwich\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`price\` int not null) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`publisher2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`type\` enum('local', 'global') not null, \`type2\` enum('LOCAL', 'GLOBAL') not null, \`enum1\` tinyint null, \`enum2\` tinyint null, \`enum3\` tinyint null, \`enum4\` enum('a', 'b', 'c') null, \`enum5\` enum('a') null) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`foo_baz2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`version\` datetime(3) not null default current_timestamp(3)) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`foo_bar2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`name with space\` varchar(255) null, \`baz_id\` int unsigned null, \`foo_bar_id\` int unsigned null, \`version\` datetime not null default current_timestamp, \`blob\` blob null, \`array\` text null, \`object_property\` json null) default character set utf8mb4 engine = InnoDB;", - "alter table \`foo_bar2\` add index \`foo_bar2_baz_id_index\`(\`baz_id\`);", - "alter table \`foo_bar2\` add unique \`foo_bar2_baz_id_unique\`(\`baz_id\`);", - "alter table \`foo_bar2\` add index \`foo_bar2_foo_bar_id_index\`(\`foo_bar_id\`);", - "alter table \`foo_bar2\` add unique \`foo_bar2_foo_bar_id_unique\`(\`foo_bar_id\`);", - "", - "create table \`foo_param2\` (\`bar_id\` int unsigned not null, \`baz_id\` int unsigned not null, \`value\` varchar(255) not null, \`version\` datetime(3) not null default current_timestamp(3)) default character set utf8mb4 engine = InnoDB;", - "alter table \`foo_param2\` add index \`foo_param2_bar_id_index\`(\`bar_id\`);", - "alter table \`foo_param2\` add index \`foo_param2_baz_id_index\`(\`baz_id\`);", - "alter table \`foo_param2\` add primary key \`foo_param2_pkey\`(\`bar_id\`, \`baz_id\`);", - "", - "create table \`dummy2\` (\`id\` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`car2\` (\`name\` varchar(100) not null, \`year\` int unsigned not null, \`price\` int not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`car2\` add index \`car2_name_index\`(\`name\`);", - "alter table \`car2\` add index \`car2_year_index\`(\`year\`);", - "alter table \`car2\` add primary key \`car2_pkey\`(\`name\`, \`year\`);", - "", - "create table \`car_owner2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`car_name\` varchar(100) not null, \`car_year\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`car_owner2\` add index \`car_owner2_car_name_car_year_index\`(\`car_name\`, \`car_year\`);", - "", - "create table \`user2\` (\`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`foo\` int null, \`favourite_car_name\` varchar(100) null, \`favourite_car_year\` int unsigned null) default character set utf8mb4 engine = InnoDB;", - "alter table \`user2\` add index \`user2_favourite_car_name_favourite_car_year_index\`(\`favourite_car_name\`, \`favourite_car_year\`);", - "alter table \`user2\` add unique \`user2_favourite_car_name_favourite_car_year_unique\`(\`favourite_car_name\`, \`favourite_car_year\`);", - "alter table \`user2\` add primary key \`user2_pkey\`(\`first_name\`, \`last_name\`);", - "", - "create table \`user2_sandwiches\` (\`user2_first_name\` varchar(100) not null, \`user2_last_name\` varchar(100) not null, \`sandwich_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`user2_sandwiches\` add index \`user2_sandwiches_sandwich_id_index\`(\`sandwich_id\`);", - "alter table \`user2_sandwiches\` add index \`user2_sandwiches_user2_first_name_user2_last_name_index\`(\`user2_first_name\`, \`user2_last_name\`);", - "alter table \`user2_sandwiches\` add primary key \`user2_sandwiches_pkey\`(\`user2_first_name\`, \`user2_last_name\`, \`sandwich_id\`);", - "", - "create table \`user2_cars\` (\`user2_first_name\` varchar(100) not null, \`user2_last_name\` varchar(100) not null, \`car2_name\` varchar(100) not null, \`car2_year\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`user2_cars\` add index \`user2_cars_user2_first_name_user2_last_name_index\`(\`user2_first_name\`, \`user2_last_name\`);", - "alter table \`user2_cars\` add index \`user2_cars_car2_name_car2_year_index\`(\`car2_name\`, \`car2_year\`);", - "alter table \`user2_cars\` add primary key \`user2_cars_pkey\`(\`user2_first_name\`, \`user2_last_name\`, \`car2_name\`, \`car2_year\`);", - "", - "create table \`book_tag2\` (\`id\` bigint unsigned not null auto_increment primary key, \`name\` varchar(50) not null) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`base_user2\` (\`id\` int unsigned not null auto_increment primary key, \`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`type\` enum('employee', 'manager', 'owner') not null, \`owner_prop\` varchar(255) null, \`favourite_employee_id\` int unsigned null, \`favourite_manager_id\` int unsigned null, \`employee_prop\` int null, \`manager_prop\` varchar(255) null) default character set utf8mb4 engine = InnoDB;", - "alter table \`base_user2\` add index \`base_user2_type_index\`(\`type\`);", - "alter table \`base_user2\` add index \`base_user2_favourite_employee_id_index\`(\`favourite_employee_id\`);", - "alter table \`base_user2\` add index \`base_user2_favourite_manager_id_index\`(\`favourite_manager_id\`);", - "alter table \`base_user2\` add unique \`base_user2_favourite_manager_id_unique\`(\`favourite_manager_id\`);", - "", - "create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int null default null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` text null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int unsigned null) default character set utf8mb4 engine = InnoDB;", - "alter table \`author2\` add index \`custom_email_index_name\`(\`email\`);", - "alter table \`author2\` add unique \`custom_email_unique_name\`(\`email\`);", - "alter table \`author2\` add index \`author2_terms_accepted_index\`(\`terms_accepted\`);", - "alter table \`author2\` add index \`author2_born_index\`(\`born\`);", - "alter table \`author2\` add index \`born_time_idx\`(\`born_time\`);", - "alter table \`author2\` add index \`author2_favourite_book_uuid_pk_index\`(\`favourite_book_uuid_pk\`);", - "alter table \`author2\` add index \`author2_favourite_author_id_index\`(\`favourite_author_id\`);", - "alter table \`author2\` add index \`custom_idx_name_123\`(\`name\`);", - "alter table \`author2\` add index \`author2_name_age_index\`(\`name\`, \`age\`);", - "alter table \`author2\` add unique \`author2_name_email_unique\`(\`name\`, \`email\`);", - "", - "create table \`book2\` (\`uuid_pk\` varchar(36) not null, \`created_at\` datetime(3) not null default current_timestamp(3), \`title\` varchar(255) null default '', \`perex\` text null, \`price\` numeric(8,2) null, \`double\` double null, \`meta\` json null, \`author_id\` int unsigned not null, \`publisher_id\` int unsigned null) default character set utf8mb4 engine = InnoDB;", - "alter table \`book2\` add index \`book2_author_id_index\`(\`author_id\`);", - "alter table \`book2\` add index \`book2_publisher_id_index\`(\`publisher_id\`);", - "alter table \`book2\` add primary key \`book2_pkey\`(\`uuid_pk\`);", - "", - "create table \`test2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) null, \`book_uuid_pk\` varchar(36) null, \`parent_id\` int unsigned null, \`version\` int not null default 1) default character set utf8mb4 engine = InnoDB;", - "alter table \`test2\` add index \`test2_book_uuid_pk_index\`(\`book_uuid_pk\`);", - "alter table \`test2\` add unique \`test2_book_uuid_pk_unique\`(\`book_uuid_pk\`);", - "alter table \`test2\` add index \`test2_parent_id_index\`(\`parent_id\`);", - "", - "create table \`configuration2\` (\`property\` varchar(255) not null, \`test_id\` int unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`configuration2\` add index \`configuration2_test_id_index\`(\`test_id\`);", - "alter table \`configuration2\` add primary key \`configuration2_pkey\`(\`property\`, \`test_id\`);", - "", - "create table \`publisher2_tests\` (\`id\` int unsigned not null auto_increment primary key, \`publisher2_id\` int unsigned not null, \`test2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`publisher2_tests\` add index \`publisher2_tests_publisher2_id_index\`(\`publisher2_id\`);", - "alter table \`publisher2_tests\` add index \`publisher2_tests_test2_id_index\`(\`test2_id\`);", - "", - "create table \`test2_bars\` (\`test2_id\` int unsigned not null, \`foo_bar2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`test2_bars\` add index \`test2_bars_test2_id_index\`(\`test2_id\`);", - "alter table \`test2_bars\` add index \`test2_bars_foo_bar2_id_index\`(\`foo_bar2_id\`);", - "alter table \`test2_bars\` add primary key \`test2_bars_pkey\`(\`test2_id\`, \`foo_bar2_id\`);", - "", - "create table \`book2_tags\` (\`order\` int unsigned not null auto_increment primary key, \`book2_uuid_pk\` varchar(36) not null, \`book_tag2_id\` bigint unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`book2_tags\` add index \`book2_tags_book2_uuid_pk_index\`(\`book2_uuid_pk\`);", - "alter table \`book2_tags\` add index \`book2_tags_book_tag2_id_index\`(\`book_tag2_id\`);", - "", - "create table \`book_to_tag_unordered\` (\`book2_uuid_pk\` varchar(36) not null, \`book_tag2_id\` bigint unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`book_to_tag_unordered\` add index \`book_to_tag_unordered_book2_uuid_pk_index\`(\`book2_uuid_pk\`);", - "alter table \`book_to_tag_unordered\` add index \`book_to_tag_unordered_book_tag2_id_index\`(\`book_tag2_id\`);", - "alter table \`book_to_tag_unordered\` add primary key \`book_to_tag_unordered_pkey\`(\`book2_uuid_pk\`, \`book_tag2_id\`);", - "", - "create table \`author_to_friend\` (\`author2_1_id\` int unsigned not null, \`author2_2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`author_to_friend\` add index \`author_to_friend_author2_1_id_index\`(\`author2_1_id\`);", - "alter table \`author_to_friend\` add index \`author_to_friend_author2_2_id_index\`(\`author2_2_id\`);", - "alter table \`author_to_friend\` add primary key \`author_to_friend_pkey\`(\`author2_1_id\`, \`author2_2_id\`);", - "", - "create table \`author2_following\` (\`author2_1_id\` int unsigned not null, \`author2_2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`author2_following\` add index \`author2_following_author2_1_id_index\`(\`author2_1_id\`);", - "alter table \`author2_following\` add index \`author2_following_author2_2_id_index\`(\`author2_2_id\`);", - "alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`author2_1_id\`, \`author2_2_id\`);", - "", - "create table \`address2\` (\`author_id\` int unsigned not null, \`value\` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';", - "alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);", - "alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);", - "", - "alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;", - "alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;", - "", - "alter table \`foo_param2\` add constraint \`foo_param2_bar_id_foreign\` foreign key (\`bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade;", - "alter table \`foo_param2\` add constraint \`foo_param2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade;", - "", - "alter table \`car_owner2\` add constraint \`car_owner2_car_name_car_year_foreign\` foreign key (\`car_name\`, \`car_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade;", - "", - "alter table \`user2\` add constraint \`user2_favourite_car_name_favourite_car_year_foreign\` foreign key (\`favourite_car_name\`, \`favourite_car_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade on delete set null;", - "", - "alter table \`user2_sandwiches\` add constraint \`user2_sandwiches_user2_first_name_user2_last_name_foreign\` foreign key (\`user2_first_name\`, \`user2_last_name\`) references \`user2\` (\`first_name\`, \`last_name\`) on update cascade on delete cascade;", - "alter table \`user2_sandwiches\` add constraint \`user2_sandwiches_sandwich_id_foreign\` foreign key (\`sandwich_id\`) references \`sandwich\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`user2_cars\` add constraint \`user2_cars_user2_first_name_user2_last_name_foreign\` foreign key (\`user2_first_name\`, \`user2_last_name\`) references \`user2\` (\`first_name\`, \`last_name\`) on update cascade on delete cascade;", - "alter table \`user2_cars\` add constraint \`user2_cars_car2_name_car2_year_foreign\` foreign key (\`car2_name\`, \`car2_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade on delete cascade;", - "", - "alter table \`base_user2\` add constraint \`base_user2_favourite_employee_id_foreign\` foreign key (\`favourite_employee_id\`) references \`base_user2\` (\`id\`) on update cascade on delete set null;", - "alter table \`base_user2\` add constraint \`base_user2_favourite_manager_id_foreign\` foreign key (\`favourite_manager_id\`) references \`base_user2\` (\`id\`) on update cascade on delete set null;", - "", - "alter table \`author2\` add constraint \`author2_favourite_book_uuid_pk_foreign\` foreign key (\`favourite_book_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update no action on delete cascade;", - "alter table \`author2\` add constraint \`author2_favourite_author_id_foreign\` foreign key (\`favourite_author_id\`) references \`author2\` (\`id\`) on update cascade on delete set null;", - "", - "alter table \`book2\` add constraint \`book2_author_id_foreign\` foreign key (\`author_id\`) references \`author2\` (\`id\`);", - "alter table \`book2\` add constraint \`book2_publisher_id_foreign\` foreign key (\`publisher_id\`) references \`publisher2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`test2\` add constraint \`test2_book_uuid_pk_foreign\` foreign key (\`book_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on delete set null;", - "alter table \`test2\` add constraint \`test2_parent_id_foreign\` foreign key (\`parent_id\`) references \`test2\` (\`id\`) on update cascade on delete set null;", - "", - "alter table \`configuration2\` add constraint \`configuration2_test_id_foreign\` foreign key (\`test_id\`) references \`test2\` (\`id\`) on update cascade;", - "", - "alter table \`publisher2_tests\` add constraint \`publisher2_tests_publisher2_id_foreign\` foreign key (\`publisher2_id\`) references \`publisher2\` (\`id\`) on update cascade on delete cascade;", - "alter table \`publisher2_tests\` add constraint \`publisher2_tests_test2_id_foreign\` foreign key (\`test2_id\`) references \`test2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`test2_bars\` add constraint \`test2_bars_test2_id_foreign\` foreign key (\`test2_id\`) references \`test2\` (\`id\`) on update cascade on delete cascade;", - "alter table \`test2_bars\` add constraint \`test2_bars_foo_bar2_id_foreign\` foreign key (\`foo_bar2_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`book2_tags\` add constraint \`book2_tags_book2_uuid_pk_foreign\` foreign key (\`book2_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update cascade on delete cascade;", - "alter table \`book2_tags\` add constraint \`book2_tags_book_tag2_id_foreign\` foreign key (\`book_tag2_id\`) references \`book_tag2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`book_to_tag_unordered\` add constraint \`book_to_tag_unordered_book2_uuid_pk_foreign\` foreign key (\`book2_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update cascade on delete cascade;", - "alter table \`book_to_tag_unordered\` add constraint \`book_to_tag_unordered_book_tag2_id_foreign\` foreign key (\`book_tag2_id\`) references \`book_tag2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`author_to_friend\` add constraint \`author_to_friend_author2_1_id_foreign\` foreign key (\`author2_1_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - "alter table \`author_to_friend\` add constraint \`author_to_friend_author2_2_id_foreign\` foreign key (\`author2_2_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`author2_following\` add constraint \`author2_following_author2_1_id_foreign\` foreign key (\`author2_1_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - "alter table \`author2_following\` add constraint \`author2_following_author2_2_id_foreign\` foreign key (\`author2_2_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`address2\` add constraint \`address2_author_id_foreign\` foreign key (\`author_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - ], + "diff": Object { + "down": Array [], + "up": Array [ + "create table \`sandwich\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`price\` int not null) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`publisher2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`type\` enum('local', 'global') not null, \`type2\` enum('LOCAL', 'GLOBAL') not null, \`enum1\` tinyint null, \`enum2\` tinyint null, \`enum3\` tinyint null, \`enum4\` enum('a', 'b', 'c') null, \`enum5\` enum('a') null) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`foo_baz2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`version\` datetime(3) not null default current_timestamp(3)) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`foo_bar2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`name with space\` varchar(255) null, \`baz_id\` int unsigned null, \`foo_bar_id\` int unsigned null, \`version\` datetime not null default current_timestamp, \`blob\` blob null, \`array\` text null, \`object_property\` json null) default character set utf8mb4 engine = InnoDB;", + "alter table \`foo_bar2\` add index \`foo_bar2_baz_id_index\`(\`baz_id\`);", + "alter table \`foo_bar2\` add unique \`foo_bar2_baz_id_unique\`(\`baz_id\`);", + "alter table \`foo_bar2\` add index \`foo_bar2_foo_bar_id_index\`(\`foo_bar_id\`);", + "alter table \`foo_bar2\` add unique \`foo_bar2_foo_bar_id_unique\`(\`foo_bar_id\`);", + "", + "create table \`foo_param2\` (\`bar_id\` int unsigned not null, \`baz_id\` int unsigned not null, \`value\` varchar(255) not null, \`version\` datetime(3) not null default current_timestamp(3)) default character set utf8mb4 engine = InnoDB;", + "alter table \`foo_param2\` add index \`foo_param2_bar_id_index\`(\`bar_id\`);", + "alter table \`foo_param2\` add index \`foo_param2_baz_id_index\`(\`baz_id\`);", + "alter table \`foo_param2\` add primary key \`foo_param2_pkey\`(\`bar_id\`, \`baz_id\`);", + "", + "create table \`dummy2\` (\`id\` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`car2\` (\`name\` varchar(100) not null, \`year\` int unsigned not null, \`price\` int not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`car2\` add index \`car2_name_index\`(\`name\`);", + "alter table \`car2\` add index \`car2_year_index\`(\`year\`);", + "alter table \`car2\` add primary key \`car2_pkey\`(\`name\`, \`year\`);", + "", + "create table \`car_owner2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`car_name\` varchar(100) not null, \`car_year\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`car_owner2\` add index \`car_owner2_car_name_car_year_index\`(\`car_name\`, \`car_year\`);", + "", + "create table \`user2\` (\`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`foo\` int null, \`favourite_car_name\` varchar(100) null, \`favourite_car_year\` int unsigned null) default character set utf8mb4 engine = InnoDB;", + "alter table \`user2\` add index \`user2_favourite_car_name_favourite_car_year_index\`(\`favourite_car_name\`, \`favourite_car_year\`);", + "alter table \`user2\` add unique \`user2_favourite_car_name_favourite_car_year_unique\`(\`favourite_car_name\`, \`favourite_car_year\`);", + "alter table \`user2\` add primary key \`user2_pkey\`(\`first_name\`, \`last_name\`);", + "", + "create table \`user2_sandwiches\` (\`user2_first_name\` varchar(100) not null, \`user2_last_name\` varchar(100) not null, \`sandwich_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`user2_sandwiches\` add index \`user2_sandwiches_sandwich_id_index\`(\`sandwich_id\`);", + "alter table \`user2_sandwiches\` add index \`user2_sandwiches_user2_first_name_user2_last_name_index\`(\`user2_first_name\`, \`user2_last_name\`);", + "alter table \`user2_sandwiches\` add primary key \`user2_sandwiches_pkey\`(\`user2_first_name\`, \`user2_last_name\`, \`sandwich_id\`);", + "", + "create table \`user2_cars\` (\`user2_first_name\` varchar(100) not null, \`user2_last_name\` varchar(100) not null, \`car2_name\` varchar(100) not null, \`car2_year\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`user2_cars\` add index \`user2_cars_user2_first_name_user2_last_name_index\`(\`user2_first_name\`, \`user2_last_name\`);", + "alter table \`user2_cars\` add index \`user2_cars_car2_name_car2_year_index\`(\`car2_name\`, \`car2_year\`);", + "alter table \`user2_cars\` add primary key \`user2_cars_pkey\`(\`user2_first_name\`, \`user2_last_name\`, \`car2_name\`, \`car2_year\`);", + "", + "create table \`book_tag2\` (\`id\` bigint unsigned not null auto_increment primary key, \`name\` varchar(50) not null) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`base_user2\` (\`id\` int unsigned not null auto_increment primary key, \`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`type\` enum('employee', 'manager', 'owner') not null, \`owner_prop\` varchar(255) null, \`favourite_employee_id\` int unsigned null, \`favourite_manager_id\` int unsigned null, \`employee_prop\` int null, \`manager_prop\` varchar(255) null) default character set utf8mb4 engine = InnoDB;", + "alter table \`base_user2\` add index \`base_user2_type_index\`(\`type\`);", + "alter table \`base_user2\` add index \`base_user2_favourite_employee_id_index\`(\`favourite_employee_id\`);", + "alter table \`base_user2\` add index \`base_user2_favourite_manager_id_index\`(\`favourite_manager_id\`);", + "alter table \`base_user2\` add unique \`base_user2_favourite_manager_id_unique\`(\`favourite_manager_id\`);", + "", + "create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int null default null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` text null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int unsigned null) default character set utf8mb4 engine = InnoDB;", + "alter table \`author2\` add index \`custom_email_index_name\`(\`email\`);", + "alter table \`author2\` add unique \`custom_email_unique_name\`(\`email\`);", + "alter table \`author2\` add index \`author2_terms_accepted_index\`(\`terms_accepted\`);", + "alter table \`author2\` add index \`author2_born_index\`(\`born\`);", + "alter table \`author2\` add index \`born_time_idx\`(\`born_time\`);", + "alter table \`author2\` add index \`author2_favourite_book_uuid_pk_index\`(\`favourite_book_uuid_pk\`);", + "alter table \`author2\` add index \`author2_favourite_author_id_index\`(\`favourite_author_id\`);", + "alter table \`author2\` add index \`custom_idx_name_123\`(\`name\`);", + "alter table \`author2\` add index \`author2_name_age_index\`(\`name\`, \`age\`);", + "alter table \`author2\` add unique \`author2_name_email_unique\`(\`name\`, \`email\`);", + "", + "create table \`book2\` (\`uuid_pk\` varchar(36) not null, \`created_at\` datetime(3) not null default current_timestamp(3), \`title\` varchar(255) null default '', \`perex\` text null, \`price\` numeric(8,2) null, \`double\` double null, \`meta\` json null, \`author_id\` int unsigned not null, \`publisher_id\` int unsigned null) default character set utf8mb4 engine = InnoDB;", + "alter table \`book2\` add index \`book2_author_id_index\`(\`author_id\`);", + "alter table \`book2\` add index \`book2_publisher_id_index\`(\`publisher_id\`);", + "alter table \`book2\` add primary key \`book2_pkey\`(\`uuid_pk\`);", + "", + "create table \`test2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) null, \`book_uuid_pk\` varchar(36) null, \`parent_id\` int unsigned null, \`version\` int not null default 1) default character set utf8mb4 engine = InnoDB;", + "alter table \`test2\` add index \`test2_book_uuid_pk_index\`(\`book_uuid_pk\`);", + "alter table \`test2\` add unique \`test2_book_uuid_pk_unique\`(\`book_uuid_pk\`);", + "alter table \`test2\` add index \`test2_parent_id_index\`(\`parent_id\`);", + "", + "create table \`configuration2\` (\`property\` varchar(255) not null, \`test_id\` int unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`configuration2\` add index \`configuration2_test_id_index\`(\`test_id\`);", + "alter table \`configuration2\` add primary key \`configuration2_pkey\`(\`property\`, \`test_id\`);", + "", + "create table \`publisher2_tests\` (\`id\` int unsigned not null auto_increment primary key, \`publisher2_id\` int unsigned not null, \`test2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`publisher2_tests\` add index \`publisher2_tests_publisher2_id_index\`(\`publisher2_id\`);", + "alter table \`publisher2_tests\` add index \`publisher2_tests_test2_id_index\`(\`test2_id\`);", + "", + "create table \`test2_bars\` (\`test2_id\` int unsigned not null, \`foo_bar2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`test2_bars\` add index \`test2_bars_test2_id_index\`(\`test2_id\`);", + "alter table \`test2_bars\` add index \`test2_bars_foo_bar2_id_index\`(\`foo_bar2_id\`);", + "alter table \`test2_bars\` add primary key \`test2_bars_pkey\`(\`test2_id\`, \`foo_bar2_id\`);", + "", + "create table \`book2_tags\` (\`order\` int unsigned not null auto_increment primary key, \`book2_uuid_pk\` varchar(36) not null, \`book_tag2_id\` bigint unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`book2_tags\` add index \`book2_tags_book2_uuid_pk_index\`(\`book2_uuid_pk\`);", + "alter table \`book2_tags\` add index \`book2_tags_book_tag2_id_index\`(\`book_tag2_id\`);", + "", + "create table \`book_to_tag_unordered\` (\`book2_uuid_pk\` varchar(36) not null, \`book_tag2_id\` bigint unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`book_to_tag_unordered\` add index \`book_to_tag_unordered_book2_uuid_pk_index\`(\`book2_uuid_pk\`);", + "alter table \`book_to_tag_unordered\` add index \`book_to_tag_unordered_book_tag2_id_index\`(\`book_tag2_id\`);", + "alter table \`book_to_tag_unordered\` add primary key \`book_to_tag_unordered_pkey\`(\`book2_uuid_pk\`, \`book_tag2_id\`);", + "", + "create table \`author_to_friend\` (\`author2_1_id\` int unsigned not null, \`author2_2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`author_to_friend\` add index \`author_to_friend_author2_1_id_index\`(\`author2_1_id\`);", + "alter table \`author_to_friend\` add index \`author_to_friend_author2_2_id_index\`(\`author2_2_id\`);", + "alter table \`author_to_friend\` add primary key \`author_to_friend_pkey\`(\`author2_1_id\`, \`author2_2_id\`);", + "", + "create table \`author2_following\` (\`author2_1_id\` int unsigned not null, \`author2_2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`author2_following\` add index \`author2_following_author2_1_id_index\`(\`author2_1_id\`);", + "alter table \`author2_following\` add index \`author2_following_author2_2_id_index\`(\`author2_2_id\`);", + "alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`author2_1_id\`, \`author2_2_id\`);", + "", + "create table \`address2\` (\`author_id\` int unsigned not null, \`value\` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';", + "alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);", + "alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);", + "", + "alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;", + "alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;", + "", + "alter table \`foo_param2\` add constraint \`foo_param2_bar_id_foreign\` foreign key (\`bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade;", + "alter table \`foo_param2\` add constraint \`foo_param2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade;", + "", + "alter table \`car_owner2\` add constraint \`car_owner2_car_name_car_year_foreign\` foreign key (\`car_name\`, \`car_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade;", + "", + "alter table \`user2\` add constraint \`user2_favourite_car_name_favourite_car_year_foreign\` foreign key (\`favourite_car_name\`, \`favourite_car_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade on delete set null;", + "", + "alter table \`user2_sandwiches\` add constraint \`user2_sandwiches_user2_first_name_user2_last_name_foreign\` foreign key (\`user2_first_name\`, \`user2_last_name\`) references \`user2\` (\`first_name\`, \`last_name\`) on update cascade on delete cascade;", + "alter table \`user2_sandwiches\` add constraint \`user2_sandwiches_sandwich_id_foreign\` foreign key (\`sandwich_id\`) references \`sandwich\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`user2_cars\` add constraint \`user2_cars_user2_first_name_user2_last_name_foreign\` foreign key (\`user2_first_name\`, \`user2_last_name\`) references \`user2\` (\`first_name\`, \`last_name\`) on update cascade on delete cascade;", + "alter table \`user2_cars\` add constraint \`user2_cars_car2_name_car2_year_foreign\` foreign key (\`car2_name\`, \`car2_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade on delete cascade;", + "", + "alter table \`base_user2\` add constraint \`base_user2_favourite_employee_id_foreign\` foreign key (\`favourite_employee_id\`) references \`base_user2\` (\`id\`) on update cascade on delete set null;", + "alter table \`base_user2\` add constraint \`base_user2_favourite_manager_id_foreign\` foreign key (\`favourite_manager_id\`) references \`base_user2\` (\`id\`) on update cascade on delete set null;", + "", + "alter table \`author2\` add constraint \`author2_favourite_book_uuid_pk_foreign\` foreign key (\`favourite_book_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update no action on delete cascade;", + "alter table \`author2\` add constraint \`author2_favourite_author_id_foreign\` foreign key (\`favourite_author_id\`) references \`author2\` (\`id\`) on update cascade on delete set null;", + "", + "alter table \`book2\` add constraint \`book2_author_id_foreign\` foreign key (\`author_id\`) references \`author2\` (\`id\`);", + "alter table \`book2\` add constraint \`book2_publisher_id_foreign\` foreign key (\`publisher_id\`) references \`publisher2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`test2\` add constraint \`test2_book_uuid_pk_foreign\` foreign key (\`book_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on delete set null;", + "alter table \`test2\` add constraint \`test2_parent_id_foreign\` foreign key (\`parent_id\`) references \`test2\` (\`id\`) on update cascade on delete set null;", + "", + "alter table \`configuration2\` add constraint \`configuration2_test_id_foreign\` foreign key (\`test_id\`) references \`test2\` (\`id\`) on update cascade;", + "", + "alter table \`publisher2_tests\` add constraint \`publisher2_tests_publisher2_id_foreign\` foreign key (\`publisher2_id\`) references \`publisher2\` (\`id\`) on update cascade on delete cascade;", + "alter table \`publisher2_tests\` add constraint \`publisher2_tests_test2_id_foreign\` foreign key (\`test2_id\`) references \`test2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`test2_bars\` add constraint \`test2_bars_test2_id_foreign\` foreign key (\`test2_id\`) references \`test2\` (\`id\`) on update cascade on delete cascade;", + "alter table \`test2_bars\` add constraint \`test2_bars_foo_bar2_id_foreign\` foreign key (\`foo_bar2_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`book2_tags\` add constraint \`book2_tags_book2_uuid_pk_foreign\` foreign key (\`book2_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update cascade on delete cascade;", + "alter table \`book2_tags\` add constraint \`book2_tags_book_tag2_id_foreign\` foreign key (\`book_tag2_id\`) references \`book_tag2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`book_to_tag_unordered\` add constraint \`book_to_tag_unordered_book2_uuid_pk_foreign\` foreign key (\`book2_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update cascade on delete cascade;", + "alter table \`book_to_tag_unordered\` add constraint \`book_to_tag_unordered_book_tag2_id_foreign\` foreign key (\`book_tag2_id\`) references \`book_tag2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`author_to_friend\` add constraint \`author_to_friend_author2_1_id_foreign\` foreign key (\`author2_1_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + "alter table \`author_to_friend\` add constraint \`author_to_friend_author2_2_id_foreign\` foreign key (\`author2_2_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`author2_following\` add constraint \`author2_following_author2_1_id_foreign\` foreign key (\`author2_1_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + "alter table \`author2_following\` add constraint \`author2_following_author2_2_id_foreign\` foreign key (\`author2_2_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`address2\` add constraint \`address2_author_id_foreign\` foreign key (\`author_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + ], + }, "fileName": "Migration20191013214813.ts", } `; @@ -367,19 +370,38 @@ class Migration20191013214813 extends Migration { this.addSql('alter table \`test2\` drop \`foo___baz\`;'); } + async down() { + this.addSql('alter table \`book2\` add \`foo\` varchar(255) null default lol;'); + + this.addSql('alter table \`test2\` add \`foo___bar\` int unsigned null, add \`foo___baz\` int unsigned null;'); + this.addSql('alter table \`test2\` add constraint \`test2_foo___bar_foreign\` foreign key (\`foo___bar\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;'); + this.addSql('alter table \`test2\` add unique \`test2_foo___bar_unique\`(\`foo___bar\`);'); + this.addSql('alter table \`test2\` add index \`test2_foo___bar_index\`(\`foo___bar\`);'); + } + } exports.Migration20191013214813 = Migration20191013214813; ", - "diff": Array [ - "alter table \`test2\` drop foreign key \`test2_foo___bar_foreign\`;", - "", - "alter table \`book2\` drop \`foo\`;", - "", - "alter table \`test2\` drop index \`test2_foo___bar_unique\`;", - "alter table \`test2\` drop index \`test2_foo___bar_index\`;", - "alter table \`test2\` drop \`foo___bar\`;", - "alter table \`test2\` drop \`foo___baz\`;", - ], + "diff": Object { + "down": Array [ + "alter table \`book2\` add \`foo\` varchar(255) null default lol;", + "", + "alter table \`test2\` add \`foo___bar\` int unsigned null, add \`foo___baz\` int unsigned null;", + "alter table \`test2\` add constraint \`test2_foo___bar_foreign\` foreign key (\`foo___bar\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;", + "alter table \`test2\` add unique \`test2_foo___bar_unique\`(\`foo___bar\`);", + "alter table \`test2\` add index \`test2_foo___bar_index\`(\`foo___bar\`);", + ], + "up": Array [ + "alter table \`test2\` drop foreign key \`test2_foo___bar_foreign\`;", + "", + "alter table \`book2\` drop \`foo\`;", + "", + "alter table \`test2\` drop index \`test2_foo___bar_unique\`;", + "alter table \`test2\` drop index \`test2_foo___bar_index\`;", + "alter table \`test2\` drop \`foo___bar\`;", + "alter table \`test2\` drop \`foo___baz\`;", + ], + }, "fileName": "Migration20191013214813.js", } `; @@ -401,18 +423,37 @@ export class Migration20191013214813 extends Migration { this.addSql('alter table \`test2\` drop \`foo___baz\`;'); } + async down(): Promise { + this.addSql('alter table \`book2\` add \`foo\` varchar(255) null default lol;'); + + this.addSql('alter table \`test2\` add \`foo___bar\` int unsigned null, add \`foo___baz\` int unsigned null;'); + this.addSql('alter table \`test2\` add constraint \`test2_foo___bar_foreign\` foreign key (\`foo___bar\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;'); + this.addSql('alter table \`test2\` add unique \`test2_foo___bar_unique\`(\`foo___bar\`);'); + this.addSql('alter table \`test2\` add index \`test2_foo___bar_index\`(\`foo___bar\`);'); + } + } ", - "diff": Array [ - "alter table \`test2\` drop foreign key \`test2_foo___bar_foreign\`;", - "", - "alter table \`book2\` drop \`foo\`;", - "", - "alter table \`test2\` drop index \`test2_foo___bar_unique\`;", - "alter table \`test2\` drop index \`test2_foo___bar_index\`;", - "alter table \`test2\` drop \`foo___bar\`;", - "alter table \`test2\` drop \`foo___baz\`;", - ], + "diff": Object { + "down": Array [ + "alter table \`book2\` add \`foo\` varchar(255) null default lol;", + "", + "alter table \`test2\` add \`foo___bar\` int unsigned null, add \`foo___baz\` int unsigned null;", + "alter table \`test2\` add constraint \`test2_foo___bar_foreign\` foreign key (\`foo___bar\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;", + "alter table \`test2\` add unique \`test2_foo___bar_unique\`(\`foo___bar\`);", + "alter table \`test2\` add index \`test2_foo___bar_index\`(\`foo___bar\`);", + ], + "up": Array [ + "alter table \`test2\` drop foreign key \`test2_foo___bar_foreign\`;", + "", + "alter table \`book2\` drop \`foo\`;", + "", + "alter table \`test2\` drop index \`test2_foo___bar_unique\`;", + "alter table \`test2\` drop index \`test2_foo___bar_index\`;", + "alter table \`test2\` drop \`foo___bar\`;", + "alter table \`test2\` drop \`foo___baz\`;", + ], + }, "fileName": "migration-20191013214813.ts", } `; @@ -434,18 +475,37 @@ export class Migration20191013214813 extends Migration { this.addSql('alter table \`test2\` drop \`foo___baz\`;'); } + async down(): Promise { + this.addSql('alter table \`book2\` add \`foo\` varchar(255) null default lol;'); + + this.addSql('alter table \`test2\` add \`foo___bar\` int unsigned null, add \`foo___baz\` int unsigned null;'); + this.addSql('alter table \`test2\` add constraint \`test2_foo___bar_foreign\` foreign key (\`foo___bar\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;'); + this.addSql('alter table \`test2\` add unique \`test2_foo___bar_unique\`(\`foo___bar\`);'); + this.addSql('alter table \`test2\` add index \`test2_foo___bar_index\`(\`foo___bar\`);'); + } + } ", - "diff": Array [ - "alter table \`test2\` drop foreign key \`test2_foo___bar_foreign\`;", - "", - "alter table \`book2\` drop \`foo\`;", - "", - "alter table \`test2\` drop index \`test2_foo___bar_unique\`;", - "alter table \`test2\` drop index \`test2_foo___bar_index\`;", - "alter table \`test2\` drop \`foo___bar\`;", - "alter table \`test2\` drop \`foo___baz\`;", - ], + "diff": Object { + "down": Array [ + "alter table \`book2\` add \`foo\` varchar(255) null default lol;", + "", + "alter table \`test2\` add \`foo___bar\` int unsigned null, add \`foo___baz\` int unsigned null;", + "alter table \`test2\` add constraint \`test2_foo___bar_foreign\` foreign key (\`foo___bar\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;", + "alter table \`test2\` add unique \`test2_foo___bar_unique\`(\`foo___bar\`);", + "alter table \`test2\` add index \`test2_foo___bar_index\`(\`foo___bar\`);", + ], + "up": Array [ + "alter table \`test2\` drop foreign key \`test2_foo___bar_foreign\`;", + "", + "alter table \`book2\` drop \`foo\`;", + "", + "alter table \`test2\` drop index \`test2_foo___bar_unique\`;", + "alter table \`test2\` drop index \`test2_foo___bar_index\`;", + "alter table \`test2\` drop \`foo___bar\`;", + "alter table \`test2\` drop \`foo___baz\`;", + ], + }, "fileName": "Migration20191013214813.ts", } `; @@ -618,165 +678,168 @@ export class Migration20191013214813 extends Migration { } ", - "diff": Array [ - "create table \`sandwich\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`price\` int not null) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`publisher2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`type\` enum('local', 'global') not null, \`type2\` enum('LOCAL', 'GLOBAL') not null, \`enum1\` tinyint null, \`enum2\` tinyint null, \`enum3\` tinyint null, \`enum4\` enum('a', 'b', 'c') null, \`enum5\` enum('a') null) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`foo_baz2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`version\` datetime(3) not null default current_timestamp(3)) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`foo_bar2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`name with space\` varchar(255) null, \`baz_id\` int unsigned null, \`foo_bar_id\` int unsigned null, \`version\` datetime not null default current_timestamp, \`blob\` blob null, \`array\` text null, \`object_property\` json null) default character set utf8mb4 engine = InnoDB;", - "alter table \`foo_bar2\` add index \`foo_bar2_baz_id_index\`(\`baz_id\`);", - "alter table \`foo_bar2\` add unique \`foo_bar2_baz_id_unique\`(\`baz_id\`);", - "alter table \`foo_bar2\` add index \`foo_bar2_foo_bar_id_index\`(\`foo_bar_id\`);", - "alter table \`foo_bar2\` add unique \`foo_bar2_foo_bar_id_unique\`(\`foo_bar_id\`);", - "", - "create table \`foo_param2\` (\`bar_id\` int unsigned not null, \`baz_id\` int unsigned not null, \`value\` varchar(255) not null, \`version\` datetime(3) not null default current_timestamp(3)) default character set utf8mb4 engine = InnoDB;", - "alter table \`foo_param2\` add index \`foo_param2_bar_id_index\`(\`bar_id\`);", - "alter table \`foo_param2\` add index \`foo_param2_baz_id_index\`(\`baz_id\`);", - "alter table \`foo_param2\` add primary key \`foo_param2_pkey\`(\`bar_id\`, \`baz_id\`);", - "", - "create table \`dummy2\` (\`id\` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`car2\` (\`name\` varchar(100) not null, \`year\` int unsigned not null, \`price\` int not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`car2\` add index \`car2_name_index\`(\`name\`);", - "alter table \`car2\` add index \`car2_year_index\`(\`year\`);", - "alter table \`car2\` add primary key \`car2_pkey\`(\`name\`, \`year\`);", - "", - "create table \`car_owner2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`car_name\` varchar(100) not null, \`car_year\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`car_owner2\` add index \`car_owner2_car_name_car_year_index\`(\`car_name\`, \`car_year\`);", - "", - "create table \`user2\` (\`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`foo\` int null, \`favourite_car_name\` varchar(100) null, \`favourite_car_year\` int unsigned null) default character set utf8mb4 engine = InnoDB;", - "alter table \`user2\` add index \`user2_favourite_car_name_favourite_car_year_index\`(\`favourite_car_name\`, \`favourite_car_year\`);", - "alter table \`user2\` add unique \`user2_favourite_car_name_favourite_car_year_unique\`(\`favourite_car_name\`, \`favourite_car_year\`);", - "alter table \`user2\` add primary key \`user2_pkey\`(\`first_name\`, \`last_name\`);", - "", - "create table \`user2_sandwiches\` (\`user2_first_name\` varchar(100) not null, \`user2_last_name\` varchar(100) not null, \`sandwich_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`user2_sandwiches\` add index \`user2_sandwiches_sandwich_id_index\`(\`sandwich_id\`);", - "alter table \`user2_sandwiches\` add index \`user2_sandwiches_user2_first_name_user2_last_name_index\`(\`user2_first_name\`, \`user2_last_name\`);", - "alter table \`user2_sandwiches\` add primary key \`user2_sandwiches_pkey\`(\`user2_first_name\`, \`user2_last_name\`, \`sandwich_id\`);", - "", - "create table \`user2_cars\` (\`user2_first_name\` varchar(100) not null, \`user2_last_name\` varchar(100) not null, \`car2_name\` varchar(100) not null, \`car2_year\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`user2_cars\` add index \`user2_cars_user2_first_name_user2_last_name_index\`(\`user2_first_name\`, \`user2_last_name\`);", - "alter table \`user2_cars\` add index \`user2_cars_car2_name_car2_year_index\`(\`car2_name\`, \`car2_year\`);", - "alter table \`user2_cars\` add primary key \`user2_cars_pkey\`(\`user2_first_name\`, \`user2_last_name\`, \`car2_name\`, \`car2_year\`);", - "", - "create table \`book_tag2\` (\`id\` bigint unsigned not null auto_increment primary key, \`name\` varchar(50) not null) default character set utf8mb4 engine = InnoDB;", - "", - "create table \`base_user2\` (\`id\` int unsigned not null auto_increment primary key, \`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`type\` enum('employee', 'manager', 'owner') not null, \`owner_prop\` varchar(255) null, \`favourite_employee_id\` int unsigned null, \`favourite_manager_id\` int unsigned null, \`employee_prop\` int null, \`manager_prop\` varchar(255) null) default character set utf8mb4 engine = InnoDB;", - "alter table \`base_user2\` add index \`base_user2_type_index\`(\`type\`);", - "alter table \`base_user2\` add index \`base_user2_favourite_employee_id_index\`(\`favourite_employee_id\`);", - "alter table \`base_user2\` add index \`base_user2_favourite_manager_id_index\`(\`favourite_manager_id\`);", - "alter table \`base_user2\` add unique \`base_user2_favourite_manager_id_unique\`(\`favourite_manager_id\`);", - "", - "create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int null default null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` text null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int unsigned null) default character set utf8mb4 engine = InnoDB;", - "alter table \`author2\` add index \`custom_email_index_name\`(\`email\`);", - "alter table \`author2\` add unique \`custom_email_unique_name\`(\`email\`);", - "alter table \`author2\` add index \`author2_terms_accepted_index\`(\`terms_accepted\`);", - "alter table \`author2\` add index \`author2_born_index\`(\`born\`);", - "alter table \`author2\` add index \`born_time_idx\`(\`born_time\`);", - "alter table \`author2\` add index \`author2_favourite_book_uuid_pk_index\`(\`favourite_book_uuid_pk\`);", - "alter table \`author2\` add index \`author2_favourite_author_id_index\`(\`favourite_author_id\`);", - "alter table \`author2\` add index \`custom_idx_name_123\`(\`name\`);", - "alter table \`author2\` add index \`author2_name_age_index\`(\`name\`, \`age\`);", - "alter table \`author2\` add unique \`author2_name_email_unique\`(\`name\`, \`email\`);", - "", - "create table \`book2\` (\`uuid_pk\` varchar(36) not null, \`created_at\` datetime(3) not null default current_timestamp(3), \`title\` varchar(255) null default '', \`perex\` text null, \`price\` numeric(8,2) null, \`double\` double null, \`meta\` json null, \`author_id\` int unsigned not null, \`publisher_id\` int unsigned null) default character set utf8mb4 engine = InnoDB;", - "alter table \`book2\` add index \`book2_author_id_index\`(\`author_id\`);", - "alter table \`book2\` add index \`book2_publisher_id_index\`(\`publisher_id\`);", - "alter table \`book2\` add primary key \`book2_pkey\`(\`uuid_pk\`);", - "", - "create table \`test2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) null, \`book_uuid_pk\` varchar(36) null, \`parent_id\` int unsigned null, \`version\` int not null default 1) default character set utf8mb4 engine = InnoDB;", - "alter table \`test2\` add index \`test2_book_uuid_pk_index\`(\`book_uuid_pk\`);", - "alter table \`test2\` add unique \`test2_book_uuid_pk_unique\`(\`book_uuid_pk\`);", - "alter table \`test2\` add index \`test2_parent_id_index\`(\`parent_id\`);", - "", - "create table \`configuration2\` (\`property\` varchar(255) not null, \`test_id\` int unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`configuration2\` add index \`configuration2_test_id_index\`(\`test_id\`);", - "alter table \`configuration2\` add primary key \`configuration2_pkey\`(\`property\`, \`test_id\`);", - "", - "create table \`publisher2_tests\` (\`id\` int unsigned not null auto_increment primary key, \`publisher2_id\` int unsigned not null, \`test2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`publisher2_tests\` add index \`publisher2_tests_publisher2_id_index\`(\`publisher2_id\`);", - "alter table \`publisher2_tests\` add index \`publisher2_tests_test2_id_index\`(\`test2_id\`);", - "", - "create table \`test2_bars\` (\`test2_id\` int unsigned not null, \`foo_bar2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`test2_bars\` add index \`test2_bars_test2_id_index\`(\`test2_id\`);", - "alter table \`test2_bars\` add index \`test2_bars_foo_bar2_id_index\`(\`foo_bar2_id\`);", - "alter table \`test2_bars\` add primary key \`test2_bars_pkey\`(\`test2_id\`, \`foo_bar2_id\`);", - "", - "create table \`book2_tags\` (\`order\` int unsigned not null auto_increment primary key, \`book2_uuid_pk\` varchar(36) not null, \`book_tag2_id\` bigint unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`book2_tags\` add index \`book2_tags_book2_uuid_pk_index\`(\`book2_uuid_pk\`);", - "alter table \`book2_tags\` add index \`book2_tags_book_tag2_id_index\`(\`book_tag2_id\`);", - "", - "create table \`book_to_tag_unordered\` (\`book2_uuid_pk\` varchar(36) not null, \`book_tag2_id\` bigint unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`book_to_tag_unordered\` add index \`book_to_tag_unordered_book2_uuid_pk_index\`(\`book2_uuid_pk\`);", - "alter table \`book_to_tag_unordered\` add index \`book_to_tag_unordered_book_tag2_id_index\`(\`book_tag2_id\`);", - "alter table \`book_to_tag_unordered\` add primary key \`book_to_tag_unordered_pkey\`(\`book2_uuid_pk\`, \`book_tag2_id\`);", - "", - "create table \`author_to_friend\` (\`author2_1_id\` int unsigned not null, \`author2_2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`author_to_friend\` add index \`author_to_friend_author2_1_id_index\`(\`author2_1_id\`);", - "alter table \`author_to_friend\` add index \`author_to_friend_author2_2_id_index\`(\`author2_2_id\`);", - "alter table \`author_to_friend\` add primary key \`author_to_friend_pkey\`(\`author2_1_id\`, \`author2_2_id\`);", - "", - "create table \`author2_following\` (\`author2_1_id\` int unsigned not null, \`author2_2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", - "alter table \`author2_following\` add index \`author2_following_author2_1_id_index\`(\`author2_1_id\`);", - "alter table \`author2_following\` add index \`author2_following_author2_2_id_index\`(\`author2_2_id\`);", - "alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`author2_1_id\`, \`author2_2_id\`);", - "", - "create table \`address2\` (\`author_id\` int unsigned not null, \`value\` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';", - "alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);", - "alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);", - "", - "alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;", - "alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;", - "", - "alter table \`foo_param2\` add constraint \`foo_param2_bar_id_foreign\` foreign key (\`bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade;", - "alter table \`foo_param2\` add constraint \`foo_param2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade;", - "", - "alter table \`car_owner2\` add constraint \`car_owner2_car_name_car_year_foreign\` foreign key (\`car_name\`, \`car_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade;", - "", - "alter table \`user2\` add constraint \`user2_favourite_car_name_favourite_car_year_foreign\` foreign key (\`favourite_car_name\`, \`favourite_car_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade on delete set null;", - "", - "alter table \`user2_sandwiches\` add constraint \`user2_sandwiches_user2_first_name_user2_last_name_foreign\` foreign key (\`user2_first_name\`, \`user2_last_name\`) references \`user2\` (\`first_name\`, \`last_name\`) on update cascade on delete cascade;", - "alter table \`user2_sandwiches\` add constraint \`user2_sandwiches_sandwich_id_foreign\` foreign key (\`sandwich_id\`) references \`sandwich\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`user2_cars\` add constraint \`user2_cars_user2_first_name_user2_last_name_foreign\` foreign key (\`user2_first_name\`, \`user2_last_name\`) references \`user2\` (\`first_name\`, \`last_name\`) on update cascade on delete cascade;", - "alter table \`user2_cars\` add constraint \`user2_cars_car2_name_car2_year_foreign\` foreign key (\`car2_name\`, \`car2_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade on delete cascade;", - "", - "alter table \`base_user2\` add constraint \`base_user2_favourite_employee_id_foreign\` foreign key (\`favourite_employee_id\`) references \`base_user2\` (\`id\`) on update cascade on delete set null;", - "alter table \`base_user2\` add constraint \`base_user2_favourite_manager_id_foreign\` foreign key (\`favourite_manager_id\`) references \`base_user2\` (\`id\`) on update cascade on delete set null;", - "", - "alter table \`author2\` add constraint \`author2_favourite_book_uuid_pk_foreign\` foreign key (\`favourite_book_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update no action on delete cascade;", - "alter table \`author2\` add constraint \`author2_favourite_author_id_foreign\` foreign key (\`favourite_author_id\`) references \`author2\` (\`id\`) on update cascade on delete set null;", - "", - "alter table \`book2\` add constraint \`book2_author_id_foreign\` foreign key (\`author_id\`) references \`author2\` (\`id\`);", - "alter table \`book2\` add constraint \`book2_publisher_id_foreign\` foreign key (\`publisher_id\`) references \`publisher2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`test2\` add constraint \`test2_book_uuid_pk_foreign\` foreign key (\`book_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on delete set null;", - "alter table \`test2\` add constraint \`test2_parent_id_foreign\` foreign key (\`parent_id\`) references \`test2\` (\`id\`) on update cascade on delete set null;", - "", - "alter table \`configuration2\` add constraint \`configuration2_test_id_foreign\` foreign key (\`test_id\`) references \`test2\` (\`id\`) on update cascade;", - "", - "alter table \`publisher2_tests\` add constraint \`publisher2_tests_publisher2_id_foreign\` foreign key (\`publisher2_id\`) references \`publisher2\` (\`id\`) on update cascade on delete cascade;", - "alter table \`publisher2_tests\` add constraint \`publisher2_tests_test2_id_foreign\` foreign key (\`test2_id\`) references \`test2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`test2_bars\` add constraint \`test2_bars_test2_id_foreign\` foreign key (\`test2_id\`) references \`test2\` (\`id\`) on update cascade on delete cascade;", - "alter table \`test2_bars\` add constraint \`test2_bars_foo_bar2_id_foreign\` foreign key (\`foo_bar2_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`book2_tags\` add constraint \`book2_tags_book2_uuid_pk_foreign\` foreign key (\`book2_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update cascade on delete cascade;", - "alter table \`book2_tags\` add constraint \`book2_tags_book_tag2_id_foreign\` foreign key (\`book_tag2_id\`) references \`book_tag2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`book_to_tag_unordered\` add constraint \`book_to_tag_unordered_book2_uuid_pk_foreign\` foreign key (\`book2_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update cascade on delete cascade;", - "alter table \`book_to_tag_unordered\` add constraint \`book_to_tag_unordered_book_tag2_id_foreign\` foreign key (\`book_tag2_id\`) references \`book_tag2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`author_to_friend\` add constraint \`author_to_friend_author2_1_id_foreign\` foreign key (\`author2_1_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - "alter table \`author_to_friend\` add constraint \`author_to_friend_author2_2_id_foreign\` foreign key (\`author2_2_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`author2_following\` add constraint \`author2_following_author2_1_id_foreign\` foreign key (\`author2_1_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - "alter table \`author2_following\` add constraint \`author2_following_author2_2_id_foreign\` foreign key (\`author2_2_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - "", - "alter table \`address2\` add constraint \`address2_author_id_foreign\` foreign key (\`author_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", - ], + "diff": Object { + "down": Array [], + "up": Array [ + "create table \`sandwich\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`price\` int not null) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`publisher2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`type\` enum('local', 'global') not null, \`type2\` enum('LOCAL', 'GLOBAL') not null, \`enum1\` tinyint null, \`enum2\` tinyint null, \`enum3\` tinyint null, \`enum4\` enum('a', 'b', 'c') null, \`enum5\` enum('a') null) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`foo_baz2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`version\` datetime(3) not null default current_timestamp(3)) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`foo_bar2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`name with space\` varchar(255) null, \`baz_id\` int unsigned null, \`foo_bar_id\` int unsigned null, \`version\` datetime not null default current_timestamp, \`blob\` blob null, \`array\` text null, \`object_property\` json null) default character set utf8mb4 engine = InnoDB;", + "alter table \`foo_bar2\` add index \`foo_bar2_baz_id_index\`(\`baz_id\`);", + "alter table \`foo_bar2\` add unique \`foo_bar2_baz_id_unique\`(\`baz_id\`);", + "alter table \`foo_bar2\` add index \`foo_bar2_foo_bar_id_index\`(\`foo_bar_id\`);", + "alter table \`foo_bar2\` add unique \`foo_bar2_foo_bar_id_unique\`(\`foo_bar_id\`);", + "", + "create table \`foo_param2\` (\`bar_id\` int unsigned not null, \`baz_id\` int unsigned not null, \`value\` varchar(255) not null, \`version\` datetime(3) not null default current_timestamp(3)) default character set utf8mb4 engine = InnoDB;", + "alter table \`foo_param2\` add index \`foo_param2_bar_id_index\`(\`bar_id\`);", + "alter table \`foo_param2\` add index \`foo_param2_baz_id_index\`(\`baz_id\`);", + "alter table \`foo_param2\` add primary key \`foo_param2_pkey\`(\`bar_id\`, \`baz_id\`);", + "", + "create table \`dummy2\` (\`id\` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`car2\` (\`name\` varchar(100) not null, \`year\` int unsigned not null, \`price\` int not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`car2\` add index \`car2_name_index\`(\`name\`);", + "alter table \`car2\` add index \`car2_year_index\`(\`year\`);", + "alter table \`car2\` add primary key \`car2_pkey\`(\`name\`, \`year\`);", + "", + "create table \`car_owner2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) not null, \`car_name\` varchar(100) not null, \`car_year\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`car_owner2\` add index \`car_owner2_car_name_car_year_index\`(\`car_name\`, \`car_year\`);", + "", + "create table \`user2\` (\`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`foo\` int null, \`favourite_car_name\` varchar(100) null, \`favourite_car_year\` int unsigned null) default character set utf8mb4 engine = InnoDB;", + "alter table \`user2\` add index \`user2_favourite_car_name_favourite_car_year_index\`(\`favourite_car_name\`, \`favourite_car_year\`);", + "alter table \`user2\` add unique \`user2_favourite_car_name_favourite_car_year_unique\`(\`favourite_car_name\`, \`favourite_car_year\`);", + "alter table \`user2\` add primary key \`user2_pkey\`(\`first_name\`, \`last_name\`);", + "", + "create table \`user2_sandwiches\` (\`user2_first_name\` varchar(100) not null, \`user2_last_name\` varchar(100) not null, \`sandwich_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`user2_sandwiches\` add index \`user2_sandwiches_sandwich_id_index\`(\`sandwich_id\`);", + "alter table \`user2_sandwiches\` add index \`user2_sandwiches_user2_first_name_user2_last_name_index\`(\`user2_first_name\`, \`user2_last_name\`);", + "alter table \`user2_sandwiches\` add primary key \`user2_sandwiches_pkey\`(\`user2_first_name\`, \`user2_last_name\`, \`sandwich_id\`);", + "", + "create table \`user2_cars\` (\`user2_first_name\` varchar(100) not null, \`user2_last_name\` varchar(100) not null, \`car2_name\` varchar(100) not null, \`car2_year\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`user2_cars\` add index \`user2_cars_user2_first_name_user2_last_name_index\`(\`user2_first_name\`, \`user2_last_name\`);", + "alter table \`user2_cars\` add index \`user2_cars_car2_name_car2_year_index\`(\`car2_name\`, \`car2_year\`);", + "alter table \`user2_cars\` add primary key \`user2_cars_pkey\`(\`user2_first_name\`, \`user2_last_name\`, \`car2_name\`, \`car2_year\`);", + "", + "create table \`book_tag2\` (\`id\` bigint unsigned not null auto_increment primary key, \`name\` varchar(50) not null) default character set utf8mb4 engine = InnoDB;", + "", + "create table \`base_user2\` (\`id\` int unsigned not null auto_increment primary key, \`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`type\` enum('employee', 'manager', 'owner') not null, \`owner_prop\` varchar(255) null, \`favourite_employee_id\` int unsigned null, \`favourite_manager_id\` int unsigned null, \`employee_prop\` int null, \`manager_prop\` varchar(255) null) default character set utf8mb4 engine = InnoDB;", + "alter table \`base_user2\` add index \`base_user2_type_index\`(\`type\`);", + "alter table \`base_user2\` add index \`base_user2_favourite_employee_id_index\`(\`favourite_employee_id\`);", + "alter table \`base_user2\` add index \`base_user2_favourite_manager_id_index\`(\`favourite_manager_id\`);", + "alter table \`base_user2\` add unique \`base_user2_favourite_manager_id_unique\`(\`favourite_manager_id\`);", + "", + "create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int null default null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` text null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int unsigned null) default character set utf8mb4 engine = InnoDB;", + "alter table \`author2\` add index \`custom_email_index_name\`(\`email\`);", + "alter table \`author2\` add unique \`custom_email_unique_name\`(\`email\`);", + "alter table \`author2\` add index \`author2_terms_accepted_index\`(\`terms_accepted\`);", + "alter table \`author2\` add index \`author2_born_index\`(\`born\`);", + "alter table \`author2\` add index \`born_time_idx\`(\`born_time\`);", + "alter table \`author2\` add index \`author2_favourite_book_uuid_pk_index\`(\`favourite_book_uuid_pk\`);", + "alter table \`author2\` add index \`author2_favourite_author_id_index\`(\`favourite_author_id\`);", + "alter table \`author2\` add index \`custom_idx_name_123\`(\`name\`);", + "alter table \`author2\` add index \`author2_name_age_index\`(\`name\`, \`age\`);", + "alter table \`author2\` add unique \`author2_name_email_unique\`(\`name\`, \`email\`);", + "", + "create table \`book2\` (\`uuid_pk\` varchar(36) not null, \`created_at\` datetime(3) not null default current_timestamp(3), \`title\` varchar(255) null default '', \`perex\` text null, \`price\` numeric(8,2) null, \`double\` double null, \`meta\` json null, \`author_id\` int unsigned not null, \`publisher_id\` int unsigned null) default character set utf8mb4 engine = InnoDB;", + "alter table \`book2\` add index \`book2_author_id_index\`(\`author_id\`);", + "alter table \`book2\` add index \`book2_publisher_id_index\`(\`publisher_id\`);", + "alter table \`book2\` add primary key \`book2_pkey\`(\`uuid_pk\`);", + "", + "create table \`test2\` (\`id\` int unsigned not null auto_increment primary key, \`name\` varchar(255) null, \`book_uuid_pk\` varchar(36) null, \`parent_id\` int unsigned null, \`version\` int not null default 1) default character set utf8mb4 engine = InnoDB;", + "alter table \`test2\` add index \`test2_book_uuid_pk_index\`(\`book_uuid_pk\`);", + "alter table \`test2\` add unique \`test2_book_uuid_pk_unique\`(\`book_uuid_pk\`);", + "alter table \`test2\` add index \`test2_parent_id_index\`(\`parent_id\`);", + "", + "create table \`configuration2\` (\`property\` varchar(255) not null, \`test_id\` int unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`configuration2\` add index \`configuration2_test_id_index\`(\`test_id\`);", + "alter table \`configuration2\` add primary key \`configuration2_pkey\`(\`property\`, \`test_id\`);", + "", + "create table \`publisher2_tests\` (\`id\` int unsigned not null auto_increment primary key, \`publisher2_id\` int unsigned not null, \`test2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`publisher2_tests\` add index \`publisher2_tests_publisher2_id_index\`(\`publisher2_id\`);", + "alter table \`publisher2_tests\` add index \`publisher2_tests_test2_id_index\`(\`test2_id\`);", + "", + "create table \`test2_bars\` (\`test2_id\` int unsigned not null, \`foo_bar2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`test2_bars\` add index \`test2_bars_test2_id_index\`(\`test2_id\`);", + "alter table \`test2_bars\` add index \`test2_bars_foo_bar2_id_index\`(\`foo_bar2_id\`);", + "alter table \`test2_bars\` add primary key \`test2_bars_pkey\`(\`test2_id\`, \`foo_bar2_id\`);", + "", + "create table \`book2_tags\` (\`order\` int unsigned not null auto_increment primary key, \`book2_uuid_pk\` varchar(36) not null, \`book_tag2_id\` bigint unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`book2_tags\` add index \`book2_tags_book2_uuid_pk_index\`(\`book2_uuid_pk\`);", + "alter table \`book2_tags\` add index \`book2_tags_book_tag2_id_index\`(\`book_tag2_id\`);", + "", + "create table \`book_to_tag_unordered\` (\`book2_uuid_pk\` varchar(36) not null, \`book_tag2_id\` bigint unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`book_to_tag_unordered\` add index \`book_to_tag_unordered_book2_uuid_pk_index\`(\`book2_uuid_pk\`);", + "alter table \`book_to_tag_unordered\` add index \`book_to_tag_unordered_book_tag2_id_index\`(\`book_tag2_id\`);", + "alter table \`book_to_tag_unordered\` add primary key \`book_to_tag_unordered_pkey\`(\`book2_uuid_pk\`, \`book_tag2_id\`);", + "", + "create table \`author_to_friend\` (\`author2_1_id\` int unsigned not null, \`author2_2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`author_to_friend\` add index \`author_to_friend_author2_1_id_index\`(\`author2_1_id\`);", + "alter table \`author_to_friend\` add index \`author_to_friend_author2_2_id_index\`(\`author2_2_id\`);", + "alter table \`author_to_friend\` add primary key \`author_to_friend_pkey\`(\`author2_1_id\`, \`author2_2_id\`);", + "", + "create table \`author2_following\` (\`author2_1_id\` int unsigned not null, \`author2_2_id\` int unsigned not null) default character set utf8mb4 engine = InnoDB;", + "alter table \`author2_following\` add index \`author2_following_author2_1_id_index\`(\`author2_1_id\`);", + "alter table \`author2_following\` add index \`author2_following_author2_2_id_index\`(\`author2_2_id\`);", + "alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`author2_1_id\`, \`author2_2_id\`);", + "", + "create table \`address2\` (\`author_id\` int unsigned not null, \`value\` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';", + "alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);", + "alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);", + "", + "alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;", + "alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;", + "", + "alter table \`foo_param2\` add constraint \`foo_param2_bar_id_foreign\` foreign key (\`bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade;", + "alter table \`foo_param2\` add constraint \`foo_param2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade;", + "", + "alter table \`car_owner2\` add constraint \`car_owner2_car_name_car_year_foreign\` foreign key (\`car_name\`, \`car_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade;", + "", + "alter table \`user2\` add constraint \`user2_favourite_car_name_favourite_car_year_foreign\` foreign key (\`favourite_car_name\`, \`favourite_car_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade on delete set null;", + "", + "alter table \`user2_sandwiches\` add constraint \`user2_sandwiches_user2_first_name_user2_last_name_foreign\` foreign key (\`user2_first_name\`, \`user2_last_name\`) references \`user2\` (\`first_name\`, \`last_name\`) on update cascade on delete cascade;", + "alter table \`user2_sandwiches\` add constraint \`user2_sandwiches_sandwich_id_foreign\` foreign key (\`sandwich_id\`) references \`sandwich\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`user2_cars\` add constraint \`user2_cars_user2_first_name_user2_last_name_foreign\` foreign key (\`user2_first_name\`, \`user2_last_name\`) references \`user2\` (\`first_name\`, \`last_name\`) on update cascade on delete cascade;", + "alter table \`user2_cars\` add constraint \`user2_cars_car2_name_car2_year_foreign\` foreign key (\`car2_name\`, \`car2_year\`) references \`car2\` (\`name\`, \`year\`) on update cascade on delete cascade;", + "", + "alter table \`base_user2\` add constraint \`base_user2_favourite_employee_id_foreign\` foreign key (\`favourite_employee_id\`) references \`base_user2\` (\`id\`) on update cascade on delete set null;", + "alter table \`base_user2\` add constraint \`base_user2_favourite_manager_id_foreign\` foreign key (\`favourite_manager_id\`) references \`base_user2\` (\`id\`) on update cascade on delete set null;", + "", + "alter table \`author2\` add constraint \`author2_favourite_book_uuid_pk_foreign\` foreign key (\`favourite_book_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update no action on delete cascade;", + "alter table \`author2\` add constraint \`author2_favourite_author_id_foreign\` foreign key (\`favourite_author_id\`) references \`author2\` (\`id\`) on update cascade on delete set null;", + "", + "alter table \`book2\` add constraint \`book2_author_id_foreign\` foreign key (\`author_id\`) references \`author2\` (\`id\`);", + "alter table \`book2\` add constraint \`book2_publisher_id_foreign\` foreign key (\`publisher_id\`) references \`publisher2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`test2\` add constraint \`test2_book_uuid_pk_foreign\` foreign key (\`book_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on delete set null;", + "alter table \`test2\` add constraint \`test2_parent_id_foreign\` foreign key (\`parent_id\`) references \`test2\` (\`id\`) on update cascade on delete set null;", + "", + "alter table \`configuration2\` add constraint \`configuration2_test_id_foreign\` foreign key (\`test_id\`) references \`test2\` (\`id\`) on update cascade;", + "", + "alter table \`publisher2_tests\` add constraint \`publisher2_tests_publisher2_id_foreign\` foreign key (\`publisher2_id\`) references \`publisher2\` (\`id\`) on update cascade on delete cascade;", + "alter table \`publisher2_tests\` add constraint \`publisher2_tests_test2_id_foreign\` foreign key (\`test2_id\`) references \`test2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`test2_bars\` add constraint \`test2_bars_test2_id_foreign\` foreign key (\`test2_id\`) references \`test2\` (\`id\`) on update cascade on delete cascade;", + "alter table \`test2_bars\` add constraint \`test2_bars_foo_bar2_id_foreign\` foreign key (\`foo_bar2_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`book2_tags\` add constraint \`book2_tags_book2_uuid_pk_foreign\` foreign key (\`book2_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update cascade on delete cascade;", + "alter table \`book2_tags\` add constraint \`book2_tags_book_tag2_id_foreign\` foreign key (\`book_tag2_id\`) references \`book_tag2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`book_to_tag_unordered\` add constraint \`book_to_tag_unordered_book2_uuid_pk_foreign\` foreign key (\`book2_uuid_pk\`) references \`book2\` (\`uuid_pk\`) on update cascade on delete cascade;", + "alter table \`book_to_tag_unordered\` add constraint \`book_to_tag_unordered_book_tag2_id_foreign\` foreign key (\`book_tag2_id\`) references \`book_tag2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`author_to_friend\` add constraint \`author_to_friend_author2_1_id_foreign\` foreign key (\`author2_1_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + "alter table \`author_to_friend\` add constraint \`author_to_friend_author2_2_id_foreign\` foreign key (\`author2_2_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`author2_following\` add constraint \`author2_following_author2_1_id_foreign\` foreign key (\`author2_1_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + "alter table \`author2_following\` add constraint \`author2_following_author2_2_id_foreign\` foreign key (\`author2_2_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + "", + "alter table \`address2\` add constraint \`address2_author_id_foreign\` foreign key (\`author_id\`) references \`author2\` (\`id\`) on update cascade on delete cascade;", + ], + }, "fileName": "Migration20191013214813.ts", } `; diff --git a/tests/features/schema-generator/__snapshots__/SchemaGenerator.postgres.test.ts.snap b/tests/features/schema-generator/__snapshots__/SchemaGenerator.postgres.test.ts.snap index 5603bc19d941..29207d6f0a28 100644 --- a/tests/features/schema-generator/__snapshots__/SchemaGenerator.postgres.test.ts.snap +++ b/tests/features/schema-generator/__snapshots__/SchemaGenerator.postgres.test.ts.snap @@ -427,8 +427,8 @@ alter table \\"new_table\\" drop column \\"id\\"; alter table \\"new_table\\" drop column \\"updated_at\\"; alter table \\"author2\\" drop constraint if exists \\"author2_name_check\\"; -alter table \\"author2\\" alter column \\"name\\" type int using (\\"name\\"::int); alter table \\"author2\\" alter column \\"name\\" drop default; +alter table \\"author2\\" alter column \\"name\\" type int using (\\"name\\"::int); alter table \\"author2\\" alter column \\"name\\" set not null; alter table \\"author2\\" drop column \\"favourite_book_uuid_pk\\"; diff --git a/tests/features/schema-generator/__snapshots__/adding-composite-fk.postgres.test.ts.snap b/tests/features/schema-generator/__snapshots__/adding-composite-fk.postgres.test.ts.snap index ef865e377f00..7cfd1b2ce58c 100644 --- a/tests/features/schema-generator/__snapshots__/adding-composite-fk.postgres.test.ts.snap +++ b/tests/features/schema-generator/__snapshots__/adding-composite-fk.postgres.test.ts.snap @@ -1,10 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`adding m:1 with composite PK (FK as PK + scalar PK) (GH 1687) schema generator adds the m:1 columns and FK properly 1`] = ` -"set names 'utf8'; -set session_replication_role = 'replica'; +Object { + "down": "alter table \\"state\\" drop constraint \\"state_country_id_foreign\\"; -create table \\"country\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"currency\\" varchar(255) not null, \\"currency_symbol\\" varchar(255) not null); +alter table \\"city\\" drop constraint \\"city_state_country_id_state_id_foreign\\"; + +drop table if exists \\"country\\" cascade; + +drop table if exists \\"state\\" cascade; + +drop table if exists \\"user\\" cascade; + +drop table if exists \\"city\\" cascade; + +", + "up": "create table \\"country\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"currency\\" varchar(255) not null, \\"currency_symbol\\" varchar(255) not null); create table \\"state\\" (\\"country_id\\" int not null, \\"id\\" int not null, \\"name\\" varchar(255) not null); alter table \\"state\\" add constraint \\"state_pkey\\" primary key (\\"country_id\\", \\"id\\"); @@ -20,12 +31,25 @@ alter table \\"state\\" add constraint \\"state_country_id_foreign\\" foreign ke alter table \\"city\\" add constraint \\"city_state_country_id_state_id_foreign\\" foreign key (\\"state_country_id\\", \\"state_id\\") references \\"state\\" (\\"country_id\\", \\"id\\") on update cascade; -set session_replication_role = 'origin'; -" +", +} `; exports[`adding m:1 with composite PK (FK as PK + scalar PK) (GH 1687) schema generator adds the m:1 columns and FK properly 2`] = ` -"alter table \\"user\\" add column \\"city_state_country_id\\" int not null, add column \\"city_state_id\\" int not null, add column \\"city_id\\" int not null; +Object { + "down": "alter table \\"user\\" drop constraint \\"user_city_state_country_id_city_state_id_city_id_foreign\\"; + +alter table \\"user\\" drop constraint if exists \\"user_created_check\\"; +alter table \\"user\\" alter column \\"created\\" type timestamptz using (\\"created\\"::timestamptz); +alter table \\"user\\" drop constraint if exists \\"user_modified_check\\"; +alter table \\"user\\" alter column \\"modified\\" type timestamptz using (\\"modified\\"::timestamptz); +drop index \\"user_city_state_country_id_city_state_id_city_id_index\\"; +alter table \\"user\\" drop column \\"city_state_country_id\\"; +alter table \\"user\\" drop column \\"city_state_id\\"; +alter table \\"user\\" drop column \\"city_id\\"; + +", + "up": "alter table \\"user\\" add column \\"city_state_country_id\\" int not null, add column \\"city_state_id\\" int not null, add column \\"city_id\\" int not null; alter table \\"user\\" drop constraint if exists \\"user_created_check\\"; alter table \\"user\\" alter column \\"created\\" type timestamptz using (\\"created\\"::timestamptz); alter table \\"user\\" drop constraint if exists \\"user_modified_check\\"; @@ -33,5 +57,6 @@ alter table \\"user\\" alter column \\"modified\\" type timestamptz using (\\"mo alter table \\"user\\" add constraint \\"user_city_state_country_id_city_state_id_city_id_foreign\\" foreign key (\\"city_state_country_id\\", \\"city_state_id\\", \\"city_id\\") references \\"city\\" (\\"state_country_id\\", \\"state_id\\", \\"id\\") on update cascade; create index \\"user_city_state_country_id_city_state_id_city_id_index\\" on \\"user\\" (\\"city_state_country_id\\", \\"city_state_id\\", \\"city_id\\"); -" +", +} `; diff --git a/tests/features/schema-generator/__snapshots__/adding-fk-column.sqlite.test.ts.snap b/tests/features/schema-generator/__snapshots__/adding-fk-column.sqlite.test.ts.snap index 164f8a31e515..dd6cbad00958 100644 --- a/tests/features/schema-generator/__snapshots__/adding-fk-column.sqlite.test.ts.snap +++ b/tests/features/schema-generator/__snapshots__/adding-fk-column.sqlite.test.ts.snap @@ -1,9 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`adding FK column (GH 942) schema: adding 1:1 relation 1`] = ` -"alter table \`user\` add column \`profile_id\` text null constraint user_profile_id_foreign references \`profile\` (\`id\`) on update cascade on delete set null; +"pragma foreign_keys = off; + +alter table \`user\` add column \`profile_id\` text null constraint user_profile_id_foreign references \`profile\` (\`id\`) on update cascade on delete set null; create index \`user_profile_id_index\` on \`user\` (\`profile_id\`); create unique index \`user_profile_id_unique\` on \`user\` (\`profile_id\`); +pragma foreign_keys = on; " `; diff --git a/tests/features/schema-generator/__snapshots__/changing-pk-type.mysql.test.ts.snap b/tests/features/schema-generator/__snapshots__/changing-pk-type.mysql.test.ts.snap index 850e544d3f62..7829ce01bf58 100644 --- a/tests/features/schema-generator/__snapshots__/changing-pk-type.mysql.test.ts.snap +++ b/tests/features/schema-generator/__snapshots__/changing-pk-type.mysql.test.ts.snap @@ -1,42 +1,76 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`changing PK column type [mysql] (GH 1480) changing PK type: 0. create schema with text PK 1`] = ` -"create table \`user\` (\`id\` varchar(255) not null) default character set utf8mb4 engine = InnoDB; +Object { + "down": "drop table if exists \`user\`; + +", + "up": "create table \`user\` (\`id\` varchar(255) not null) default character set utf8mb4 engine = InnoDB; alter table \`user\` add primary key \`user_pkey\`(\`id\`); -" +", +} `; exports[`changing PK column type [mysql] (GH 1480) changing PK type: 1. change PK type from text to int 1`] = ` -"alter table \`user\` modify \`id\` int not null; +Object { + "down": "alter table \`user\` modify \`id\` varchar(255) not null; + +", + "up": "alter table \`user\` modify \`id\` int not null; -" +", +} `; exports[`changing PK column type [mysql] (GH 1480) changing PK type: 2. add new PK (make it composite PK) 1`] = ` -"alter table \`user\` add \`id2\` int not null; +Object { + "down": "alter table \`user\` drop primary key; +alter table \`user\` drop \`id2\`; +alter table \`user\` add primary key \`user_pkey\`(\`id\`); + +", + "up": "alter table \`user\` add \`id2\` int not null; alter table \`user\` drop primary key; alter table \`user\` add primary key \`user_pkey\`(\`id\`, \`id2\`); -" +", +} `; exports[`changing PK column type [mysql] (GH 1480) changing PK type: 3. remove old PK (make it single PK again) 1`] = ` -"alter table \`user\` drop primary key; +Object { + "down": "alter table \`user\` add \`id2\` int not null; +alter table \`user\` drop primary key; +alter table \`user\` add primary key \`user_pkey\`(\`id\`, \`id2\`); + +", + "up": "alter table \`user\` drop primary key; alter table \`user\` drop \`id2\`; alter table \`user\` add primary key \`user_pkey\`(\`id\`); -" +", +} `; exports[`changing PK column type [mysql] (GH 1480) changing PK type: 4. change PK type from int to serial 1`] = ` -"alter table \`user\` modify \`id\` int unsigned not null auto_increment; +Object { + "down": "alter table \`user\` modify \`id\` int not null; -" +", + "up": "alter table \`user\` modify \`id\` int unsigned not null auto_increment; + +", +} `; exports[`changing PK column type [mysql] (GH 1480) changing PK type: 5. change PK type from AI int to text 1`] = ` -"alter table \`user\` modify \`id\` varchar(36) not null; +Object { + "down": "alter table \`user\` modify \`id\` int unsigned not null auto_increment; + +", + "up": "alter table \`user\` modify \`id\` varchar(36) not null; -" +", +} `; diff --git a/tests/features/schema-generator/__snapshots__/changing-pk-type.postgres.test.ts.snap b/tests/features/schema-generator/__snapshots__/changing-pk-type.postgres.test.ts.snap index 436b33b30347..97e9b26a5f31 100644 --- a/tests/features/schema-generator/__snapshots__/changing-pk-type.postgres.test.ts.snap +++ b/tests/features/schema-generator/__snapshots__/changing-pk-type.postgres.test.ts.snap @@ -1,49 +1,93 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`changing PK column type [postgres] (GH 1480) change PK type from json to serial: 0. create schema with text PK 1`] = ` -"create table \\"user\\" (\\"id\\" text not null); +exports[`changing PK column type [postgres] (GH 1480) changing PK type: 0. create schema with text PK 1`] = ` +Object { + "down": "drop table if exists \\"user\\" cascade; + +", + "up": "create table \\"user\\" (\\"id\\" varchar(255) not null); alter table \\"user\\" add constraint \\"user_pkey\\" primary key (\\"id\\"); -" +", +} `; -exports[`changing PK column type [postgres] (GH 1480) change PK type from json to serial: 1. change PK type from text to int 1`] = ` -"alter table \\"user\\" drop constraint if exists \\"user_id_check\\"; +exports[`changing PK column type [postgres] (GH 1480) changing PK type: 1. change PK type from text to int 1`] = ` +Object { + "down": "alter table \\"user\\" drop constraint if exists \\"user_id_check\\"; +alter table \\"user\\" alter column \\"id\\" type varchar using (\\"id\\"::varchar); + +", + "up": "alter table \\"user\\" drop constraint if exists \\"user_id_check\\"; alter table \\"user\\" alter column \\"id\\" type int using (\\"id\\"::int); -" +", +} `; -exports[`changing PK column type [postgres] (GH 1480) change PK type from json to serial: 2. add new PK (make it composite PK) 1`] = ` -"alter table \\"user\\" add column \\"id2\\" int not null; +exports[`changing PK column type [postgres] (GH 1480) changing PK type: 2. add new PK (make it composite PK) 1`] = ` +Object { + "down": "alter table \\"user\\" drop constraint \\"user_pkey\\"; +alter table \\"user\\" drop column \\"id2\\"; +alter table \\"user\\" add constraint \\"user_pkey\\" primary key (\\"id\\"); + +", + "up": "alter table \\"user\\" add column \\"id2\\" int not null; alter table \\"user\\" drop constraint \\"user_pkey\\"; alter table \\"user\\" add constraint \\"user_pkey\\" primary key (\\"id\\", \\"id2\\"); -" +", +} `; -exports[`changing PK column type [postgres] (GH 1480) change PK type from json to serial: 3. remove old PK (make it single PK again) 1`] = ` -"alter table \\"user\\" drop constraint \\"user_pkey\\"; +exports[`changing PK column type [postgres] (GH 1480) changing PK type: 3. remove old PK (make it single PK again) 1`] = ` +Object { + "down": "alter table \\"user\\" add column \\"id2\\" int4 not null default null; +alter table \\"user\\" drop constraint \\"user_pkey\\"; +alter table \\"user\\" add constraint \\"user_pkey\\" primary key (\\"id\\", \\"id2\\"); + +", + "up": "alter table \\"user\\" drop constraint \\"user_pkey\\"; alter table \\"user\\" drop column \\"id2\\"; alter table \\"user\\" add constraint \\"user_pkey\\" primary key (\\"id\\"); -" +", +} `; -exports[`changing PK column type [postgres] (GH 1480) change PK type from json to serial: 4. change PK type from int to serial 1`] = ` -"alter table \\"user\\" drop constraint if exists \\"user_id_check\\"; +exports[`changing PK column type [postgres] (GH 1480) changing PK type: 4. change PK type from int to serial 1`] = ` +Object { + "down": "alter table \\"user\\" drop constraint if exists \\"user_id_check\\"; +alter table \\"user\\" alter column \\"id\\" type int4 using (\\"id\\"::int4); +alter table \\"user\\" alter column \\"id\\" drop default; + +", + "up": "alter table \\"user\\" drop constraint if exists \\"user_id_check\\"; alter table \\"user\\" alter column \\"id\\" type int using (\\"id\\"::int); create sequence if not exists \\"user_id_seq\\"; select setval('user_id_seq', (select max(\\"id\\") from \\"user\\")); alter table \\"user\\" alter column \\"id\\" set default nextval('user_id_seq'); -" +", +} `; -exports[`changing PK column type [postgres] (GH 1480) change PK type from json to serial: 5. change PK type from serial to text 1`] = ` -"alter table \\"user\\" drop constraint if exists \\"user_id_check\\"; -alter table \\"user\\" alter column \\"id\\" type text using (\\"id\\"::text); +exports[`changing PK column type [postgres] (GH 1480) changing PK type: 5. change PK type from serial to text 1`] = ` +Object { + "down": "alter table \\"user\\" alter column \\"id\\" type text using (\\"id\\"::text); + +alter table \\"user\\" drop constraint if exists \\"user_id_check\\"; +alter table \\"user\\" alter column \\"id\\" type int4 using (\\"id\\"::int4); +create sequence if not exists \\"user_id_seq\\"; +select setval('user_id_seq', (select max(\\"id\\") from \\"user\\")); +alter table \\"user\\" alter column \\"id\\" set default nextval('user_id_seq'); + +", + "up": "alter table \\"user\\" drop constraint if exists \\"user_id_check\\"; +alter table \\"user\\" alter column \\"id\\" drop default; +alter table \\"user\\" alter column \\"id\\" type uuid using (\\"id\\"::text::uuid); alter table \\"user\\" alter column \\"id\\" drop default; -" +", +} `; diff --git a/tests/features/schema-generator/adding-composite-fk.postgres.test.ts b/tests/features/schema-generator/adding-composite-fk.postgres.test.ts index 8694cbca9852..f095513543d7 100644 --- a/tests/features/schema-generator/adding-composite-fk.postgres.test.ts +++ b/tests/features/schema-generator/adding-composite-fk.postgres.test.ts @@ -124,15 +124,19 @@ describe('adding m:1 with composite PK (FK as PK + scalar PK) (GH 1687)', () => afterAll(() => orm.close(true)); test('schema generator adds the m:1 columns and FK properly', async () => { - const diff0 = await orm.getSchemaGenerator().getUpdateSchemaSQL(); + const diff0 = await orm.getSchemaGenerator().getUpdateSchemaMigrationSQL({ wrap: false }); expect(diff0).toMatchSnapshot(); - await orm.getSchemaGenerator().execute(diff0); + await orm.getSchemaGenerator().execute(diff0.up); orm.getMetadata().reset('User'); await orm.discoverEntity(User1); - const diff1 = await orm.getSchemaGenerator().getUpdateSchemaSQL({ wrap: false }); + const diff1 = await orm.getSchemaGenerator().getUpdateSchemaMigrationSQL({ wrap: false }); expect(diff1).toMatchSnapshot(); - await orm.getSchemaGenerator().execute(diff1); + await orm.getSchemaGenerator().execute(diff1.up); + + // down + await orm.getSchemaGenerator().execute(diff1.down); + await orm.getSchemaGenerator().execute(diff0.down); }); }); diff --git a/tests/features/schema-generator/adding-fk-column.sqlite.test.ts b/tests/features/schema-generator/adding-fk-column.sqlite.test.ts index 43d1d7380f2f..d8f56b0e12a7 100644 --- a/tests/features/schema-generator/adding-fk-column.sqlite.test.ts +++ b/tests/features/schema-generator/adding-fk-column.sqlite.test.ts @@ -47,9 +47,13 @@ describe('adding FK column (GH 942)', () => { test('schema: adding 1:1 relation', async () => { await orm.discoverEntity(User2); orm.getMetadata().reset('User'); - const diff1 = await orm.getSchemaGenerator().getUpdateSchemaSQL({ wrap: false }); + const diff1 = await orm.getSchemaGenerator().getUpdateSchemaSQL(); expect(diff1).toMatchSnapshot(); await orm.getSchemaGenerator().execute(diff1); + + // sqlite does not support automatic down migrations + const diff2 = await orm.getSchemaGenerator().getUpdateSchemaMigrationSQL(); + expect(diff2.down).toBe(''); }); }); diff --git a/tests/features/schema-generator/changing-pk-type.mysql.test.ts b/tests/features/schema-generator/changing-pk-type.mysql.test.ts index a5fd439db558..dce058cd2ee5 100644 --- a/tests/features/schema-generator/changing-pk-type.mysql.test.ts +++ b/tests/features/schema-generator/changing-pk-type.mysql.test.ts @@ -80,19 +80,26 @@ describe('changing PK column type [mysql] (GH 1480)', () => { orm.getMetadata().reset(e1.name); } - const diff = await generator.getUpdateSchemaSQL({ wrap: false }); + const diff = await generator.getUpdateSchemaMigrationSQL({ wrap: false }); expect(diff).toMatchSnapshot(snap); - await generator.execute(diff); + await generator.execute(diff.up); + + return diff.down; }; - await testMigration(User0, undefined, '0. create schema with text PK'); - await testMigration(User0, User1, '1. change PK type from text to int'); - await testMigration(User1, User2, '2. add new PK (make it composite PK)'); - await testMigration(User2, User3, '3. remove old PK (make it single PK again)'); - await testMigration(User3, User4, '4. change PK type from int to serial'); + const down: string[] = []; + down.push(await testMigration(User0, undefined, '0. create schema with text PK')); + down.push(await testMigration(User0, User1, '1. change PK type from text to int')); + down.push(await testMigration(User1, User2, '2. add new PK (make it composite PK)')); + down.push(await testMigration(User2, User3, '3. remove old PK (make it single PK again)')); + down.push(await testMigration(User3, User4, '4. change PK type from int to serial')); await expect(generator.getUpdateSchemaSQL({ wrap: false })).resolves.toBe(''); - await testMigration(User4, User5, '5. change PK type from AI int to text'); + down.push(await testMigration(User4, User5, '5. change PK type from AI int to text')); await expect(generator.getUpdateSchemaSQL({ wrap: false })).resolves.toBe(''); + + for (const sql of down.reverse()) { + await generator.execute(sql); + } }); }); diff --git a/tests/features/schema-generator/changing-pk-type.postgres.test.ts b/tests/features/schema-generator/changing-pk-type.postgres.test.ts index 4554fffb34c1..d150bb017e37 100644 --- a/tests/features/schema-generator/changing-pk-type.postgres.test.ts +++ b/tests/features/schema-generator/changing-pk-type.postgres.test.ts @@ -5,7 +5,7 @@ import { PostgreSqlDriver, SchemaGenerator } from '@mikro-orm/postgresql'; @Entity({ tableName: 'user' }) class User0 { - @PrimaryKey({ type: t.text }) + @PrimaryKey({ type: t.string }) id!: string; } @@ -48,7 +48,7 @@ class User4 { @Entity({ tableName: 'user' }) class User5 { - @PrimaryKey({ type: t.text }) + @PrimaryKey({ type: t.uuid }) id!: string; } @@ -71,7 +71,7 @@ describe('changing PK column type [postgres] (GH 1480)', () => { afterAll(() => orm.close(true)); - test('change PK type from json to serial', async () => { + test('changing PK type', async () => { const generator = orm.getSchemaGenerator(); const testMigration = async (e1: Constructor, e2: Constructor | undefined, snap: string) => { if (e2) { @@ -79,19 +79,26 @@ describe('changing PK column type [postgres] (GH 1480)', () => { orm.getMetadata().reset(e1.name); } - const diff = await generator.getUpdateSchemaSQL({ wrap: false }); + const diff = await generator.getUpdateSchemaMigrationSQL({ wrap: false }); expect(diff).toMatchSnapshot(snap); - await generator.execute(diff); + await generator.execute(diff.up); + + return diff.down; }; - await testMigration(User0, undefined, '0. create schema with text PK'); - await testMigration(User0, User1, '1. change PK type from text to int'); - await testMigration(User1, User2, '2. add new PK (make it composite PK)'); - await testMigration(User2, User3, '3. remove old PK (make it single PK again)'); - await testMigration(User3, User4, '4. change PK type from int to serial'); + const down: string[] = []; + down.push(await testMigration(User0, undefined, '0. create schema with text PK')); + down.push(await testMigration(User0, User1, '1. change PK type from text to int')); + down.push(await testMigration(User1, User2, '2. add new PK (make it composite PK)')); + down.push(await testMigration(User2, User3, '3. remove old PK (make it single PK again)')); + down.push(await testMigration(User3, User4, '4. change PK type from int to serial')); await expect(generator.getUpdateSchemaSQL({ wrap: false })).resolves.toBe(''); - await testMigration(User4, User5, '5. change PK type from serial to text'); + down.push(await testMigration(User4, User5, '5. change PK type from serial to text')); await expect(generator.getUpdateSchemaSQL({ wrap: false })).resolves.toBe(''); + + for (const sql of down.reverse()) { + await generator.execute(sql); + } }); });