swaf/src/migrations/CreateMigrationsTable.ts

35 lines
1.1 KiB
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 {
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');
} catch (e) {
if (e.code !== 'ER_NO_SUCH_TABLE') {
return false;
}
}
return await super.shouldRun(currentVersion);
}
2020-07-24 12:13:28 +02:00
public async install(connection: Connection): Promise<void> {
await this.query(`CREATE TABLE migrations
(
id INT NOT NULL,
name VARCHAR(64) NOT NULL,
migration_date DATE,
PRIMARY KEY (id)
)`, connection);
2020-04-22 15:52:17 +02:00
}
2020-07-24 12:13:28 +02:00
public async rollback(connection: Connection): Promise<void> {
2020-06-04 17:27:05 +02:00
await this.query('DROP TABLE migrations', connection);
2020-04-22 15:52:17 +02:00
}
}