|
@@ -8,10 +8,12 @@ import {
|
|
ModelStatic,
|
|
ModelStatic,
|
|
DataTypes,
|
|
DataTypes,
|
|
Utils,
|
|
Utils,
|
|
- ModelOptions
|
|
|
|
|
|
+ ModelOptions,
|
|
|
|
+ Options
|
|
} from "sequelize";
|
|
} from "sequelize";
|
|
import { Dirent } from "fs";
|
|
import { Dirent } from "fs";
|
|
import * as inflection from "inflection";
|
|
import * as inflection from "inflection";
|
|
|
|
+import { SequelizeStorage, Umzug } from "umzug";
|
|
import BaseModule, { ModuleStatus } from "@/BaseModule";
|
|
import BaseModule, { ModuleStatus } from "@/BaseModule";
|
|
import DataModuleJob from "./DataModule/DataModuleJob";
|
|
import DataModuleJob from "./DataModule/DataModuleJob";
|
|
import Job from "@/Job";
|
|
import Job from "@/Job";
|
|
@@ -105,13 +107,11 @@ export class DataModule extends BaseModule {
|
|
await this._stopped();
|
|
await this._stopped();
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * setupSequelize - Setup sequelize instance
|
|
|
|
- */
|
|
|
|
- private async _setupSequelize() {
|
|
|
|
|
|
+ private async _createSequelizeInstance(options: Options = {}) {
|
|
const { username, password, host, port, database } =
|
|
const { username, password, host, port, database } =
|
|
config.get<any>("postgres");
|
|
config.get<any>("postgres");
|
|
- this._sequelize = new Sequelize(database, username, password, {
|
|
|
|
|
|
+
|
|
|
|
+ const sequelize = new Sequelize(database, username, password, {
|
|
host,
|
|
host,
|
|
port,
|
|
port,
|
|
dialect: "postgres",
|
|
dialect: "postgres",
|
|
@@ -121,6 +121,19 @@ export class DataModule extends BaseModule {
|
|
category: "sql",
|
|
category: "sql",
|
|
message
|
|
message
|
|
}),
|
|
}),
|
|
|
|
+ ...options
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ await sequelize.authenticate();
|
|
|
|
+
|
|
|
|
+ return sequelize;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * setupSequelize - Setup sequelize instance
|
|
|
|
+ */
|
|
|
|
+ private async _setupSequelize() {
|
|
|
|
+ this._sequelize = await this._createSequelizeInstance({
|
|
define: {
|
|
define: {
|
|
hooks: this._getSequelizeHooks()
|
|
hooks: this._getSequelizeHooks()
|
|
}
|
|
}
|
|
@@ -171,13 +184,7 @@ export class DataModule extends BaseModule {
|
|
|
|
|
|
await this._sequelize.sync();
|
|
await this._sequelize.sync();
|
|
|
|
|
|
- // TODO move to a better spot and improve
|
|
|
|
- try {
|
|
|
|
- await this._sequelize.query(`DROP TABLE IF EXISTS "minifiedUsers"`);
|
|
|
|
- } catch (err) {}
|
|
|
|
- await this._sequelize.query(
|
|
|
|
- `CREATE OR REPLACE VIEW "minifiedUsers" AS SELECT _id, username, name, role FROM users`
|
|
|
|
- );
|
|
|
|
|
|
+ await this._runMigrations();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -343,6 +350,35 @@ export class DataModule extends BaseModule {
|
|
this._events[EventClass.getName()] = EventClass;
|
|
this._events[EventClass.getName()] = EventClass;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private async _runMigrations() {
|
|
|
|
+ const sequelize = await this._createSequelizeInstance({
|
|
|
|
+ logging: message =>
|
|
|
|
+ this.log({
|
|
|
|
+ type: "debug",
|
|
|
|
+ category: "migrations.sql",
|
|
|
|
+ message
|
|
|
|
+ })
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ const migrator = new Umzug({
|
|
|
|
+ migrations: {
|
|
|
|
+ glob: [
|
|
|
|
+ `${this.constructor.name}/migrations/*.ts`,
|
|
|
|
+ { cwd: __dirname }
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
|
|
+ context: sequelize,
|
|
|
|
+ storage: new SequelizeStorage({
|
|
|
|
+ sequelize: sequelize!
|
|
|
|
+ }),
|
|
|
|
+ logger: console
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ await migrator.up();
|
|
|
|
+
|
|
|
|
+ await sequelize.close();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
export default new DataModule();
|
|
export default new DataModule();
|