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,33 +50,43 @@ module.exports = createIrreversibleMigration(async (knex) => {
const now = knex.raw('CURRENT_TIMESTAMP'); const now = knex.raw('CURRENT_TIMESTAMP');
await knex('settings') if (!slackURLSetting) {
.insert({ logging.info(`Populating slack_url setting with value: ${slackUrl}`);
id: ObjectId.generate(),
key: 'slack_url',
group: 'slack',
type: 'string',
flags: null,
value: slackUrl,
created_by: 1,
created_at: now,
updated_by: 1,
updated_at: now
});
await knex('settings') await knex('settings')
.insert({ .insert({
id: ObjectId.generate(), id: ObjectId.generate(),
key: 'slack_username', key: 'slack_url',
group: 'slack', group: 'slack',
type: 'string', type: 'string',
flags: null, flags: null,
value: slackUsername, value: slackUrl,
created_by: 1, created_by: 1,
created_at: now, created_at: now,
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')
.insert({
id: ObjectId.generate(),
key: 'slack_username',
group: 'slack',
type: 'string',
flags: null,
value: slackUsername,
created_by: 1,
created_at: now,
updated_by: 1,
updated_at: now
});
}
logging.info(`Removing slack setting`);
await knex('settings') await knex('settings')
.where({ .where({