|
|
@@ -5,6 +5,7 @@ import { Database } from 'sqlite3';
|
|
|
import { Logger } from '../../../common/util/logger.class';
|
|
|
|
|
|
import { MigrationException } from '../lib/migration-exception.class';
|
|
|
+import { MigrationRunner } from '../lib/migration-runner.interface';
|
|
|
import { SQLiteController } from './sqlite-controller.base';
|
|
|
|
|
|
export class DBMigration extends SQLiteController {
|
|
|
@@ -26,7 +27,7 @@ export class DBMigration extends SQLiteController {
|
|
|
Logger.debug('[DEBUG] lastid', lastID);
|
|
|
|
|
|
for (const file of files) {
|
|
|
- const m = /^(\d{12})_(.*)\.sql$/.exec(file);
|
|
|
+ const m = /^(\d{12})_(.*)\.(sql|js)$/.exec(file);
|
|
|
|
|
|
if (!m) {
|
|
|
throw new MigrationException(`File ${file} does not match migration file pattern. Aborted processing migrations.`);
|
|
|
@@ -35,21 +36,45 @@ export class DBMigration extends SQLiteController {
|
|
|
const id = Number(m[1]);
|
|
|
if (id <= lastID) continue;
|
|
|
|
|
|
- const migFilepath = path.join(migrationsDir, file);
|
|
|
- const migration = await fsp.readFile(migFilepath, { encoding: 'utf-8' });
|
|
|
-
|
|
|
- Logger.info('[INFO] Applying DB migration', file);
|
|
|
- await this.beginTransaction();
|
|
|
- try {
|
|
|
- await this.exec(migration);
|
|
|
- await this.run('INSERT INTO db_migrations(id, title, migrated) VALUES(?, ?, ?);', [id, m[2], new Date().getTime()]);
|
|
|
- await this.commit();
|
|
|
- Logger.info('[INFO] DB migration', file, 'succeeded.');
|
|
|
- } catch (error) {
|
|
|
- Logger.error('[ERROR] DB migration failed at', file, '- Rolling back...');
|
|
|
- await this.rollback();
|
|
|
-
|
|
|
- throw error;
|
|
|
+ if (m[2] === 'sql') {
|
|
|
+ const migFilepath = path.join(migrationsDir, file);
|
|
|
+ const migration = await fsp.readFile(migFilepath, { encoding: 'utf-8' });
|
|
|
+
|
|
|
+ Logger.info('[INFO] Applying SQL DB migration', file);
|
|
|
+ await this.beginTransaction();
|
|
|
+ try {
|
|
|
+ await this.exec(migration);
|
|
|
+ await this.run('INSERT INTO db_migrations(id, title, migrated) VALUES(?, ?, ?);', [id, m[2], new Date().getTime()]);
|
|
|
+ await this.commit();
|
|
|
+ Logger.info('[INFO] DB migration', file, 'succeeded.');
|
|
|
+ } catch (error) {
|
|
|
+ Logger.error('[ERROR] DB migration failed at', file, '- Rolling back...');
|
|
|
+ await this.rollback();
|
|
|
+
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const migFilepath = path.join(migrationsDir, file.substring(0, file.length - 3));
|
|
|
+ const imp = require(migFilepath).default;
|
|
|
+ if (typeof imp !== 'function') {
|
|
|
+ console.log('TYPE', typeof imp, 'KEYS', Object.keys(imp));
|
|
|
+ throw new MigrationException(`File ${file} is not a valid <MigrationRunner> implementation!`);
|
|
|
+ }
|
|
|
+ const mig = imp as MigrationRunner;
|
|
|
+
|
|
|
+ Logger.info('[INFO] Applying TypeScript DB migration', file);
|
|
|
+ await this.beginTransaction();
|
|
|
+ try {
|
|
|
+ await mig(this);
|
|
|
+ await this.run('INSERT INTO db_migrations(id, title, migrated) VALUES(?, ?, ?);', [id, m[2], new Date().getTime()]);
|
|
|
+ await this.commit();
|
|
|
+ Logger.info('[INFO] DB migration', file, 'succeeded.');
|
|
|
+ } catch (error) {
|
|
|
+ Logger.error('[ERROR] DB migration failed at', file, '- Rolling back...');
|
|
|
+ await this.rollback();
|
|
|
+
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|