Skip to content

Commit

Permalink
feat(core): Allow log level to be set in migration helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Oct 21, 2019
1 parent 4f2c4df commit 34cb07e
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions packages/core/src/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -38,15 +38,7 @@ export interface MigrationOptions {
*/
export async function runMigrations(userConfig: Partial<VendureConfig>) {
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();
}
Expand All @@ -60,15 +52,7 @@ export async function runMigrations(userConfig: Partial<VendureConfig>) {
*/
export async function revertLastMigration(userConfig: Partial<VendureConfig>) {
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();
}
Expand All @@ -83,13 +67,7 @@ export async function revertLastMigration(userConfig: Partial<VendureConfig>) {
*/
export async function generateMigration(userConfig: Partial<VendureConfig>, 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
Expand Down Expand Up @@ -161,6 +139,16 @@ export async function generateMigration(userConfig: Partial<VendureConfig>, opti
await connection.close();
}

function createConnectionOptions(userConfig: Partial<VendureConfig>): 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.
Expand Down

0 comments on commit 34cb07e

Please sign in to comment.