From c0bdcf166c5faba40ee42a69c4febfebcf3fc75a Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Tue, 30 Nov 2021 22:31:53 +0100 Subject: [PATCH 1/3] Insert lock row fix during migration --- lib/migrations/migrate/migration-list-resolver.js | 4 +--- lib/migrations/migrate/table-creator.js | 11 ++++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/migrations/migrate/migration-list-resolver.js b/lib/migrations/migrate/migration-list-resolver.js index d575fd1ffc..873ad20b62 100644 --- a/lib/migrations/migrate/migration-list-resolver.js +++ b/lib/migrations/migrate/migration-list-resolver.js @@ -11,12 +11,10 @@ function listAll(migrationSource, loadExtensions) { async function listCompleted(tableName, schemaName, trxOrKnex) { await ensureTable(tableName, schemaName, trxOrKnex); - const completedMigrations = await trxOrKnex + return await trxOrKnex .from(getTableName(tableName, schemaName)) .orderBy('id') .select('name'); - - return completedMigrations; } // Gets the migration list from the migration directory specified in config, as well as diff --git a/lib/migrations/migrate/table-creator.js b/lib/migrations/migrate/table-creator.js index db61aebb7b..4a6212828a 100644 --- a/lib/migrations/migrate/table-creator.js +++ b/lib/migrations/migrate/table-creator.js @@ -55,11 +55,12 @@ function _createMigrationLockTable(tableName, schemaName, trxOrKnex) { function _insertLockRowIfNeeded(tableName, schemaName, trxOrKnex) { const lockTableWithSchema = getLockTableNameWithSchema(tableName, schemaName); return trxOrKnex - .from(trxOrKnex.raw('?? (??)', [lockTableWithSchema, 'is_locked'])) - .insert(function () { - return this.select(trxOrKnex.raw('?', [0])).whereNotExists(function () { - return this.select('*').from(lockTableWithSchema); - }); + .select('*') + .from(lockTableWithSchema) + .then((data) => { + return !data.length + ? trxOrKnex.from(lockTableWithSchema).insert({ is_locked: 0 }) + : null; }); } From f846ced50770f4d050eae7bf30fe6165be7da0fb Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Wed, 1 Dec 2021 00:26:30 +0100 Subject: [PATCH 2/3] add tests --- lib/migrations/migrate/Migrator.js | 2 +- .../migrate/migration-integration.spec.js | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/migrations/migrate/Migrator.js b/lib/migrations/migrate/Migrator.js index 353ea18f50..9d93050ab8 100644 --- a/lib/migrations/migrate/Migrator.js +++ b/lib/migrations/migrate/Migrator.js @@ -328,7 +328,7 @@ class Migrator { .where('is_locked', '=', 0) .update({ is_locked: 1 }) .then((rowCount) => { - if (rowCount != 1) { + if (rowCount !== 1) { throw new Error('Migration table is already locked'); } }); diff --git a/test/integration2/migrate/migration-integration.spec.js b/test/integration2/migrate/migration-integration.spec.js index ac4105722f..d17bbbb67d 100644 --- a/test/integration2/migrate/migration-integration.spec.js +++ b/test/integration2/migrate/migration-integration.spec.js @@ -25,6 +25,9 @@ const { } = require('../../util/db-helpers'); const { assertNumber } = require('../../util/assertHelper'); const { getAllDbs, getKnexForDb } = require('../util/knex-instance-provider'); +const { + ensureTable, +} = require('../../../lib/migrations/migrate/table-creator'); describe('Migrations', function () { getAllDbs().forEach((db) => { @@ -1320,6 +1323,35 @@ describe('Migrations', function () { } }); }); + + describe('Test lock row', async () => { + beforeEach(async () => { + await knex.schema.dropTableIfExists('test_lock'); + }); + + it('should insert is_locked value to 1 if lock table not exists', async () => { + const result = await ensureTable('test', undefined, knex); + + expect(result.rowCount).is.equal(1); + const data = await knex('test_lock').select('*'); + expect(data[0]).to.have.property('is_locked'); + expect(Number.parseInt(data[0].is_locked)).to.not.be.ok; + }); + + it('should is_locked value still be 1 if row already exists', async () => { + await knex.schema.createTable('test_lock', (t) => { + t.increments('index').primary(); + t.integer('is_locked'); + }); + await knex('test_lock').insert({ is_locked: 1 }); + + const result = await ensureTable('test', undefined, knex); + expect(result).to.false; + const data = await knex('test_lock').select('*'); + expect(data[0]).to.have.property('is_locked'); + expect(Number.parseInt(data[0].is_locked)).to.be.ok; + }); + }); }); }); }); From aead58ab6626e29090a44684a3197c4bee72012f Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Wed, 1 Dec 2021 00:33:38 +0100 Subject: [PATCH 3/3] fix expect result --- test/integration2/migrate/migration-integration.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration2/migrate/migration-integration.spec.js b/test/integration2/migrate/migration-integration.spec.js index d17bbbb67d..2c138ea4b8 100644 --- a/test/integration2/migrate/migration-integration.spec.js +++ b/test/integration2/migrate/migration-integration.spec.js @@ -1332,7 +1332,7 @@ describe('Migrations', function () { it('should insert is_locked value to 1 if lock table not exists', async () => { const result = await ensureTable('test', undefined, knex); - expect(result.rowCount).is.equal(1); + expect(!!(result || result.length)).is.true; const data = await knex('test_lock').select('*'); expect(data[0]).to.have.property('is_locked'); expect(Number.parseInt(data[0].is_locked)).to.not.be.ok;