From 34cb07e5a88bc7201d2ad20de101bbc78ae4a80a Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Mon, 21 Oct 2019 17:43:59 +0200 Subject: [PATCH] feat(core): Allow log level to be set in migration helpers --- packages/core/src/migrate.ts | 40 +++++++++++++----------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/packages/core/src/migrate.ts b/packages/core/src/migrate.ts index 754465ddd0..f08b4ea65a 100644 --- a/packages/core/src/migrate.ts +++ b/packages/core/src/migrate.ts @@ -2,7 +2,7 @@ import chalk from 'chalk'; import fs from 'fs-extra'; import path from 'path'; -import { Connection, createConnection } from 'typeorm'; +import { Connection, ConnectionOptions, createConnection } from 'typeorm'; import { MysqlDriver } from 'typeorm/driver/mysql/MysqlDriver'; import { camelCase } from 'typeorm/util/StringUtils'; @@ -38,15 +38,7 @@ export interface MigrationOptions { */ export async function runMigrations(userConfig: Partial) { const config = await preBootstrapConfig(userConfig); - Object.assign(config.dbConnectionOptions, { - subscribers: [], - synchronize: false, - migrationsRun: false, - dropSchema: false, - logger: 'advanced-console', - logging: ['query', 'error', 'schema'], - }); - const connection = await createConnection(config.dbConnectionOptions); + const connection = await createConnection(createConnectionOptions(userConfig)); await disableForeignKeysForSqLite(connection, () => connection.runMigrations({ transaction: true })); await connection.close(); } @@ -60,15 +52,7 @@ export async function runMigrations(userConfig: Partial) { */ export async function revertLastMigration(userConfig: Partial) { const config = await preBootstrapConfig(userConfig); - Object.assign(config.dbConnectionOptions, { - subscribers: [], - synchronize: false, - migrationsRun: false, - dropSchema: false, - logger: 'advanced-console', - logging: ['query', 'error', 'schema'], - }); - const connection = await createConnection(config.dbConnectionOptions); + const connection = await createConnection(createConnectionOptions(userConfig)); await disableForeignKeysForSqLite(connection, () => connection.undoLastMigration({ transaction: true })); await connection.close(); } @@ -83,13 +67,7 @@ export async function revertLastMigration(userConfig: Partial) { */ export async function generateMigration(userConfig: Partial, options: MigrationOptions) { const config = await preBootstrapConfig(userConfig); - Object.assign(config.dbConnectionOptions, { - synchronize: false, - migrationsRun: false, - dropSchema: false, - logging: false, - }); - const connection = await createConnection(config.dbConnectionOptions); + const connection = await createConnection(createConnectionOptions(userConfig)); // TODO: This can hopefully be simplified if/when TypeORM exposes this CLI command directly. // See https://github.com/typeorm/typeorm/issues/4494 @@ -161,6 +139,16 @@ export async function generateMigration(userConfig: Partial, opti await connection.close(); } +function createConnectionOptions(userConfig: Partial): ConnectionOptions { + return Object.assign({ logging: ['query', 'error', 'schema'] }, userConfig.dbConnectionOptions, { + subscribers: [], + synchronize: false, + migrationsRun: false, + dropSchema: false, + logger: 'advanced-console', + }); +} + /** * There is a bug in TypeORM which causes db schema changes to fail with SQLite. This * is a work-around for the issue.