From 03d9826f9310c12c3ef49d9e2819995024dcd1e8 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 8 Nov 2020 15:48:34 +0100 Subject: [PATCH] Migration: remove `connection` parameter from query() method Closes #5 --- .../migrations/AddApprovedFieldToUsersTable.ts | 9 ++++----- src/auth/migrations/CreateMagicLinksTable.ts | 9 ++++----- .../CreateUsersAndUserEmailsTable.ts | 13 ++++++------- src/auth/migrations/DropNameFromUsers.ts | 9 ++++----- .../migrations/FixUserMainEmailRelation.ts | 17 ++++++++--------- src/db/Migration.ts | 18 ++++++++++++++---- src/db/MysqlConnectionManager.ts | 8 ++++++-- src/migrations/CreateMigrationsTable.ts | 9 ++++----- src/migrations/DropLegacyLogsTable.ts | 5 ++--- 9 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/auth/migrations/AddApprovedFieldToUsersTable.ts b/src/auth/migrations/AddApprovedFieldToUsersTable.ts index fbe7970..b1db7de 100644 --- a/src/auth/migrations/AddApprovedFieldToUsersTable.ts +++ b/src/auth/migrations/AddApprovedFieldToUsersTable.ts @@ -1,16 +1,15 @@ import Migration from "../../db/Migration"; -import {Connection} from "mysql"; import ModelFactory from "../../db/ModelFactory"; import User from "../models/User"; import UserApprovedComponent from "../models/UserApprovedComponent"; export default class AddApprovedFieldToUsersTable extends Migration { - public async install(connection: Connection): Promise { - await this.query('ALTER TABLE users ADD COLUMN approved BOOLEAN NOT NULL DEFAULT 0', connection); + public async install(): Promise { + await this.query('ALTER TABLE users ADD COLUMN approved BOOLEAN NOT NULL DEFAULT 0'); } - public async rollback(connection: Connection): Promise { - await this.query('ALTER TABLE users DROP COLUMN approved', connection); + public async rollback(): Promise { + await this.query('ALTER TABLE users DROP COLUMN approved'); } public registerModels(): void { diff --git a/src/auth/migrations/CreateMagicLinksTable.ts b/src/auth/migrations/CreateMagicLinksTable.ts index 3f223de..14b0c05 100644 --- a/src/auth/migrations/CreateMagicLinksTable.ts +++ b/src/auth/migrations/CreateMagicLinksTable.ts @@ -1,10 +1,9 @@ import Migration from "../../db/Migration"; -import {Connection} from "mysql"; import ModelFactory from "../../db/ModelFactory"; import MagicLink from "../models/MagicLink"; export default class CreateMagicLinksTable extends Migration { - public async install(connection: Connection): Promise { + public async install(): Promise { await this.query(`CREATE TABLE magic_links ( id INT NOT NULL AUTO_INCREMENT, @@ -16,11 +15,11 @@ export default class CreateMagicLinksTable extends Migration { generated_at DATETIME NOT NULL, authorized BOOLEAN NOT NULL, PRIMARY KEY (id) - )`, connection); + )`); } - public async rollback(connection: Connection): Promise { - await this.query('DROP TABLE magic_links', connection); + public async rollback(): Promise { + await this.query('DROP TABLE magic_links'); } public registerModels(): void { diff --git a/src/auth/migrations/CreateUsersAndUserEmailsTable.ts b/src/auth/migrations/CreateUsersAndUserEmailsTable.ts index 0c9ecf7..41ea2d0 100644 --- a/src/auth/migrations/CreateUsersAndUserEmailsTable.ts +++ b/src/auth/migrations/CreateUsersAndUserEmailsTable.ts @@ -1,11 +1,10 @@ import Migration from "../../db/Migration"; -import {Connection} from "mysql"; import ModelFactory from "../../db/ModelFactory"; import User from "../models/User"; import UserEmail from "../models/UserEmail"; export default class CreateUsersAndUserEmailsTable extends Migration { - public async install(connection: Connection): Promise { + public async install(): Promise { await this.query(`CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, @@ -14,7 +13,7 @@ export default class CreateUsersAndUserEmailsTable extends Migration { created_at DATETIME NOT NULL DEFAULT NOW(), updated_at DATETIME NOT NULL DEFAULT NOW(), PRIMARY KEY (id) - )`, connection); + )`); await this.query(`CREATE TABLE user_emails ( id INT NOT NULL AUTO_INCREMENT, @@ -24,12 +23,12 @@ export default class CreateUsersAndUserEmailsTable extends Migration { created_at DATETIME NOT NULL DEFAULT NOW(), PRIMARY KEY (id), FOREIGN KEY user_fk (user_id) REFERENCES users (id) ON DELETE CASCADE - )`, connection); + )`); } - public async rollback(connection: Connection): Promise { - await this.query('DROP TABLE user_emails', connection); - await this.query('DROP TABLE users', connection); + public async rollback(): Promise { + await this.query('DROP TABLE user_emails'); + await this.query('DROP TABLE users'); } public registerModels(): void { diff --git a/src/auth/migrations/DropNameFromUsers.ts b/src/auth/migrations/DropNameFromUsers.ts index 1ff45db..28796cb 100644 --- a/src/auth/migrations/DropNameFromUsers.ts +++ b/src/auth/migrations/DropNameFromUsers.ts @@ -1,13 +1,12 @@ import Migration from "../../db/Migration"; -import {Connection} from "mysql"; export default class DropNameFromUsers extends Migration { - public async install(connection: Connection): Promise { - await this.query('ALTER TABLE users DROP COLUMN name', connection); + public async install(): Promise { + await this.query('ALTER TABLE users DROP COLUMN name'); } - public async rollback(connection: Connection): Promise { - await this.query('ALTER TABLE users ADD COLUMN name VARCHAR(64)', connection); + public async rollback(): Promise { + await this.query('ALTER TABLE users ADD COLUMN name VARCHAR(64)'); } } diff --git a/src/auth/migrations/FixUserMainEmailRelation.ts b/src/auth/migrations/FixUserMainEmailRelation.ts index ff6c8d0..ccde3df 100644 --- a/src/auth/migrations/FixUserMainEmailRelation.ts +++ b/src/auth/migrations/FixUserMainEmailRelation.ts @@ -1,25 +1,24 @@ import Migration from "../../db/Migration"; -import {Connection} from "mysql"; export default class FixUserMainEmailRelation extends Migration { - public async install(connection: Connection): Promise { + public async install(): Promise { await this.query(`ALTER TABLE users ADD COLUMN main_email_id INT, - ADD FOREIGN KEY main_user_email_fk (main_email_id) REFERENCES user_emails (id)`, connection); + ADD FOREIGN KEY main_user_email_fk (main_email_id) REFERENCES user_emails (id)`); await this.query(`UPDATE users u LEFT JOIN user_emails ue ON u.id = ue.user_id SET u.main_email_id=ue.id - WHERE ue.main = true`, connection); + WHERE ue.main = true`); await this.query(`ALTER TABLE user_emails - DROP COLUMN main`, connection); + DROP COLUMN main`); } - public async rollback(connection: Connection): Promise { + public async rollback(): Promise { await this.query(`ALTER TABLE user_emails - ADD COLUMN main BOOLEAN DEFAULT false`, connection); + ADD COLUMN main BOOLEAN DEFAULT false`); await this.query(`UPDATE user_emails ue LEFT JOIN users u ON ue.id = u.main_email_id - SET ue.main = true`, connection); + SET ue.main = true`); await this.query(`ALTER TABLE users DROP FOREIGN KEY main_user_email_fk, - DROP COLUMN main_email_id`, connection); + DROP COLUMN main_email_id`); } } diff --git a/src/db/Migration.ts b/src/db/Migration.ts index 2bd8212..2e142ec 100644 --- a/src/db/Migration.ts +++ b/src/db/Migration.ts @@ -4,6 +4,7 @@ import {Type} from "../Utils"; export default abstract class Migration { public readonly version: number; + private currentConnection?: Connection; public constructor(version: number) { this.version = version; @@ -13,14 +14,23 @@ export default abstract class Migration { return this.version > currentVersion; } - public abstract async install(connection: Connection): Promise; + public abstract async install(): Promise; - public abstract async rollback(connection: Connection): Promise; + public abstract async rollback(): Promise; public registerModels?(): void; - protected async query(queryString: string, connection: Connection): Promise { - await MysqlConnectionManager.query(queryString, undefined, connection); + protected async query(queryString: string): Promise { + 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; } } diff --git a/src/db/MysqlConnectionManager.ts b/src/db/MysqlConnectionManager.ts index 8cd1049..a927628 100644 --- a/src/db/MysqlConnectionManager.ts +++ b/src/db/MysqlConnectionManager.ts @@ -197,7 +197,9 @@ export default class MysqlConnectionManager { if (await migration.shouldRun(currentVersion)) { log.info('Running migration ', migration.version, migration.constructor.name); await MysqlConnectionManager.wrapTransaction(async c => { - await migration.install(c); + migration.setCurrentConnection(c); + await migration.install(); + migration.setCurrentConnection(null); await query('INSERT INTO migrations VALUES(?, ?, NOW())', [ migration.version, migration.constructor.name, @@ -219,7 +221,9 @@ export default class MysqlConnectionManager { const migration = this.migrations[migrationId]; log.info('Rolling back migration ', migration.version, migration.constructor.name); await MysqlConnectionManager.wrapTransaction(async c => { - await migration.rollback(c); + migration.setCurrentConnection(c); + await migration.rollback(); + migration.setCurrentConnection(null); await query('DELETE FROM migrations WHERE id=?', [migration.version]); }); } diff --git a/src/migrations/CreateMigrationsTable.ts b/src/migrations/CreateMigrationsTable.ts index 915321b..0f1c6e8 100644 --- a/src/migrations/CreateMigrationsTable.ts +++ b/src/migrations/CreateMigrationsTable.ts @@ -1,5 +1,4 @@ import Migration from "../db/Migration"; -import {Connection} from "mysql"; import {query} from "../db/MysqlConnectionManager"; /** @@ -18,17 +17,17 @@ export default class CreateMigrationsTable extends Migration { return await super.shouldRun(currentVersion); } - public async install(connection: Connection): Promise { + public async install(): Promise { await this.query(`CREATE TABLE migrations ( id INT NOT NULL, name VARCHAR(64) NOT NULL, migration_date DATE, PRIMARY KEY (id) - )`, connection); + )`); } - public async rollback(connection: Connection): Promise { - await this.query('DROP TABLE migrations', connection); + public async rollback(): Promise { + await this.query('DROP TABLE migrations'); } } diff --git a/src/migrations/DropLegacyLogsTable.ts b/src/migrations/DropLegacyLogsTable.ts index f19493f..c6c0cc1 100644 --- a/src/migrations/DropLegacyLogsTable.ts +++ b/src/migrations/DropLegacyLogsTable.ts @@ -1,9 +1,8 @@ import Migration from "../db/Migration"; -import {Connection} from "mysql"; export default class DropLegacyLogsTable extends Migration { - public async install(connection: Connection): Promise { - await this.query('DROP TABLE IF EXISTS logs', connection); + public async install(): Promise { + await this.query('DROP TABLE IF EXISTS logs'); } public async rollback(): Promise {