From bbf59fc6c1fc2e909d72c034687443b260278428 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 31 Oct 2017 15:47:30 +0000 Subject: [PATCH] Refactored to save settings only if value changes (#9194) refs #9192 - Each setting is saved individually - Update this to only happen on import, or when a value changes - Reduces the amount of work Ghost does on every setting change --- core/server/models/settings.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/core/server/models/settings.js b/core/server/models/settings.js index 8d26284705..3d5c625f41 100644 --- a/core/server/models/settings.js +++ b/core/server/models/settings.js @@ -118,22 +118,27 @@ Settings = ghostBookshelf.Model.extend({ item = self.filterData(item); return Settings.forge({key: item.key}).fetch(options).then(function then(setting) { - var saveData = {}; - if (setting) { - if (item.hasOwnProperty('value')) { - saveData.value = item.value; - } - // Internal context can overwrite type (for fixture migrations) - if (options.context && options.context.internal && item.hasOwnProperty('type')) { - saveData.type = item.type; - } // it's allowed to edit all attributes in case of importing/migrating if (options.importing) { - saveData = item; - } + return setting.save(item, options); + } else { + // If we have a value, set it. + if (item.hasOwnProperty('value')) { + setting.set('value', item.value); + } + // Internal context can overwrite type (for fixture migrations) + if (options.context && options.context.internal && item.hasOwnProperty('type')) { + setting.set('type', item.type); + } - return setting.save(saveData, options); + // If anything has changed, save the updated model + if (setting.hasChanged()) { + return setting.save(null, options); + } + + return setting; + } } return Promise.reject(new errors.NotFoundError({message: i18n.t('errors.models.settings.unableToFindSetting', {key: item.key})}));