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I,+BAAc,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,eAAe,CAAC,aAAa,EAAE,CAAQ,CAAC,CAAC;yBACxK;6BAAM;4BACH,UAAU,CAAC,WAAW,CAAC,GAAG,SAAS,CAAC,SAAS,CAAC;4BAC9C,UAAU,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC;yBACvC;6BAEG,CAAA,IAAI,CAAC,UAAU,CAAC,MAAM,YAAY,yBAAW,CAAA,EAA7C,wBAA6C;wBACvC,WAAW,GAAG,WAA+B,CAAC;wBACpD,WAAW,CAAC,kBAAkB,CAAC,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,QAAS,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;;;wBAEzH,EAAE,GAAG,WAAW,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC;wBACpD,qBAAM,EAAE,CAAC,MAAM,EAAE;iCACZ,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC;iCAC1B,KAAK,CAAI,EAAE,CAAC,MAAM,CAAC,WAAW,CAAC,kBAAe,CAAC;iCAC/C,QAAQ,CAAI,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,aAAU,CAAC;iCACxC,aAAa,CAAC,UAAU,CAAC;iCACzB,OAAO,EAAE,EAAA;;wBALd,SAKc,CAAC;;;;;;KAGtB;IAEL,wBAAC;AAAD,CA3VA,AA2VC,IAAA;AA3VY,8CAAiB","file":"MigrationExecutor.js","sourcesContent":["import {Table} from \"../schema-builder/table/Table\";\nimport {Connection} from \"../connection/Connection\";\nimport {Migration} from \"./Migration\";\nimport {ObjectLiteral} from \"../common/ObjectLiteral\";\nimport {PromiseUtils} from \"../util/PromiseUtils\";\nimport {QueryRunner} from \"../query-runner/QueryRunner\";\nimport {SqlServerDriver} from \"../driver/sqlserver/SqlServerDriver\";\nimport {MssqlParameter} from \"../driver/sqlserver/MssqlParameter\";\nimport {SqlServerConnectionOptions} from \"../driver/sqlserver/SqlServerConnectionOptions\";\nimport {PostgresConnectionOptions} from \"../driver/postgres/PostgresConnectionOptions\";\nimport { MongoDriver } from \"../driver/mongodb/MongoDriver\";\nimport { MongoQueryRunner } from \"../driver/mongodb/MongoQueryRunner\";\n\n/**\n * Executes migrations: runs pending and reverts previously executed migrations.\n */\nexport class MigrationExecutor {\n\n    // -------------------------------------------------------------------------\n    // Public Properties\n    // -------------------------------------------------------------------------\n\n    /**\n     * Indicates if migrations must be executed in a transaction.\n     */\n    transaction: boolean = true;\n\n    // -------------------------------------------------------------------------\n    // Private Properties\n    // -------------------------------------------------------------------------\n\n    private readonly migrationsTable: string;\n    private readonly migrationsTableName: string;\n\n    // -------------------------------------------------------------------------\n    // Constructor\n    // -------------------------------------------------------------------------\n\n    constructor(protected connection: Connection,\n                protected queryRunner?: QueryRunner) {\n\n        const options = <SqlServerConnectionOptions|PostgresConnectionOptions>this.connection.driver.options;\n        this.migrationsTableName = connection.options.migrationsTableName || \"migrations\";\n        this.migrationsTable = this.connection.driver.buildTableName(this.migrationsTableName, options.schema, options.database);\n    }\n\n    // -------------------------------------------------------------------------\n    // Public Methods\n    // -------------------------------------------------------------------------\n\n    /**\n     * Executes all pending migrations. Pending migrations are migrations that are not yet executed,\n     * thus not saved in the database.\n     */\n    async executePendingMigrations(): Promise<Migration[]> {\n\n        const queryRunner = this.queryRunner || this.connection.createQueryRunner(\"master\");\n        // create migrations table if its not created yet\n        await this.createMigrationsTableIfNotExist(queryRunner);\n        // get all migrations that are executed and saved in the database\n        const executedMigrations = await this.loadExecutedMigrations(queryRunner);\n\n        // get the time when last migration was executed\n        let lastTimeExecutedMigration = this.getLatestTimestampMigration(executedMigrations);\n\n        // get all user's migrations in the source code\n        const allMigrations = this.getMigrations();\n\n        // variable to store all migrations we did successefuly\n        const successMigrations: Migration[] = [];\n\n        // find all migrations that needs to be executed\n        const pendingMigrations = allMigrations.filter(migration => {\n            // check if we already have executed migration\n            const executedMigration = executedMigrations.find(executedMigration => executedMigration.name === migration.name);\n            if (executedMigration)\n                return false;\n\n            // migration is new and not executed. now check if its timestamp is correct\n            // if (lastTimeExecutedMigration && migration.timestamp < lastTimeExecutedMigration.timestamp)\n            //     throw new Error(`New migration found: ${migration.name}, however this migration's timestamp is not valid. Migration's timestamp should not be older then migrations already executed in the database.`);\n\n            // every check is passed means that migration was not run yet and we need to run it\n            return true;\n        });\n\n        // if no migrations are pending then nothing to do here\n        if (!pendingMigrations.length) {\n            this.connection.logger.logSchemaBuild(`No migrations are pending`);\n            // if query runner was created by us then release it\n            if (!this.queryRunner)\n                await queryRunner.release();\n            return [];\n        }\n\n        // log information about migration execution\n        this.connection.logger.logSchemaBuild(`${executedMigrations.length} migrations are already loaded in the database.`);\n        this.connection.logger.logSchemaBuild(`${allMigrations.length} migrations were found in the source code.`);\n        if (lastTimeExecutedMigration)\n            this.connection.logger.logSchemaBuild(`${lastTimeExecutedMigration.name} is the last executed migration. It was executed on ${new Date(lastTimeExecutedMigration.timestamp).toString()}.`);\n        this.connection.logger.logSchemaBuild(`${pendingMigrations.length} migrations are new migrations that needs to be executed.`);\n\n        // start transaction if its not started yet\n        let transactionStartedByUs = false;\n        if (this.transaction && !queryRunner.isTransactionActive) {\n            await queryRunner.startTransaction();\n            transactionStartedByUs = true;\n        }\n\n        // run all pending migrations in a sequence\n        try {\n            await PromiseUtils.runInSequence(pendingMigrations, migration => {\n                return migration.instance!.up(queryRunner)\n                    .then(() => { // now when migration is executed we need to insert record about it into the database\n                        return this.insertExecutedMigration(queryRunner, migration);\n                    })\n                    .then(() => { // informative log about migration success\n                        successMigrations.push(migration);\n                        this.connection.logger.logSchemaBuild(`Migration ${migration.name} has been executed successfully.`);\n                    });\n            });\n\n            // commit transaction if we started it\n            if (transactionStartedByUs)\n                await queryRunner.commitTransaction();\n\n        } catch (err) { // rollback transaction if we started it\n            if (transactionStartedByUs) {\n                try { // we throw original error even if rollback thrown an error\n                    await queryRunner.rollbackTransaction();\n                } catch (rollbackError) { }\n            }\n\n            throw err;\n\n        } finally {\n\n            // if query runner was created by us then release it\n            if (!this.queryRunner)\n                await queryRunner.release();\n        }\n        return successMigrations;\n\n    }\n\n    /**\n     * Reverts last migration that were run.\n     */\n    async undoLastMigration(): Promise<void> {\n\n        const queryRunner = this.queryRunner || this.connection.createQueryRunner(\"master\");\n\n        // create migrations table if its not created yet\n        await this.createMigrationsTableIfNotExist(queryRunner);\n\n        // get all migrations that are executed and saved in the database\n        const executedMigrations = await this.loadExecutedMigrations(queryRunner);\n\n        // get the time when last migration was executed\n        let lastTimeExecutedMigration = this.getLatestExecutedMigration(executedMigrations);\n\n        // if no migrations found in the database then nothing to revert\n        if (!lastTimeExecutedMigration) {\n            this.connection.logger.logSchemaBuild(`No migrations was found in the database. Nothing to revert!`);\n            return;\n        }\n\n        // get all user's migrations in the source code\n        const allMigrations = this.getMigrations();\n\n        // find the instance of the migration we need to remove\n        const migrationToRevert = allMigrations.find(migration => migration.name === lastTimeExecutedMigration!.name);\n\n        // if no migrations found in the database then nothing to revert\n        if (!migrationToRevert)\n            throw new Error(`No migration ${lastTimeExecutedMigration.name} was found in the source code. Make sure you have this migration in your codebase and its included in the connection options.`);\n\n        // log information about migration execution\n        this.connection.logger.logSchemaBuild(`${executedMigrations.length} migrations are already loaded in the database.`);\n        this.connection.logger.logSchemaBuild(`${lastTimeExecutedMigration.name} is the last executed migration. It was executed on ${new Date(lastTimeExecutedMigration.timestamp).toString()}.`);\n        this.connection.logger.logSchemaBuild(`Now reverting it...`);\n\n        // start transaction if its not started yet\n        let transactionStartedByUs = false;\n        if (this.transaction && !queryRunner.isTransactionActive) {\n            await queryRunner.startTransaction();\n            transactionStartedByUs = true;\n        }\n\n        try {\n            await migrationToRevert.instance!.down(queryRunner);\n            await this.deleteExecutedMigration(queryRunner, migrationToRevert);\n            this.connection.logger.logSchemaBuild(`Migration ${migrationToRevert.name} has been reverted successfully.`);\n\n            // commit transaction if we started it\n            if (transactionStartedByUs)\n                await queryRunner.commitTransaction();\n\n        } catch (err) { // rollback transaction if we started it\n            if (transactionStartedByUs) {\n                try { // we throw original error even if rollback thrown an error\n                    await queryRunner.rollbackTransaction();\n                } catch (rollbackError) { }\n            }\n\n            throw err;\n\n        } finally {\n\n            // if query runner was created by us then release it\n            if (!this.queryRunner)\n                await queryRunner.release();\n        }\n    }\n\n    // -------------------------------------------------------------------------\n    // Protected Methods\n    // -------------------------------------------------------------------------\n\n    /**\n     * Creates table \"migrations\" that will store information about executed migrations.\n     */\n    protected async createMigrationsTableIfNotExist(queryRunner: QueryRunner): Promise<void> {\n        // If driver is mongo no need to create\n        if (this.connection.driver instanceof MongoDriver) {\n            return;\n        }\n        const tableExist = await queryRunner.hasTable(this.migrationsTable); // todo: table name should be configurable\n        if (!tableExist) {\n            await queryRunner.createTable(new Table(\n                {\n                    name: this.migrationsTable,\n                    columns: [\n                        {\n                            name: \"id\",\n                            type: this.connection.driver.normalizeType({type: this.connection.driver.mappedDataTypes.migrationId}),\n                            isGenerated: true,\n                            generationStrategy: \"increment\",\n                            isPrimary: true,\n                            isNullable: false\n                        },\n                        {\n                            name: \"timestamp\",\n                            type: this.connection.driver.normalizeType({type: this.connection.driver.mappedDataTypes.migrationTimestamp}),\n                            isPrimary: false,\n                            isNullable: false\n                        },\n                        {\n                            name: \"name\",\n                            type: this.connection.driver.normalizeType({type: this.connection.driver.mappedDataTypes.migrationName}),\n                            isNullable: false\n                        },\n                    ]\n                },\n            ));\n        }\n    }\n\n    /**\n     * Loads all migrations that were executed and saved into the database.\n     */\n    protected async loadExecutedMigrations(queryRunner: QueryRunner): Promise<Migration[]> {\n        if (this.connection.driver instanceof MongoDriver) {\n            const mongoRunner = queryRunner as MongoQueryRunner;\n            return await mongoRunner.databaseConnection.db(this.connection.driver.database!).collection(this.migrationsTableName).find().toArray();\n        } else {\n            const migrationsRaw: ObjectLiteral[] = await this.connection.manager\n            .createQueryBuilder(queryRunner)\n            .select()\n            .from(this.migrationsTable, this.migrationsTableName)\n            .getRawMany();\n            return migrationsRaw.map(migrationRaw => {\n                return new Migration(parseInt(migrationRaw[\"id\"]), parseInt(migrationRaw[\"timestamp\"]), migrationRaw[\"name\"]);\n            });\n        }\n    }\n\n    /**\n     * Gets all migrations that setup for this connection.\n     */\n    protected getMigrations(): Migration[] {\n        const migrations = this.connection.migrations.map(migration => {\n            const migrationClassName = (migration.constructor as any).name;\n            const migrationTimestamp = parseInt(migrationClassName.substr(-13));\n            if (!migrationTimestamp)\n                throw new Error(`${migrationClassName} migration name is wrong. Migration class name should have a JavaScript timestamp appended.`);\n\n            return new Migration(undefined, migrationTimestamp, migrationClassName, migration);\n        });\n\n        // sort them by timestamp\n        return migrations.sort((a, b) => a.timestamp - b.timestamp);\n    }\n\n    /**\n     * Finds the latest migration (sorts by timestamp) in the given array of migrations.\n     */\n    protected getLatestTimestampMigration(migrations: Migration[]): Migration|undefined {\n        const sortedMigrations = migrations.map(migration => migration).sort((a, b) => (a.timestamp - b.timestamp) * -1);\n        return sortedMigrations.length > 0 ? sortedMigrations[0] : undefined;\n    }\n\n    /**\n     * Finds the latest migration (sorts by id) in the given array of migrations.\n     */\n    protected getLatestExecutedMigration(migrations: Migration[]): Migration|undefined {\n        const sortedMigrations = migrations.map(migration => migration).sort((a, b) => ((a.id || 0) - (b.id || 0)) * -1);\n        return sortedMigrations.length > 0 ? sortedMigrations[0] : undefined;\n    }\n\n    /**\n     * Inserts new executed migration's data into migrations table.\n     */\n    protected async insertExecutedMigration(queryRunner: QueryRunner, migration: Migration): Promise<void> {\n        const values: ObjectLiteral = {};\n        if (this.connection.driver instanceof SqlServerDriver) {\n            values[\"timestamp\"] = new MssqlParameter(migration.timestamp, this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.migrationTimestamp }) as any);\n            values[\"name\"] = new MssqlParameter(migration.name, this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.migrationName }) as any);\n        } else {\n            values[\"timestamp\"] = migration.timestamp;\n            values[\"name\"] = migration.name;\n        }\n        if (this.connection.driver instanceof MongoDriver) {  \n            const mongoRunner = queryRunner as MongoQueryRunner;\n            mongoRunner.databaseConnection.db(this.connection.driver.database!).collection(this.migrationsTableName).insert(values);               \n        } else {\n            const qb = queryRunner.manager.createQueryBuilder();\n            await qb.insert()\n                .into(this.migrationsTable)\n                .values(values)\n                .execute();\n        }\n    }\n\n    /**\n     * Delete previously executed migration's data from the migrations table.\n     */\n    protected async deleteExecutedMigration(queryRunner: QueryRunner, migration: Migration): Promise<void> {\n\n        const conditions: ObjectLiteral = {};\n        if (this.connection.driver instanceof SqlServerDriver) {\n            conditions[\"timestamp\"] = new MssqlParameter(migration.timestamp, this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.migrationTimestamp }) as any);\n            conditions[\"name\"] = new MssqlParameter(migration.name, this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.migrationName }) as any);\n        } else {\n            conditions[\"timestamp\"] = migration.timestamp;\n            conditions[\"name\"] = migration.name;\n        }\n\n        if (this.connection.driver instanceof MongoDriver) {\n            const mongoRunner = queryRunner as MongoQueryRunner;\n            mongoRunner.databaseConnection.db(this.connection.driver.database!).collection(this.migrationsTableName).deleteOne(conditions);               \n        } else {\n            const qb = queryRunner.manager.createQueryBuilder();\n            await qb.delete()\n                .from(this.migrationsTable)\n                .where(`${qb.escape(\"timestamp\")} = :timestamp`)\n                .andWhere(`${qb.escape(\"name\")} = :name`)\n                .setParameters(conditions)\n                .execute();\n        }\n\n    }\n\n}\n"],"sourceRoot":".."}