diff --git a/src/handlers/migrateMigrationsHandler.ts b/src/handlers/migrateMigrationsHandler.ts index 490c1fb..9eff065 100644 --- a/src/handlers/migrateMigrationsHandler.ts +++ b/src/handlers/migrateMigrationsHandler.ts @@ -12,6 +12,6 @@ export const migrateMigrationsHandler = (direction: Direction) => () => { console.error('Database creds not found in config') process.exit(1) } - const migrator = new Migrator(cfg) + const migrator = new Migrator(cfg, configParser) migrator.migrate(direction) } diff --git a/src/migrator/migrator.ts b/src/migrator/migrator.ts index 5e9cd80..924b90b 100644 --- a/src/migrator/migrator.ts +++ b/src/migrator/migrator.ts @@ -1,4 +1,4 @@ -import { Config } from '../config-file/ConfigParser' +import { Config, ConfigParser } from '../config-file/ConfigParser' import ts from 'typescript' import { Kysely, @@ -19,7 +19,10 @@ export class Migrator { #db: Kysely #migrator: KyselyMigrator - constructor(public cfg: Config) { + constructor( + public cfg: Config, + public configParser: ConfigParser + ) { this.#db = this.#createDbConnector() this.#migrator = this.#createDbMigrator() } @@ -32,13 +35,20 @@ export class Migrator { }) } + #getMigrationDir = () => { + if (this.cfg.useJsExtension) { + return this.configParser.getMigrationDir() + } + return path.resolve('tempMigrations') + } + #createDbMigrator = () => { return new KyselyMigrator({ db: this.#db, provider: new FileMigrationProvider({ fs, path, - migrationFolder: path.resolve('tempMigrations'), + migrationFolder: this.#getMigrationDir(), }), }) } @@ -58,6 +68,7 @@ export class Migrator { } } #deleteDirectory() { + if (!fileSys.existsSync(this.#tempFileDir)) return this.#deleteDirectoryContents() fileSys.rmdirSync(this.#tempFileDir) }