Ver Fonte

refactor: Adds minimal users view migration

Owen Diffey há 4 meses atrás
pai
commit
c048d2454d

+ 49 - 13
backend/src/modules/DataModule.ts

@@ -8,10 +8,12 @@ import {
 	ModelStatic,
 	DataTypes,
 	Utils,
-	ModelOptions
+	ModelOptions,
+	Options
 } from "sequelize";
 import { Dirent } from "fs";
 import * as inflection from "inflection";
+import { SequelizeStorage, Umzug } from "umzug";
 import BaseModule, { ModuleStatus } from "@/BaseModule";
 import DataModuleJob from "./DataModule/DataModuleJob";
 import Job from "@/Job";
@@ -105,13 +107,11 @@ export class DataModule extends BaseModule {
 		await this._stopped();
 	}
 
-	/**
-	 * setupSequelize - Setup sequelize instance
-	 */
-	private async _setupSequelize() {
+	private async _createSequelizeInstance(options: Options = {}) {
 		const { username, password, host, port, database } =
 			config.get<any>("postgres");
-		this._sequelize = new Sequelize(database, username, password, {
+
+		const sequelize = new Sequelize(database, username, password, {
 			host,
 			port,
 			dialect: "postgres",
@@ -121,6 +121,19 @@ export class DataModule extends BaseModule {
 					category: "sql",
 					message
 				}),
+			...options
+		});
+
+		await sequelize.authenticate();
+
+		return sequelize;
+	}
+
+	/**
+	 * setupSequelize - Setup sequelize instance
+	 */
+	private async _setupSequelize() {
+		this._sequelize = await this._createSequelizeInstance({
 			define: {
 				hooks: this._getSequelizeHooks()
 			}
@@ -171,13 +184,7 @@ export class DataModule extends BaseModule {
 
 		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;
 		});
 	}
+
+	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();

+ 26 - 0
backend/src/modules/DataModule/migrations/1725485642-create-minimal-users-view.ts

@@ -0,0 +1,26 @@
+import { Sequelize, QueryTypes } from "sequelize";
+import { MigrationParams } from "umzug";
+
+export const up = async ({
+	context: sequelize
+}: MigrationParams<Sequelize>) => {
+	// TODO: Remove this when sync is no longer used
+	await sequelize.getQueryInterface().dropTable("minifiedUsers");
+
+	await sequelize.query(
+		'CREATE VIEW "minifiedUsers" AS SELECT _id, username, name, role FROM users',
+		{
+			raw: true,
+			type: QueryTypes.RAW
+		}
+	);
+};
+
+export const down = async ({
+	context: sequelize
+}: MigrationParams<Sequelize>) => {
+	await sequelize.query('DROP VIEW IF EXISTS "minifiedUsers"', {
+		raw: true,
+		type: QueryTypes.RAW
+	});
+};