swaf/src/migrations/CreateMigrationsTable.ts

36 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-11-10 17:48:54 +01:00
import {MysqlError} from "mysql";
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-10 17:48:54 +01:00
} catch (e) {
if (!(e instanceof Error) || (e as MysqlError).code !== 'ER_NO_SUCH_TABLE') {
2020-04-22 15:52:17 +02:00
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
}
}