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
This commit is contained in:
Hannah Wolfe 2017-10-31 15:47:30 +00:00 committed by GitHub
parent b1516375bb
commit bbf59fc6c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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})}));