Skip to content

Commit

Permalink
Avoid inserting multiple locks if a lock already exists (knex#4694)
Browse files Browse the repository at this point in the history
  • Loading branch information
stigerland authored and rustyconover committed Oct 18, 2021
1 parent e4bd9b9 commit 6cf68ba
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/migrations/migrate/table-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {

function ensureTable(tableName, schemaName, trxOrKnex) {
const lockTable = getLockTableName(tableName);
const lockTableWithSchema = getLockTableNameWithSchema(tableName, schemaName);
return getSchemaBuilder(trxOrKnex, schemaName)
.hasTable(tableName)
.then((exists) => {
Expand All @@ -26,8 +25,7 @@ function ensureTable(tableName, schemaName, trxOrKnex) {
})
.then((data) => {
return (
!data.length &&
trxOrKnex.into(lockTableWithSchema).insert({ is_locked: 0 })
!data.length && _insertLockRowIfNeeded(tableName, schemaName, trxOrKnex)
);
});
}
Expand All @@ -54,6 +52,17 @@ 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);
});
});
}

//Get schema-aware schema builder for a given schema nam
function getSchemaBuilder(trxOrKnex, schemaName) {
return schemaName
Expand Down

0 comments on commit 6cf68ba

Please sign in to comment.