Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💡 Changed the lock_key column constraint to primary key #243

Merged
merged 2 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions migrations/add-primary-key-to-lock-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const debug = require('debug')('knex-migrator:lock-table');

/**
* Checks if primary key index exists in a table over the given columns.
*/
function hasPrimaryKey(tableName, knex) {
const client = knex.client.config.client;

if (client === 'mysql') {
const dbName = knex.client.config.connection.database;
return knex.raw(`
SELECT CONSTRAINT_NAME
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA=:dbName
AND TABLE_NAME=:tableName
AND CONSTRAINT_TYPE='PRIMARY KEY'`, {dbName, tableName})
.then(([rawConstraints]) => {
return rawConstraints.length > 0;
});
} else {
return knex.raw(`PRAGMA index_list('${tableName}');`)
.then((rawConstraints) => {
const tablePrimaryKey = rawConstraints.find(c => c.origin === 'pk');
return tablePrimaryKey;
});
}
}

/**
* Adds an primary key index to a table over the given columns.
*/
function addPrimaryKey(tableName, columns, knex) {
return hasPrimaryKey(tableName, knex)
.then((hasUniqueConstraint) => {
if (!hasUniqueConstraint) {
debug(`Adding primary key constraint for: ${columns} in table ${tableName}`);
return knex.schema.table(tableName, function (table) {
table.primary(columns);
});
} else {
debug(`Primary key constraint for: ${columns} already exists for table: ${tableName}`);
}
});
}

/**
* @description Private helper to create add a primary key to the migration lock table. The helper is called as part of `runDatabaseUpgrades`.
* @returns {*}
*/
module.exports.up = function (connection) {
debug('Add primary key to the lock table.');

return addPrimaryKey('migrations_lock', 'lock_key', connection);
};
4 changes: 3 additions & 1 deletion migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const lockTable = require('./lock-table');
const fieldLength = require('./field-length');
const useIndex = require('./use-index');
const addPKToLockTable = require('./add-primary-key-to-lock-table');

/**
* @description Helper to run database migrations for the database tables, which knex-migrator is using.
Expand All @@ -11,5 +12,6 @@ module.exports.run = function (connection) {
return Promise.resolve()
.then(() => lockTable.up(connection))
.then(() => fieldLength.up(connection))
.then(() => useIndex.up(connection));
.then(() => useIndex.up(connection))
.then(() => addPKToLockTable.up(connection));
};
2 changes: 1 addition & 1 deletion migrations/lock-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports.up = function (connection) {
}

return connection.schema.createTable('migrations_lock', function (table) {
table.string('lock_key', 191).nullable(false).unique();
table.string('lock_key', 191).nullable(false).primary();
table.boolean('locked').default(0);
table.dateTime('acquired_at').nullable();
table.dateTime('released_at').nullable();
Expand Down
18 changes: 18 additions & 0 deletions test/functional/functional_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,5 +798,23 @@ _.each(['default', 'migrateInit'], function (initMethod) {
});
});
});

it('change current version', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much love for this ❤️

knexMigrator.currentVersion = '1.4';
if (config.get('database:client') === 'sqlite3') {
return connection.raw(`PRAGMA index_list('migrations_lock');`).then((indexes) => {
indexes.filter(index => index.origin === 'pk').length.should.eql(1);
});
} else {
return connection.raw(`
SELECT CONSTRAINT_NAME
FROM information_schema.TABLE_CONSTRAINTS
WHERE TABLE_NAME=:tableName
AND CONSTRAINT_TYPE='PRIMARY KEY'`, {tableName: 'migrations_lock'})
.then(([rawConstraints]) => {
rawConstraints.length.should.eql(1);
});
}
});
});
});