swaf/src/migrations/CreateMigrationsTable.ts

34 lines
1.0 KiB
TypeScript
Raw Normal View History

import Migration from "../db/Migration.js";
import {query} from "../db/MysqlConnectionManager.js";
2020-04-22 15:52:17 +02:00
/**
* Must be the first migration
*/
export default class CreateMigrationsTable extends Migration {
2020-07-24 12:13:28 +02:00
public async shouldRun(currentVersion: number): Promise<boolean> {
2020-04-22 15:52:17 +02:00
try {
await query('SELECT 1 FROM migrations LIMIT 1');
2021-11-08 00:52:33 +01:00
} catch (e: any) {
2020-04-22 15:52:17 +02:00
if (e.code !== 'ER_NO_SUCH_TABLE') {
return false;
}
}
return await super.shouldRun(currentVersion);
}
public async install(): Promise<void> {
2020-07-24 12:13:28 +02:00
await this.query(`CREATE TABLE migrations
(
id INT NOT NULL,
name VARCHAR(64) NOT NULL,
migration_date DATE,
PRIMARY KEY (id)
)`);
2020-04-22 15:52:17 +02:00
}
public async rollback(): Promise<void> {
await this.query('DROP TABLE migrations');
2020-04-22 15:52:17 +02:00
}
}