Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCavadenti committed Nov 30, 2021
1 parent c0bdcf1 commit f846ced
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/migrations/migrate/Migrator.js
Expand Up @@ -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');
}
});
Expand Down
32 changes: 32 additions & 0 deletions test/integration2/migrate/migration-integration.spec.js
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
});
});
});
});
});

0 comments on commit f846ced

Please sign in to comment.