mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-26 04:13:30 +03:00
Proper settings infrastructure, allowing new features without compromising old data.
On server load, check for settings which have not been set, and apply a default value to the settings table from a JSON file.
This commit is contained in:
parent
978502579f
commit
e24b5c3382
@ -145,6 +145,8 @@ Ghost.prototype.init = function () {
|
|||||||
instance.getPaths(),
|
instance.getPaths(),
|
||||||
instance.mail.init(self)
|
instance.mail.init(self)
|
||||||
).then(function () {
|
).then(function () {
|
||||||
|
return models.Settings.populateDefaults();
|
||||||
|
}).then(function () {
|
||||||
return self.initPlugins();
|
return self.initPlugins();
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
// Initialize the settings cache
|
// Initialize the settings cache
|
||||||
|
47
core/server/data/default-settings.json
Normal file
47
core/server/data/default-settings.json
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"key": "title",
|
||||||
|
"value": "Ghost",
|
||||||
|
"type": "blog"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "description",
|
||||||
|
"value": "Just a blogging platform.",
|
||||||
|
"type": "blog"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "email",
|
||||||
|
"value": "ghost@example.com",
|
||||||
|
"type": "general"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "activePlugins",
|
||||||
|
"value": "",
|
||||||
|
"type": "general"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "activeTheme",
|
||||||
|
"value": "content/themes/casper",
|
||||||
|
"type": "general"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "currentVersion",
|
||||||
|
"value": "002",
|
||||||
|
"type": "core"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "installedPlugins",
|
||||||
|
"value": "[]",
|
||||||
|
"type": "core"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "logo",
|
||||||
|
"value": "",
|
||||||
|
"type": "blog"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "icon",
|
||||||
|
"value": "",
|
||||||
|
"type": "blog"
|
||||||
|
}
|
||||||
|
]
|
@ -25,65 +25,6 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
||||||
settings: [
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "url",
|
|
||||||
"value": "http://localhost:2368",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "blog"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "title",
|
|
||||||
"value": "Ghost",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "blog"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "description",
|
|
||||||
"value": "Just a blogging platform.",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "blog"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "email",
|
|
||||||
"value": "ghost@example.com",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "general"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "activePlugins",
|
|
||||||
"value": "",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "general"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "activeTheme",
|
|
||||||
"value": "content/themes/casper",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "general"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "currentVersion",
|
|
||||||
"value": "001",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "core"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
|
|
||||||
roles: [
|
roles: [
|
||||||
{
|
{
|
||||||
"id": 1,
|
"id": 1,
|
||||||
|
@ -3,34 +3,6 @@ var uuid = require('node-uuid');
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
posts: [],
|
posts: [],
|
||||||
|
|
||||||
settings: [
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "installedPlugins",
|
|
||||||
"value": "[]",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "core"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "logo",
|
|
||||||
"value": "",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "blog"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"uuid": uuid.v4(),
|
|
||||||
"key": "icon",
|
|
||||||
"value": "",
|
|
||||||
"created_by": 1,
|
|
||||||
"updated_by": 1,
|
|
||||||
"type": "blog"
|
|
||||||
}
|
|
||||||
|
|
||||||
],
|
|
||||||
|
|
||||||
roles: [],
|
roles: [],
|
||||||
|
|
||||||
permissions: [],
|
permissions: [],
|
||||||
|
@ -101,7 +101,7 @@ up = function () {
|
|||||||
// knex('roles_users').insert(fixtures.roles_users),
|
// knex('roles_users').insert(fixtures.roles_users),
|
||||||
knex('permissions').insert(fixtures.permissions),
|
knex('permissions').insert(fixtures.permissions),
|
||||||
knex('permissions_roles').insert(fixtures.permissions_roles),
|
knex('permissions_roles').insert(fixtures.permissions_roles),
|
||||||
knex('settings').insert(fixtures.settings)
|
knex('settings').insert({ key: 'currentVersion', 'value': '001', type: 'core' })
|
||||||
]);
|
]);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -58,14 +58,6 @@ up = function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
]).then(function () {
|
]).then(function () {
|
||||||
|
|
||||||
// Once we create all of the initial tables, bootstrap any of the data
|
|
||||||
return when.all([
|
|
||||||
knex('settings').insert(fixtures.settings)
|
|
||||||
]);
|
|
||||||
|
|
||||||
}).then(function () {
|
|
||||||
|
|
||||||
// Lastly, update the current version settings to reflect this version
|
// Lastly, update the current version settings to reflect this version
|
||||||
return knex('settings')
|
return knex('settings')
|
||||||
.where('key', 'currentVersion')
|
.where('key', 'currentVersion')
|
||||||
@ -84,6 +76,7 @@ down = function () {
|
|||||||
knex.Schema.dropTableIfExists("posts_custom_data")
|
knex.Schema.dropTableIfExists("posts_custom_data")
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
// Should we also drop the currentVersion?
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.up = up;
|
exports.up = up;
|
||||||
|
@ -7,6 +7,7 @@ var _ = require('underscore'),
|
|||||||
initialVersion = "001",
|
initialVersion = "001",
|
||||||
// This currentVersion string should always be the current version of Ghost,
|
// This currentVersion string should always be the current version of Ghost,
|
||||||
// we could probably load it from the config file.
|
// we could probably load it from the config file.
|
||||||
|
// - Will be possible after default-settings.json restructure
|
||||||
currentVersion = "002";
|
currentVersion = "002";
|
||||||
|
|
||||||
function getCurrentVersion() {
|
function getCurrentVersion() {
|
||||||
|
@ -3,7 +3,8 @@ var Settings,
|
|||||||
uuid = require('node-uuid'),
|
uuid = require('node-uuid'),
|
||||||
_ = require('underscore'),
|
_ = require('underscore'),
|
||||||
errors = require('../errorHandling'),
|
errors = require('../errorHandling'),
|
||||||
when = require('when');
|
when = require('when'),
|
||||||
|
defaultSettings = require('../data/default-settings.json');
|
||||||
|
|
||||||
// Each setting is saved as a separate row in the database,
|
// Each setting is saved as a separate row in the database,
|
||||||
// but the overlying API treats them as a single key:value mapping
|
// but the overlying API treats them as a single key:value mapping
|
||||||
@ -36,7 +37,7 @@ Settings = GhostBookshelf.Model.extend({
|
|||||||
saving: function () {
|
saving: function () {
|
||||||
// Deal with the related data here
|
// Deal with the related data here
|
||||||
|
|
||||||
// Remove any properties which don't belong on the post model
|
// Remove any properties which don't belong on the model
|
||||||
this.attributes = this.pick(this.permittedAttributes);
|
this.attributes = this.pick(this.permittedAttributes);
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
@ -57,12 +58,33 @@ Settings = GhostBookshelf.Model.extend({
|
|||||||
// Accept an array of models as input
|
// Accept an array of models as input
|
||||||
if (item.toJSON) { item = item.toJSON(); }
|
if (item.toJSON) { item = item.toJSON(); }
|
||||||
return settings.forge({ key: item.key }).fetch().then(function (setting) {
|
return settings.forge({ key: item.key }).fetch().then(function (setting) {
|
||||||
|
if (setting) {
|
||||||
return setting.set('value', item.value).save();
|
return setting.set('value', item.value).save();
|
||||||
|
}
|
||||||
|
return settings.forge({ key: item.key, value: item.value }).save();
|
||||||
|
|
||||||
}, errors.logAndThrowError);
|
}, errors.logAndThrowError);
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
populateDefaults: function () {
|
||||||
|
return this.findAll().then(function (allSettings) {
|
||||||
|
var usedKeys = allSettings.models.map(function (setting) { return setting.get('key'); }),
|
||||||
|
insertOperations = [];
|
||||||
|
|
||||||
|
defaultSettings.forEach(function (defaultSetting) {
|
||||||
|
var isMissingFromDB = usedKeys.indexOf(defaultSetting.key) === -1;
|
||||||
|
if (isMissingFromDB) {
|
||||||
|
insertOperations.push(Settings.forge(defaultSetting).save());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return when.all(insertOperations);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Settings: Settings
|
Settings: Settings
|
||||||
};
|
};
|
@ -2,7 +2,8 @@
|
|||||||
var _ = require("underscore"),
|
var _ = require("underscore"),
|
||||||
should = require('should'),
|
should = require('should'),
|
||||||
helpers = require('./helpers'),
|
helpers = require('./helpers'),
|
||||||
Models = require('../../server/models');
|
Models = require('../../server/models'),
|
||||||
|
knex = require('../../server/models/base').Knex;
|
||||||
|
|
||||||
describe('Settings Model', function () {
|
describe('Settings Model', function () {
|
||||||
|
|
||||||
@ -27,6 +28,8 @@ describe('Settings Model', function () {
|
|||||||
}, done);
|
}, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('API', function () {
|
||||||
|
|
||||||
it('can browse', function (done) {
|
it('can browse', function (done) {
|
||||||
SettingsModel.browse().then(function (results) {
|
SettingsModel.browse().then(function (results) {
|
||||||
|
|
||||||
@ -189,3 +192,43 @@ describe('Settings Model', function () {
|
|||||||
}).then(null, done);
|
}).then(null, done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('populating defaults from settings.json', function (done) {
|
||||||
|
|
||||||
|
beforeEach(function (done) {
|
||||||
|
knex('settings').truncate().then(function () {
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('populates any unset settings from the JSON defaults', function (done) {
|
||||||
|
SettingsModel.findAll().then(function (allSettings) {
|
||||||
|
console.log(allSettings.models)
|
||||||
|
allSettings.length.should.equal(0);
|
||||||
|
return SettingsModel.populateDefaults();
|
||||||
|
}).then(function () {
|
||||||
|
return SettingsModel.findAll();
|
||||||
|
}).then(function (allSettings) {
|
||||||
|
allSettings.length.should.be.above(0);
|
||||||
|
return SettingsModel.read('description');
|
||||||
|
}).then(function (descriptionSetting) {
|
||||||
|
// Testing against the actual value in default-settings.json feels icky,
|
||||||
|
// but it's easier to fix the test if that ever changes than to mock out that behaviour
|
||||||
|
descriptionSetting.get('value').should.equal('Just a blogging platform.');
|
||||||
|
done();
|
||||||
|
}).then(null, done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('doesn\'t overwrite any existing settings', function (done) {
|
||||||
|
SettingsModel.edit({key: 'description', value: 'Adam\'s Blog'}).then(function () {
|
||||||
|
return SettingsModel.populateDefaults();
|
||||||
|
}).then(function () {
|
||||||
|
return SettingsModel.read('description');
|
||||||
|
}).then(function (descriptionSetting) {
|
||||||
|
descriptionSetting.get('value').should.equal('Adam\'s Blog');
|
||||||
|
done();
|
||||||
|
}).then(null, done);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
@ -8,7 +8,8 @@ var _ = require("underscore"),
|
|||||||
exporter = require('../../server/data/export'),
|
exporter = require('../../server/data/export'),
|
||||||
Exporter001 = require('../../server/data/export/001'),
|
Exporter001 = require('../../server/data/export/001'),
|
||||||
Exporter002 = require('../../server/data/export/002'),
|
Exporter002 = require('../../server/data/export/002'),
|
||||||
errors = require('../../server/errorHandling');
|
errors = require('../../server/errorHandling'),
|
||||||
|
Settings = require('../../server/models/settings').Settings;
|
||||||
|
|
||||||
describe("Export", function () {
|
describe("Export", function () {
|
||||||
|
|
||||||
@ -43,6 +44,8 @@ describe("Export", function () {
|
|||||||
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
||||||
// than the migration version we want
|
// than the migration version we want
|
||||||
migration.migrateUpFromVersion('001', '002').then(function () {
|
migration.migrateUpFromVersion('001', '002').then(function () {
|
||||||
|
return Settings.populateDefaults();
|
||||||
|
}).then(function () {
|
||||||
return exporter("001");
|
return exporter("001");
|
||||||
}).then(function (exportData) {
|
}).then(function (exportData) {
|
||||||
var tables = ['posts', 'users', 'roles', 'roles_users', 'permissions', 'permissions_roles', 'settings'];
|
var tables = ['posts', 'users', 'roles', 'roles_users', 'permissions', 'permissions_roles', 'settings'];
|
||||||
@ -88,6 +91,8 @@ describe("Export", function () {
|
|||||||
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
||||||
// than the migration version we want
|
// than the migration version we want
|
||||||
migration.migrateUpFromVersion('001', '003').then(function () {
|
migration.migrateUpFromVersion('001', '003').then(function () {
|
||||||
|
return Settings.populateDefaults();
|
||||||
|
}).then(function () {
|
||||||
return exporter("002");
|
return exporter("002");
|
||||||
}).then(function (exportData) {
|
}).then(function (exportData) {
|
||||||
var tables = [
|
var tables = [
|
||||||
|
@ -4,6 +4,7 @@ process.env.NODE_ENV = process.env.TRAVIS ? 'travis' : 'testing';
|
|||||||
var knex = require('../../server/models/base').Knex,
|
var knex = require('../../server/models/base').Knex,
|
||||||
when = require('when'),
|
when = require('when'),
|
||||||
migration = require("../../server/data/migration/"),
|
migration = require("../../server/data/migration/"),
|
||||||
|
Settings = require('../../server/models/settings').Settings,
|
||||||
helpers,
|
helpers,
|
||||||
samplePost,
|
samplePost,
|
||||||
sampleUser,
|
sampleUser,
|
||||||
@ -44,7 +45,9 @@ sampleUserRole = function (i) {
|
|||||||
helpers = {
|
helpers = {
|
||||||
|
|
||||||
initData: function (done) {
|
initData: function (done) {
|
||||||
return migration.init();
|
return migration.init().then(function () {
|
||||||
|
return Settings.populateDefaults();
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
clearData: function () {
|
clearData: function () {
|
||||||
|
@ -10,7 +10,8 @@ var _ = require("underscore"),
|
|||||||
importer = require('../../server/data/import'),
|
importer = require('../../server/data/import'),
|
||||||
Importer001 = require('../../server/data/import/001'),
|
Importer001 = require('../../server/data/import/001'),
|
||||||
Importer002 = require('../../server/data/import/002'),
|
Importer002 = require('../../server/data/import/002'),
|
||||||
errors = require('../../server/errorHandling');
|
errors = require('../../server/errorHandling'),
|
||||||
|
Settings = require('../../server/models/settings').Settings;
|
||||||
|
|
||||||
describe("Import", function () {
|
describe("Import", function () {
|
||||||
|
|
||||||
@ -50,6 +51,8 @@ describe("Import", function () {
|
|||||||
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
||||||
// than the migration version we want
|
// than the migration version we want
|
||||||
migration.migrateUpFromVersion('001', '002').then(function () {
|
migration.migrateUpFromVersion('001', '002').then(function () {
|
||||||
|
return Settings.populateDefaults();
|
||||||
|
}).then(function () {
|
||||||
// export the version 001 data ready to import
|
// export the version 001 data ready to import
|
||||||
// TODO: Should have static test data here?
|
// TODO: Should have static test data here?
|
||||||
return exporter("001");
|
return exporter("001");
|
||||||
@ -83,8 +86,7 @@ describe("Import", function () {
|
|||||||
// we always have 0 users as there isn't one in fixtures
|
// we always have 0 users as there isn't one in fixtures
|
||||||
importedData[0].length.should.equal(0);
|
importedData[0].length.should.equal(0);
|
||||||
importedData[1].length.should.equal(exportData.data.posts.length);
|
importedData[1].length.should.equal(exportData.data.posts.length);
|
||||||
// version 001 settings have 7 fields
|
importedData[2].length.should.be.above(0);
|
||||||
importedData[2].length.should.equal(7);
|
|
||||||
|
|
||||||
_.findWhere(exportData.data.settings, {key: "currentVersion"}).value.should.equal("001");
|
_.findWhere(exportData.data.settings, {key: "currentVersion"}).value.should.equal("001");
|
||||||
|
|
||||||
@ -119,6 +121,8 @@ describe("Import", function () {
|
|||||||
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
||||||
// than the migration version we want
|
// than the migration version we want
|
||||||
migration.migrateUpFromVersion('001', '002').then(function () {
|
migration.migrateUpFromVersion('001', '002').then(function () {
|
||||||
|
return Settings.populateDefaults();
|
||||||
|
}).then(function () {
|
||||||
// export the version 001 data ready to import
|
// export the version 001 data ready to import
|
||||||
// TODO: Should have static test data here?
|
// TODO: Should have static test data here?
|
||||||
return exporter("001");
|
return exporter("001");
|
||||||
@ -146,8 +150,7 @@ describe("Import", function () {
|
|||||||
importedData[0].length.should.equal(0);
|
importedData[0].length.should.equal(0);
|
||||||
// import no longer requires all data to be dropped, and adds posts
|
// import no longer requires all data to be dropped, and adds posts
|
||||||
importedData[1].length.should.equal(exportData.data.posts.length + 1);
|
importedData[1].length.should.equal(exportData.data.posts.length + 1);
|
||||||
// version 002 settings have 10 fields, and settings get updated not inserted
|
importedData[2].length.should.be.above(0);
|
||||||
importedData[2].length.should.equal(10);
|
|
||||||
|
|
||||||
_.findWhere(importedData[2], {key: "currentVersion"}).value.should.equal("002");
|
_.findWhere(importedData[2], {key: "currentVersion"}).value.should.equal("002");
|
||||||
|
|
||||||
@ -161,6 +164,8 @@ describe("Import", function () {
|
|||||||
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
// initialise database to version 001 - confusingly we have to set the max version to be one higher
|
||||||
// than the migration version we want
|
// than the migration version we want
|
||||||
migration.migrateUpFromVersion('001', '003').then(function () {
|
migration.migrateUpFromVersion('001', '003').then(function () {
|
||||||
|
return Settings.populateDefaults();
|
||||||
|
}).then(function () {
|
||||||
// export the version 002 data ready to import
|
// export the version 002 data ready to import
|
||||||
// TODO: Should have static test data here?
|
// TODO: Should have static test data here?
|
||||||
return exporter("002");
|
return exporter("002");
|
||||||
@ -184,8 +189,7 @@ describe("Import", function () {
|
|||||||
importedData[0].length.should.equal(0);
|
importedData[0].length.should.equal(0);
|
||||||
// import no longer requires all data to be dropped, and adds posts
|
// import no longer requires all data to be dropped, and adds posts
|
||||||
importedData[1].length.should.equal(exportData.data.posts.length + 1);
|
importedData[1].length.should.equal(exportData.data.posts.length + 1);
|
||||||
// version 002 settings have 10 fields, and settings get updated not inserted
|
importedData[2].length.should.be.above(0);
|
||||||
importedData[2].length.should.equal(10);
|
|
||||||
|
|
||||||
_.findWhere(importedData[2], {key: "currentVersion"}).value.should.equal("002");
|
_.findWhere(importedData[2], {key: "currentVersion"}).value.should.equal("002");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user