swaf/src/migrations/CreateMigrationsTable.ts

33 lines
982 B
TypeScript
Raw Normal View History

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