diff --git a/package.json b/package.json index 50e39d4..e55d1e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wms-core", - "version": "0.5.0", + "version": "0.6.0", "description": "Node web framework", "repository": "git@gitlab.com:ArisuOngaku/wms-core.git", "author": "Alice Gaudon ", diff --git a/src/auth/migrations/CreateMagicLinksTable.ts b/src/auth/migrations/CreateMagicLinksTable.ts index 059fe0d..23aa17f 100644 --- a/src/auth/migrations/CreateMagicLinksTable.ts +++ b/src/auth/migrations/CreateMagicLinksTable.ts @@ -1,9 +1,9 @@ import Migration from "../../db/Migration"; -import {query} from "../../db/MysqlConnectionManager"; +import {Connection} from "mysql"; export default class CreateMagicLinksTable extends Migration { - async install(): Promise { - await query('CREATE TABLE magic_links(' + + async install(connection: Connection): Promise { + await this.query('CREATE TABLE magic_links(' + 'id INT NOT NULL AUTO_INCREMENT,' + 'session_id CHAR(32) UNIQUE NOT NULL,' + 'email VARCHAR(254) NOT NULL,' + @@ -13,11 +13,11 @@ export default class CreateMagicLinksTable extends Migration { 'generated_at DATETIME NOT NULL,' + 'authorized BOOLEAN NOT NULL,' + 'PRIMARY KEY(id)' + - ')'); + ')', connection); } - async rollback(): Promise { - await query('DROP TABLE magic_links'); + async rollback(connection: Connection): Promise { + await this.query('DROP TABLE magic_links', connection); } } \ No newline at end of file diff --git a/src/auth/migrations/CreateUsersAndUserEmailsTable.ts b/src/auth/migrations/CreateUsersAndUserEmailsTable.ts index 0266ad1..1ef4d5e 100644 --- a/src/auth/migrations/CreateUsersAndUserEmailsTable.ts +++ b/src/auth/migrations/CreateUsersAndUserEmailsTable.ts @@ -1,17 +1,17 @@ import Migration from "../../db/Migration"; -import {query} from "../../db/MysqlConnectionManager"; +import {Connection} from "mysql"; export default class CreateUsersAndUserEmailsTable extends Migration { - async install(): Promise { - await query('CREATE TABLE users(' + + async install(connection: Connection): Promise { + await this.query('CREATE TABLE users(' + 'id INT NOT NULL AUTO_INCREMENT,' + 'name VARCHAR(64),' + 'is_admin BOOLEAN NOT NULL DEFAULT false,' + 'created_at DATETIME NOT NULL DEFAULT NOW(),' + 'updated_at DATETIME NOT NULL DEFAULT NOW(),' + 'PRIMARY KEY(id)' + - ')'); - await query('CREATE TABLE user_emails(' + + ')', connection); + await this.query('CREATE TABLE user_emails(' + 'id INT NOT NULL AUTO_INCREMENT,' + 'user_id INT,' + 'email VARCHAR(254) UNIQUE NOT NULL,' + @@ -19,11 +19,11 @@ 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); } - async rollback(): Promise { - await query('DROP TABLE user_emails'); - await query('DROP TABLE users'); + async rollback(connection: Connection): Promise { + await this.query('DROP TABLE user_emails', connection); + await this.query('DROP TABLE users', connection); } } \ No newline at end of file diff --git a/src/db/Migration.ts b/src/db/Migration.ts index 34b3c4a..a62ddb3 100644 --- a/src/db/Migration.ts +++ b/src/db/Migration.ts @@ -1,3 +1,6 @@ +import {Connection} from "mysql"; +import MysqlConnectionManager from "./MysqlConnectionManager"; + export default abstract class Migration { public readonly version: number; @@ -9,7 +12,11 @@ export default abstract class Migration { return this.version > currentVersion; } - abstract async install(): Promise; + abstract async install(connection: Connection): Promise; - abstract async rollback(): Promise; + abstract async rollback(connection: Connection): Promise; + + protected async query(queryString: string, connection: Connection): Promise { + await MysqlConnectionManager.query(queryString, undefined, connection); + } } \ No newline at end of file diff --git a/src/db/MysqlConnectionManager.ts b/src/db/MysqlConnectionManager.ts index c991309..48037cd 100644 --- a/src/db/MysqlConnectionManager.ts +++ b/src/db/MysqlConnectionManager.ts @@ -167,11 +167,13 @@ export default class MysqlConnectionManager { for (const migration of this.migrations) { if (await migration.shouldRun(currentVersion)) { Logger.info('Running migration ', migration.version, migration.constructor.name); - await migration.install(); - await query('INSERT INTO migrations VALUES(?, ?, NOW())', [ - migration.version, - migration.constructor.name, - ]); + await MysqlConnectionManager.wrapTransaction(async c => { + await migration.install(c); + await query('INSERT INTO migrations VALUES(?, ?, NOW())', [ + migration.version, + migration.constructor.name, + ]); + }); } } } diff --git a/src/migrations/CreateLogsTable.ts b/src/migrations/CreateLogsTable.ts index 37511b7..d18bbbb 100644 --- a/src/migrations/CreateLogsTable.ts +++ b/src/migrations/CreateLogsTable.ts @@ -1,12 +1,12 @@ import Migration from "../db/Migration"; -import {query} from "../db/MysqlConnectionManager"; +import {Connection} from "mysql"; /** * Must be the first migration */ export default class CreateLogsTable extends Migration { - async install(): Promise { - await query('CREATE TABLE logs(' + + async install(connection: Connection): Promise { + await this.query('CREATE TABLE logs(' + 'id INT NOT NULL AUTO_INCREMENT,' + 'level TINYINT UNSIGNED NOT NULL,' + 'message TEXT NOT NULL,' + @@ -16,10 +16,10 @@ export default class CreateLogsTable extends Migration { 'error_stack TEXT,' + 'created_at DATETIME NOT NULL DEFAULT NOW(),' + 'PRIMARY KEY (id)' + - ')'); + ')', connection); } - async rollback(): Promise { - await query('DROP TABLE logs'); + async rollback(connection: Connection): Promise { + await this.query('DROP TABLE logs', connection); } } \ No newline at end of file diff --git a/src/migrations/CreateMigrationsTable.ts b/src/migrations/CreateMigrationsTable.ts index 5931d24..c55fbb2 100644 --- a/src/migrations/CreateMigrationsTable.ts +++ b/src/migrations/CreateMigrationsTable.ts @@ -1,4 +1,5 @@ import Migration from "../db/Migration"; +import {Connection} from "mysql"; import {query} from "../db/MysqlConnectionManager"; /** @@ -17,16 +18,16 @@ export default class CreateMigrationsTable extends Migration { return await super.shouldRun(currentVersion); } - async install(): Promise { - await query('CREATE TABLE migrations(' + + async install(connection: Connection): Promise { + await this.query('CREATE TABLE migrations(' + 'id INT NOT NULL,' + 'name VARCHAR(64) NOT NULL,' + 'migration_date DATE,' + 'PRIMARY KEY (id)' + - ')'); + ')', connection); } - async rollback(): Promise { - await query('DROP TABLE migrations'); + async rollback(connection: Connection): Promise { + await this.query('DROP TABLE migrations', connection); } } \ No newline at end of file