2020-06-04 17:27:05 +02:00
|
|
|
import {Connection} from "mysql";
|
2021-05-03 19:29:22 +02:00
|
|
|
|
|
|
|
import {Type} from "../Utils.js";
|
|
|
|
import MysqlConnectionManager from "./MysqlConnectionManager.js";
|
2020-06-04 17:27:05 +02:00
|
|
|
|
2020-04-22 15:52:17 +02:00
|
|
|
export default abstract class Migration {
|
|
|
|
public readonly version: number;
|
2020-11-08 15:48:34 +01:00
|
|
|
private currentConnection?: Connection;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-07-24 12:13:28 +02:00
|
|
|
public constructor(version: number) {
|
2020-04-22 15:52:17 +02:00
|
|
|
this.version = version;
|
|
|
|
}
|
|
|
|
|
2020-07-24 12:13:28 +02:00
|
|
|
public async shouldRun(currentVersion: number): Promise<boolean> {
|
2020-04-22 15:52:17 +02:00
|
|
|
return this.version > currentVersion;
|
|
|
|
}
|
|
|
|
|
2020-12-04 14:42:09 +01:00
|
|
|
public abstract install(): Promise<void>;
|
2020-06-04 17:27:05 +02:00
|
|
|
|
2020-12-04 14:42:09 +01:00
|
|
|
public abstract rollback(): Promise<void>;
|
2020-07-24 12:13:28 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public registerModels?(): void;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-11-08 15:48:34 +01:00
|
|
|
protected async query(queryString: string): Promise<void> {
|
|
|
|
await MysqlConnectionManager.query(queryString, undefined, this.getCurrentConnection());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getCurrentConnection(): Connection {
|
|
|
|
if (!this.currentConnection) throw new Error('No current connection set.');
|
|
|
|
return this.currentConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setCurrentConnection(connection: Connection | null): void {
|
|
|
|
this.currentConnection = connection || undefined;
|
2020-06-04 17:27:05 +02:00
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface MigrationType<M extends Migration> extends Type<M> {
|
|
|
|
new(version: number): M;
|
|
|
|
}
|