Force an application to register migrations
This commit is contained in:
parent
5ba983d0d3
commit
3bf4f93b74
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "wms-core",
|
"name": "wms-core",
|
||||||
"version": "0.2.0",
|
"version": "0.2.5",
|
||||||
"description": "Node web framework",
|
"description": "Node web framework",
|
||||||
"repository": "git@gitlab.com:ArisuOngaku/wms-core.git",
|
"repository": "git@gitlab.com:ArisuOngaku/wms-core.git",
|
||||||
"author": "Alice Gaudon <alice@gaudon.pro>",
|
"author": "Alice Gaudon <alice@gaudon.pro>",
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
import express, {NextFunction, Request, Response, Router} from 'express';
|
import express, {NextFunction, Request, Response, Router} from 'express';
|
||||||
import {
|
import {BadRequestError, HttpError, NotFoundHttpError, ServerError, ServiceUnavailableHttpError} from "./HttpError";
|
||||||
BadRequestError,
|
|
||||||
HttpError,
|
|
||||||
NotFoundHttpError,
|
|
||||||
ServerError,
|
|
||||||
ServiceUnavailableHttpError
|
|
||||||
} from "./HttpError";
|
|
||||||
import {lib} from "nunjucks";
|
import {lib} from "nunjucks";
|
||||||
import Logger from "./Logger";
|
import Logger from "./Logger";
|
||||||
import WebSocketListener from "./WebSocketListener";
|
import WebSocketListener from "./WebSocketListener";
|
||||||
import ApplicationComponent from "./ApplicationComponent";
|
import ApplicationComponent from "./ApplicationComponent";
|
||||||
import TemplateError = lib.TemplateError;
|
|
||||||
import Controller from "./Controller";
|
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 {
|
export default abstract class Application {
|
||||||
private readonly version: string;
|
private readonly version: string;
|
||||||
@ -25,6 +22,8 @@ export default abstract class Application {
|
|||||||
this.version = version;
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract getMigrations(): Type<Migration>[];
|
||||||
|
|
||||||
protected abstract async init(): Promise<void>;
|
protected abstract async init(): Promise<void>;
|
||||||
|
|
||||||
protected use(thing: Controller | WebSocketListener | ApplicationComponent<any>) {
|
protected use(thing: Controller | WebSocketListener | ApplicationComponent<any>) {
|
||||||
@ -45,6 +44,9 @@ export default abstract class Application {
|
|||||||
this.stop().catch(console.error);
|
this.stop().catch(console.error);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Register migrations
|
||||||
|
MysqlConnectionManager.registerMigrations(this.getMigrations());
|
||||||
|
|
||||||
// Register all components and alike
|
// Register all components and alike
|
||||||
await this.init();
|
await this.init();
|
||||||
|
|
||||||
|
@ -32,4 +32,8 @@ export function cryptoRandomDictionary(size: number, dictionary: string): string
|
|||||||
}
|
}
|
||||||
|
|
||||||
return output.join('');
|
return output.join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Type<T> extends Function {
|
||||||
|
new(...args: any[]): T
|
||||||
}
|
}
|
@ -2,6 +2,7 @@ import mysql, {Connection, FieldInfo, Pool} from 'mysql';
|
|||||||
import config from 'config';
|
import config from 'config';
|
||||||
import Migration from "./Migration";
|
import Migration from "./Migration";
|
||||||
import Logger from "../Logger";
|
import Logger from "../Logger";
|
||||||
|
import {Type} from "../Utils";
|
||||||
|
|
||||||
export interface QueryResult {
|
export interface QueryResult {
|
||||||
readonly results: any[];
|
readonly results: any[];
|
||||||
@ -17,9 +18,17 @@ export async function query(queryString: string, values?: any, connection?: Conn
|
|||||||
export default class MysqlConnectionManager {
|
export default class MysqlConnectionManager {
|
||||||
private static currentPool?: Pool;
|
private static currentPool?: Pool;
|
||||||
private static databaseReady: boolean = false;
|
private static databaseReady: boolean = false;
|
||||||
|
private static migrationsRegistered: boolean = false;
|
||||||
private static readonly migrations: Migration[] = [];
|
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));
|
this.migrations.push(migration(this.migrations.length + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import registerMigrations from "./_registerMigrations";
|
|
||||||
import Model from "../src/db/Model";
|
|
||||||
import MysqlConnectionManager from "../src/db/MysqlConnectionManager";
|
import MysqlConnectionManager from "../src/db/MysqlConnectionManager";
|
||||||
|
import Model from "../src/db/Model";
|
||||||
import Validator from "../src/db/Validator";
|
import Validator from "../src/db/Validator";
|
||||||
|
import {MIGRATIONS} from "./_migrations";
|
||||||
|
|
||||||
class FakeDummyModel extends Model {
|
class FakeDummyModel extends Model {
|
||||||
public name?: string;
|
public name?: string;
|
||||||
@ -16,7 +16,7 @@ class FakeDummyModel extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
beforeAll(async (done) => {
|
beforeAll(async (done) => {
|
||||||
registerMigrations();
|
MysqlConnectionManager.registerMigrations(MIGRATIONS);
|
||||||
await MysqlConnectionManager.prepare();
|
await MysqlConnectionManager.prepare();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
7
test/_migrations.ts
Normal file
7
test/_migrations.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import CreateMigrationsTable from "../src/migrations/CreateMigrationsTable";
|
||||||
|
import CreateLogsTable from "../src/migrations/CreateLogsTable";
|
||||||
|
|
||||||
|
export const MIGRATIONS = [
|
||||||
|
CreateMigrationsTable,
|
||||||
|
CreateLogsTable,
|
||||||
|
];
|
@ -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)));
|
|
||||||
}
|
|
||||||
}
|
|
@ -539,9 +539,9 @@
|
|||||||
"@types/node" "*"
|
"@types/node" "*"
|
||||||
"@types/range-parser" "*"
|
"@types/range-parser" "*"
|
||||||
|
|
||||||
"@types/express-session@*":
|
"@types/express-session@*", "@types/express-session@^1.17.0":
|
||||||
version "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==
|
integrity sha512-OQEHeBFE1UhChVIBhRh9qElHUvTp4BzKKHxMDkGHT7WuYk5eL93hPG7D8YAIkoBSbhNEY0RjreF15zn+U0eLjA==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@types/express" "*"
|
"@types/express" "*"
|
||||||
|
Loading…
Reference in New Issue
Block a user