Fixed slack setting migration to follow idempotance rule

refs https://github.com/TryGhost/Ghost/pull/12631

- Initialy writen migration would break if executed twice
- Also accounted for situation if only part of the migration executed leaving only one of the values migrated
This commit is contained in:
Naz 2021-02-17 16:52:39 +13:00 committed by naz
parent 9964a5c23a
commit 0097f2037c

View File

@ -1,7 +1,29 @@
const ObjectId = require('bson-objectid').default; const ObjectId = require('bson-objectid').default;
const {createIrreversibleMigration} = require('../../utils'); const {createIrreversibleMigration} = require('../../utils');
const logging = require('../../../../../shared/logging');
module.exports = createIrreversibleMigration(async (knex) => { module.exports = createIrreversibleMigration(async (knex) => {
logging.info('Populating slack_url and slack_username setting values');
const slackURLSetting = await knex('settings')
.select('value')
.where({
key: 'slack_url'
})
.first();
const slackUsernameSetting = await knex('settings')
.select('value')
.where({
key: 'slack_username'
})
.first();
if (slackURLSetting && slackUsernameSetting) {
logging.warn('slack_url and slack_username setting already exitst');
return;
}
const slackSetting = await knex('settings') const slackSetting = await knex('settings')
.select('value') .select('value')
.where({ .where({
@ -28,6 +50,9 @@ module.exports = createIrreversibleMigration(async (knex) => {
const now = knex.raw('CURRENT_TIMESTAMP'); const now = knex.raw('CURRENT_TIMESTAMP');
if (!slackURLSetting) {
logging.info(`Populating slack_url setting with value: ${slackUrl}`);
await knex('settings') await knex('settings')
.insert({ .insert({
id: ObjectId.generate(), id: ObjectId.generate(),
@ -41,6 +66,10 @@ module.exports = createIrreversibleMigration(async (knex) => {
updated_by: 1, updated_by: 1,
updated_at: now updated_at: now
}); });
}
if (!slackUsernameSetting) {
logging.info(`Populating slack_username setting with value: ${slackUsername}`);
await knex('settings') await knex('settings')
.insert({ .insert({
@ -55,6 +84,9 @@ module.exports = createIrreversibleMigration(async (knex) => {
updated_by: 1, updated_by: 1,
updated_at: now updated_at: now
}); });
}
logging.info(`Removing slack setting`);
await knex('settings') await knex('settings')
.where({ .where({