From 3bf4f93b74c1ad4f32252577ea1d294dbd07463c Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Thu, 23 Apr 2020 15:43:15 +0200 Subject: [PATCH] Force an application to register migrations --- package.json | 2 +- src/Application.ts | 18 ++++++++++-------- src/Utils.ts | 4 ++++ src/db/MysqlConnectionManager.ts | 11 ++++++++++- test/Model.test.ts | 6 +++--- test/_migrations.ts | 7 +++++++ test/_registerMigrations.ts | 15 --------------- yarn.lock | 4 ++-- 8 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 test/_migrations.ts delete mode 100644 test/_registerMigrations.ts diff --git a/package.json b/package.json index 9b73b1c..735b30e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wms-core", - "version": "0.2.0", + "version": "0.2.5", "description": "Node web framework", "repository": "git@gitlab.com:ArisuOngaku/wms-core.git", "author": "Alice Gaudon ", diff --git a/src/Application.ts b/src/Application.ts index 518327a..ec0f38b 100644 --- a/src/Application.ts +++ b/src/Application.ts @@ -1,17 +1,14 @@ import express, {NextFunction, Request, Response, Router} from 'express'; -import { - BadRequestError, - HttpError, - NotFoundHttpError, - ServerError, - ServiceUnavailableHttpError -} from "./HttpError"; +import {BadRequestError, HttpError, NotFoundHttpError, ServerError, ServiceUnavailableHttpError} from "./HttpError"; import {lib} from "nunjucks"; import Logger from "./Logger"; import WebSocketListener from "./WebSocketListener"; import ApplicationComponent from "./ApplicationComponent"; -import TemplateError = lib.TemplateError; import Controller from "./Controller"; +import MysqlConnectionManager from "./db/MysqlConnectionManager"; +import Migration from "./db/Migration"; +import TemplateError = lib.TemplateError; +import {Type} from "./Utils"; export default abstract class Application { private readonly version: string; @@ -25,6 +22,8 @@ export default abstract class Application { this.version = version; } + protected abstract getMigrations(): Type[]; + protected abstract async init(): Promise; protected use(thing: Controller | WebSocketListener | ApplicationComponent) { @@ -45,6 +44,9 @@ export default abstract class Application { this.stop().catch(console.error); }); + // Register migrations + MysqlConnectionManager.registerMigrations(this.getMigrations()); + // Register all components and alike await this.init(); diff --git a/src/Utils.ts b/src/Utils.ts index c63593b..99bbfdd 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -32,4 +32,8 @@ export function cryptoRandomDictionary(size: number, dictionary: string): string } return output.join(''); +} + +export interface Type extends Function { + new(...args: any[]): T } \ No newline at end of file diff --git a/src/db/MysqlConnectionManager.ts b/src/db/MysqlConnectionManager.ts index 3bccd7e..c991309 100644 --- a/src/db/MysqlConnectionManager.ts +++ b/src/db/MysqlConnectionManager.ts @@ -2,6 +2,7 @@ import mysql, {Connection, FieldInfo, Pool} from 'mysql'; import config from 'config'; import Migration from "./Migration"; import Logger from "../Logger"; +import {Type} from "../Utils"; export interface QueryResult { readonly results: any[]; @@ -17,9 +18,17 @@ export async function query(queryString: string, values?: any, connection?: Conn export default class MysqlConnectionManager { private static currentPool?: Pool; private static databaseReady: boolean = false; + private static migrationsRegistered: boolean = false; private static readonly migrations: Migration[] = []; - public static registerMigration(migration: (version: number) => Migration) { + public static registerMigrations(migrations: Type[]) { + if (!this.migrationsRegistered) { + this.migrationsRegistered = true; + migrations.forEach(m => this.registerMigration(v => new m(v))); + } + } + + private static registerMigration(migration: (version: number) => Migration) { this.migrations.push(migration(this.migrations.length + 1)); } diff --git a/test/Model.test.ts b/test/Model.test.ts index 548804d..2f488b5 100644 --- a/test/Model.test.ts +++ b/test/Model.test.ts @@ -1,7 +1,7 @@ -import registerMigrations from "./_registerMigrations"; -import Model from "../src/db/Model"; import MysqlConnectionManager from "../src/db/MysqlConnectionManager"; +import Model from "../src/db/Model"; import Validator from "../src/db/Validator"; +import {MIGRATIONS} from "./_migrations"; class FakeDummyModel extends Model { public name?: string; @@ -16,7 +16,7 @@ class FakeDummyModel extends Model { } beforeAll(async (done) => { - registerMigrations(); + MysqlConnectionManager.registerMigrations(MIGRATIONS); await MysqlConnectionManager.prepare(); done(); }); diff --git a/test/_migrations.ts b/test/_migrations.ts new file mode 100644 index 0000000..020829c --- /dev/null +++ b/test/_migrations.ts @@ -0,0 +1,7 @@ +import CreateMigrationsTable from "../src/migrations/CreateMigrationsTable"; +import CreateLogsTable from "../src/migrations/CreateLogsTable"; + +export const MIGRATIONS = [ + CreateMigrationsTable, + CreateLogsTable, +]; \ No newline at end of file diff --git a/test/_registerMigrations.ts b/test/_registerMigrations.ts deleted file mode 100644 index f586137..0000000 --- a/test/_registerMigrations.ts +++ /dev/null @@ -1,15 +0,0 @@ -import CreateMigrationsTable from "../src/migrations/CreateMigrationsTable"; -import CreateLogsTable from "../src/migrations/CreateLogsTable"; -import MysqlConnectionManager from "../src/db/MysqlConnectionManager"; - -let migrationsRegistered = false; - -export default function registerMigrations() { - if (!migrationsRegistered) { - migrationsRegistered = true; - [ - CreateMigrationsTable, - CreateLogsTable, - ].forEach(m => MysqlConnectionManager.registerMigration(v => new m(v))); - } -} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 77e9ad0..4158deb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -539,9 +539,9 @@ "@types/node" "*" "@types/range-parser" "*" -"@types/express-session@*": +"@types/express-session@*", "@types/express-session@^1.17.0": version "1.17.0" - resolved "https://registry.yarnpkg.com/@types/express-session/-/express-session-1.17.0.tgz#770daf81368f6278e3e40dd894e1e52abbdca0cd" + resolved "http://127.0.0.1:4873/@types%2fexpress-session/-/express-session-1.17.0.tgz#770daf81368f6278e3e40dd894e1e52abbdca0cd" integrity sha512-OQEHeBFE1UhChVIBhRh9qElHUvTp4BzKKHxMDkGHT7WuYk5eL93hPG7D8YAIkoBSbhNEY0RjreF15zn+U0eLjA== dependencies: "@types/express" "*"