diff --git a/migrations/index.ts b/migrations/index.ts index ee974932..8929e494 100644 --- a/migrations/index.ts +++ b/migrations/index.ts @@ -6,8 +6,11 @@ import { Sequelize } from 'sequelize-typescript' const logger = loggingFactory('db:migrations') +type UpOptions = string | string[] | Umzug.UpToOptions | Umzug.UpDownMigrationsOptions +type DownOptions = string | string[] | Umzug.DownToOptions | Umzug.UpDownMigrationsOptions + export class Migration { - private umzugIns: Umzug.Umzug + private readonly umzugIns: Umzug.Umzug constructor (sequelize: Sequelize) { this.umzugIns = new Umzug({ @@ -23,12 +26,12 @@ export class Migration { } // eslint-disable-next-line require-await - async up (options?: string | string[] | Umzug.UpToOptions | Umzug.UpDownMigrationsOptions): Promise { + async up (options?: UpOptions): Promise { return this.umzugIns.up(options as any) } // eslint-disable-next-line require-await - async down (options?: string | string[] | Umzug.DownToOptions | Umzug.UpDownMigrationsOptions): Promise { + async down (options?: DownOptions): Promise { return this.umzugIns.down(options as any) } @@ -39,7 +42,9 @@ export class Migration { // eslint-disable-next-line require-await async pending (): Promise { return this.umzugIns.pending().catch(e => { - if (e.code === 'ENOENT') return [] + if (e.code === 'ENOENT') { + return [] + } throw e }) } @@ -55,7 +60,9 @@ export default class DbMigration { static getInstance (sequelize?: Sequelize): Migration { if (!DbMigration.ins) { - if (!sequelize) throw new Error('You need to provide Sequelize instance') + if (!sequelize) { + throw new Error('You need to provide Sequelize instance') + } DbMigration.ins = new Migration(sequelize) } diff --git a/src/cli/db-migration.ts b/src/cli/db-migration.ts index e24780ef..ff88e956 100644 --- a/src/cli/db-migration.ts +++ b/src/cli/db-migration.ts @@ -79,7 +79,9 @@ export default class DbMigrationCommand extends BaseCLICommand { : `${parsed.base}.sqlite` ) } else { - if (db[db.length - 1] === '/') throw new Error('Path should include the file name') + if (db[db.length - 1] === '/') { + throw new Error('Path should include the file name') + } return path.resolve(`${db}${parsed.ext ? '' : '.sqlite'}`) } } @@ -118,23 +120,31 @@ export default class DbMigrationCommand extends BaseCLICommand { async run (): Promise { const { flags: originalFlags } = this.parse(DbMigrationCommand) - const flags = originalFlags as OutputFlags + const parsedFlags = originalFlags as OutputFlags - if (flags.db) { - config.util.extendDeep(config, { db: `sqlite://${this.resolveDbPath(flags.db)}` }) + if (parsedFlags.db) { + config.util.extendDeep(config, { db: `sqlite://${this.resolveDbPath(parsedFlags.db)}` }) } - if (flags.generate) this.generateMigration(flags.generate) + if (parsedFlags.generate) { + this.generateMigration(parsedFlags.generate) + } // Init database connection const sequelize = sequelizeFactory() DbMigration.getInstance(sequelize) - if (!flags.up && !flags.down && !flags.generate) throw new Error('One of \'--generate, --up, --down\' required') + if (!parsedFlags.up && !parsedFlags.down && !parsedFlags.generate) { + throw new Error('One of \'--generate, --up, --down\' required') + } - if (flags.up) await this.migrate(flags.migration, flags) + if (parsedFlags.up) { + await this.migrate(parsedFlags.migration, parsedFlags) + } - if (flags.down) await this.undo(flags.migration, flags) + if (parsedFlags.down) { + await this.undo(parsedFlags.migration, parsedFlags) + } this.exit() } diff --git a/src/sequelize.ts b/src/sequelize.ts index bb624178..35053e52 100644 --- a/src/sequelize.ts +++ b/src/sequelize.ts @@ -60,7 +60,7 @@ export default async function (app: Application): Promise { // Run migration logger.info('Run DB Migrations') - await DbMigration.getInstance(app.get('sequelizeSync')).up() + await DbMigration.getInstance(sequelize).up() app.setup = function (...args): ReturnType { const result = oldSetup.apply(this, args)