Force an application to register migrations

This commit is contained in:
Alice Gaudon 2020-04-23 15:43:15 +02:00
parent 5ba983d0d3
commit 3bf4f93b74
8 changed files with 37 additions and 30 deletions

View File

@ -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 <alice@gaudon.pro>",

View File

@ -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<Migration>[];
protected abstract async init(): Promise<void>;
protected use(thing: Controller | WebSocketListener | ApplicationComponent<any>) {
@ -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();

View File

@ -32,4 +32,8 @@ export function cryptoRandomDictionary(size: number, dictionary: string): string
}
return output.join('');
}
export interface Type<T> extends Function {
new(...args: any[]): T
}

View File

@ -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<Migration>[]) {
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));
}

View File

@ -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();
});

7
test/_migrations.ts Normal file
View File

@ -0,0 +1,7 @@
import CreateMigrationsTable from "../src/migrations/CreateMigrationsTable";
import CreateLogsTable from "../src/migrations/CreateLogsTable";
export const MIGRATIONS = [
CreateMigrationsTable,
CreateLogsTable,
];

View File

@ -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)));
}
}

View File

@ -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" "*"