Skip to content

Commit

Permalink
Refactored to save settings only if value changes (#9194)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ErisDS committed Oct 31, 2017
1 parent b151637 commit bbf59fc
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions core/server/models/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

// If anything has changed, save the updated model
if (setting.hasChanged()) {
return setting.save(null, options);
}

return setting;
}

return setting.save(saveData, options);
}

return Promise.reject(new errors.NotFoundError({message: i18n.t('errors.models.settings.unableToFindSetting', {key: item.key})}));
Expand Down

0 comments on commit bbf59fc

Please sign in to comment.