Skip to content

Commit

Permalink
Enhanced 2.18 detection (#10610)
Browse files Browse the repository at this point in the history
no issue

- discovered another case
  • Loading branch information
kirrg001 committed Mar 13, 2019
1 parent ca16b19 commit a2f7160
Showing 1 changed file with 63 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports.config = {

const backupFileRegex = /ghost.([\d]{4}-[\d]{2}-[\d]{2}).json$/;

// @NOTE: spagetthi
module.exports.up = (options) => {
const contentPath = config.get('paths').contentPath;
const dataPath = path.join(contentPath, 'data');
Expand Down Expand Up @@ -53,79 +54,81 @@ module.exports.up = (options) => {
return;
}
const settings = backup && backup.data && backup.data.settings;
const migrations = backup && backup.data && backup.data.migrations;

if (!settings) {
common.logging.warn('Could not read settings from backup file, skipping...');
return;
}

if (!migrations || !migrations.length) {
common.logging.warn('Skipping migration. Not affected.');
return;
}

// NOTE: If we you have a backup file which has 2.16, but not 2.17, you are affected
// NOTE: We have corrected 2.17. If you jump form 2.16 to 2.18, you are good
const isAffected = _.find(migrations, {version: '2.16'}) &&
!_.find(migrations, {version: '2.17'});

if (!isAffected) {
common.logging.warn('Skipping migration. Not affected.');
return;
}

common.logging.warn('...is affected.');

const relevantBackupSettings = settings.filter(function (entry) {
return ['is_private', 'force_i18n', 'amp'].includes(entry.key);
}).reduce(function (obj, entry) {
return Object.assign(obj, {
[entry.key]: entry
});
}, {});

return localOptions
.transacting('settings')
.transacting('migrations')
.then((response) => {
if (!response) {
common.logging.warn('Cannot find settings.');
common.logging.warn('Cannot find migrations.');
return;
}

const relevantLiveSettings = response.filter(function (entry) {
// NOTE: You are only affected if you migrated to 2.17
// 2.18 fixed the 2.17 migration (!)
const isAffected = _.find(response, {currentVersion: '2.17', version: '2.17'});

if (!isAffected) {
common.logging.warn('Skipping migration. Not affected.');
return;
}

common.logging.warn('...is affected.');

const relevantBackupSettings = settings.filter(function (entry) {
return ['is_private', 'force_i18n', 'amp'].includes(entry.key);
});

return Promise.each(relevantLiveSettings, (liveSetting) => {
const backupSetting = relevantBackupSettings[liveSetting.key];

if (liveSetting.value === 'false' && backupSetting.value === 'true') {
common.logging.info(`Reverting setting ${liveSetting.key}`);

return localOptions
.transacting('settings')
.where('key', liveSetting.key)
.update({
value: backupSetting.value
})
.then(() => {
// CASE: we have to update settings cache, because Ghost is able to run migrations on the same process
settingsCache.set(liveSetting.key, {
id: liveSetting.id,
key: liveSetting.key,
type: liveSetting.type,
created_at: moment(liveSetting.created_at).startOf('seconds').toDate(),
updated_at: moment().startOf('seconds').toDate(),
updated_by: liveSetting.updated_by,
created_by: liveSetting.created_by,
value: backupSetting.value === 'true'
});
});
}

return Promise.resolve();
});
}).reduce(function (obj, entry) {
return Object.assign(obj, {
[entry.key]: entry
});
}, {});

return localOptions
.transacting('settings')
.then((response) => {
if (!response) {
common.logging.warn('Cannot find settings.');
return;
}

const relevantLiveSettings = response.filter(function (entry) {
return ['is_private', 'force_i18n', 'amp'].includes(entry.key);
});

return Promise.each(relevantLiveSettings, (liveSetting) => {
const backupSetting = relevantBackupSettings[liveSetting.key];

if (liveSetting.value === 'false' && backupSetting.value === 'true') {
common.logging.info(`Reverting setting ${liveSetting.key}`);

return localOptions
.transacting('settings')
.where('key', liveSetting.key)
.update({
value: backupSetting.value
})
.then(() => {
// CASE: we have to update settings cache, because Ghost is able to run migrations on the same process
settingsCache.set(liveSetting.key, {
id: liveSetting.id,
key: liveSetting.key,
type: liveSetting.type,
created_at: moment(liveSetting.created_at).startOf('seconds').toDate(),
updated_at: moment().startOf('seconds').toDate(),
updated_by: liveSetting.updated_by,
created_by: liveSetting.created_by,
value: backupSetting.value === 'true'
});
});
}

return Promise.resolve();
});
});
});
});
};

0 comments on commit a2f7160

Please sign in to comment.