Skip to content

Commit

Permalink
🎨 Preparation for going alpha (#7404)
Browse files Browse the repository at this point in the history
- Don't let people start Ghost Alpha with non-alpha databases.
- Provide a new welcome message for development mode (a little bit of positive reinforcment)
- Provide a RED WARNING when in production mode (will still be used for developing, but we can ignore)
- Change package.json to 1.0.0-alpha.0, we won't relelase this, will bump to .1 for release
  • Loading branch information
ErisDS committed Sep 20, 2016
1 parent fa07cc7 commit 9a520f3
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 67 deletions.
27 changes: 27 additions & 0 deletions core/server/data/schema/bootup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var Promise = require('bluebird'),
versioning = require('./versioning'),
migrations = require('../migration'),
errors = require('./../../errors');

module.exports = function bootUp() {
return versioning
.getDatabaseVersion()
.then(function successHandler(result) {
if (!/^alpha/.test(result)) {
// This database was not created with Ghost alpha, and is not compatible
throw new errors.DatabaseVersion(
'Your database version is not compatible with Ghost 1.0.0 Alpha (master branch)',
'Want to keep your DB? Use Ghost < 1.0.0 or the "stable" branch. Otherwise please delete your DB and restart Ghost',
'More information on the Ghost 1.0.0 Alpha at https://support.ghost.org/v1-0-alpha'
);
}
},
// We don't use .catch here, as it would catch the error from the successHandler
function errorHandler(err) {
if (err instanceof errors.DatabaseNotPopulated) {
return migrations.populate();
}

return Promise.reject(err);
});
};
2 changes: 1 addition & 1 deletion core/server/data/schema/default-settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"core": {
"databaseVersion": {
"defaultValue": "008"
"defaultValue": "alpha.1"
},
"dbHash": {
"defaultValue": null
Expand Down
17 changes: 6 additions & 11 deletions core/server/data/schema/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
var schema = require('./schema'),
checks = require('./checks'),
commands = require('./commands'),
versioning = require('./versioning'),
defaultSettings = require('./default-settings');

module.exports.tables = schema;
module.exports.checks = checks;
module.exports.commands = commands;
module.exports.versioning = versioning;
module.exports.defaultSettings = defaultSettings;
module.exports.tables = require('./schema');
module.exports.checks = require('./checks');
module.exports.commands = require('./commands');
module.exports.versioning = require('./versioning');
module.exports.defaultSettings = require('./default-settings');
module.exports.bootUp = require('./bootup');
4 changes: 0 additions & 4 deletions core/server/data/schema/versioning.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ function getDatabaseVersion() {
.where('key', 'databaseVersion')
.first('value')
.then(function (version) {
if (!version || isNaN(version.value)) {
return Promise.reject(new errors.DatabaseVersion(i18n.t('errors.data.versioning.index.dbVersionNotRecognized')));
}

return version.value;
});
}
Expand Down
11 changes: 9 additions & 2 deletions core/server/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ errors = {
var self = this,
origArgs = _.toArray(arguments).slice(1),
stack,
msgs;
msgs,
hideStack = false;

// DatabaseVersion errors are usually fatal, we output a nice message
// And the stack is not at all useful in this case
if (err instanceof DatabaseVersion) {
hideStack = true;
}

if (_.isArray(err)) {
_.each(err, function (e) {
Expand Down Expand Up @@ -172,7 +179,7 @@ errors = {
// add a new line
msgs.push('\n');

if (stack) {
if (stack && !hideStack) {
msgs.push(stack, '\n');
}

Expand Down
8 changes: 6 additions & 2 deletions core/server/ghost-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,15 @@ GhostServer.prototype.logStartMessages = function () {
// Startup & Shutdown messages
if (process.env.NODE_ENV === 'production') {
console.log(
chalk.green(i18n.t('notices.httpServer.ghostIsRunningIn', {env: process.env.NODE_ENV})),
i18n.t('notices.httpServer.yourBlogIsAvailableOn', {url: config.get('url')}),
chalk.red('Currently running Ghost 1.0.0 Alpha, this is NOT suitable for production! \n'),
chalk.white('Please switch to the stable branch. \n'),
chalk.white('More information on the Ghost 1.0.0 Alpha at: ') + chalk.cyan('https://support.ghost.org/v1-0-alpha') + '\n',
chalk.gray(i18n.t('notices.httpServer.ctrlCToShutDown'))
);
} else {
console.log(
chalk.blue('Welcome to the Ghost 1.0.0 Alpha - this version of Ghost is for development only.')
);
console.log(
chalk.green(i18n.t('notices.httpServer.ghostIsRunningIn', {env: process.env.NODE_ENV})),
i18n.t('notices.httpServer.listeningOn'),
Expand Down
42 changes: 3 additions & 39 deletions core/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ var express = require('express'),
config = require('./config'),
errors = require('./errors'),
middleware = require('./middleware'),
migrations = require('./data/migration'),
versioning = require('./data/schema/versioning'),
db = require('./data/schema'),
models = require('./models'),
permissions = require('./permissions'),
apps = require('./apps'),
Expand Down Expand Up @@ -77,43 +76,8 @@ function init(options) {
return api.themes.loadThemes();
}).then(function () {
models.init();
}).then(function () {
return versioning.getDatabaseVersion()
.then(function (currentVersion) {
var response = migrations.update.isDatabaseOutOfDate({
fromVersion: currentVersion,
toVersion: versioning.getNewestDatabaseVersion(),
forceMigration: process.env.FORCE_MIGRATION
}), maintenanceState;

if (response.migrate === true) {
maintenanceState = config.get('maintenance').enabled || false;
config.set('maintenance:enabled', true);

migrations.update.execute({
fromVersion: currentVersion,
toVersion: versioning.getNewestDatabaseVersion(),
forceMigration: process.env.FORCE_MIGRATION
}).then(function () {
config.set('maintenance:enabled', maintenanceState);
}).catch(function (err) {
if (!err) {
return;
}

errors.logErrorAndExit(err, err.context, err.help);
});
} else if (response.error) {
return Promise.reject(response.error);
}
})
.catch(function (err) {
if (err instanceof errors.DatabaseNotPopulated) {
return migrations.populate();
}

return Promise.reject(err);
});
// @TODO: this is temporary, replace migrations with a warning if a DB exists
return db.bootUp();
}).then(function () {
// Populate any missing default settings
return models.Settings.populateDefaults();
Expand Down
2 changes: 1 addition & 1 deletion core/test/unit/migration_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var should = require('should'),
// both of which are required for migrations to work properly.
describe('DB version integrity', function () {
// Only these variables should need updating
var currentDbVersion = '008',
var currentDbVersion = 'alpha.1',
currentSchemaHash = 'b3bdae210526b2d4393359c3e45d7f83',
currentFixturesHash = '30b0a956b04e634e7f2cddcae8d2fd20';

Expand Down
52 changes: 48 additions & 4 deletions core/test/unit/server_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var should = require('should'),
sandbox = sinon.sandbox.create();

describe('server bootstrap', function () {
var middlewareStub, resetMiddlewareStub, initDbHashAndFirstRunStub, resetInitDbHashAndFirstRunStub;
var middlewareStub, resetMiddlewareStub, initDbHashAndFirstRunStub, resetInitDbHashAndFirstRunStub,
populateStub;

before(function () {
models.init();
Expand All @@ -28,7 +29,7 @@ describe('server bootstrap', function () {
middlewareStub = sandbox.stub();
initDbHashAndFirstRunStub = sandbox.stub();

sandbox.stub(migration, 'populate').returns(Promise.resolve());
populateStub = sandbox.stub(migration, 'populate').returns(Promise.resolve());
sandbox.stub(models.Settings, 'populateDefaults').returns(Promise.resolve());
sandbox.stub(permissions, 'init').returns(Promise.resolve());
sandbox.stub(api, 'init').returns(Promise.resolve());
Expand Down Expand Up @@ -69,7 +70,11 @@ describe('server bootstrap', function () {
});
});

it('database does exist: expect no update', function (done) {
// @TODO fix these two tests once we've decided on a new migration
// versioning scheme
// the tests do not work right now because if the version isn't an
// alpha version, we error. I've added two temporary tests to show this.
it.skip('database does exist: expect no update', function (done) {
sandbox.stub(migration.update, 'isDatabaseOutOfDate').returns({migrate:false});
sandbox.spy(migration.update, 'execute');

Expand All @@ -91,7 +96,7 @@ describe('server bootstrap', function () {
});
});

it('database does exist: expect update', function (done) {
it.skip('database does exist: expect update', function (done) {
sandbox.stub(migration.update, 'isDatabaseOutOfDate').returns({migrate:true});
sandbox.stub(migration.update, 'execute').returns(Promise.resolve());

Expand Down Expand Up @@ -120,5 +125,44 @@ describe('server bootstrap', function () {
done(err);
});
});

// @TODO remove these temporary tests ;)
it('TEMP: database does exist: expect alpha error', function (done) {
sandbox.stub(migration.update, 'isDatabaseOutOfDate').returns({migrate:false});
sandbox.spy(migration.update, 'execute');

sandbox.stub(versioning, 'getDatabaseVersion', function () {
return Promise.resolve('006');
});

bootstrap()
.then(function () {
done('This should not be called');
})
.catch(function (err) {
err.errorType.should.eql('DatabaseVersion');
err.message.should.eql('Your database version is not compatible with Ghost 1.0.0 Alpha (master branch)');
done();
});
});

it('TEMP: database does exist: expect alpha error', function (done) {
sandbox.stub(migration.update, 'isDatabaseOutOfDate').returns({migrate:true});
sandbox.stub(migration.update, 'execute').returns(Promise.resolve());

sandbox.stub(versioning, 'getDatabaseVersion', function () {
return Promise.resolve('006');
});

bootstrap()
.then(function () {
done('This should not be called');
})
.catch(function (err) {
err.errorType.should.eql('DatabaseVersion');
err.message.should.eql('Your database version is not compatible with Ghost 1.0.0 Alpha (master branch)');
done();
});
});
});
});
10 changes: 8 additions & 2 deletions core/test/unit/versioning_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ describe('Versioning', function () {
}).catch(done);
});

it('should throw error if version does not exist', function (done) {
// @TODO change this so we handle a non-existent version?
// There is an open bug in Ghost around this:
// https://github.com/TryGhost/Ghost/issues/7345
// I think it is a timing error
it.skip('should throw error if version does not exist', function (done) {
// Setup
knexMock.schema.hasTable.returns(new Promise.resolve(true));
queryMock.first.returns(new Promise.resolve());
Expand All @@ -128,7 +132,9 @@ describe('Versioning', function () {
}).catch(done);
});

it('should throw error if version is not a number', function (done) {
// @TODO decide on a new scheme for database versioning and update
// how we validate those versions
it.skip('should throw error if version is not a number', function (done) {
// Setup
knexMock.schema.hasTable.returns(new Promise.resolve(true));
queryMock.first.returns(new Promise.resolve('Eyjafjallajökull'));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ghost",
"version": "0.11.0",
"version": "1.0.0-alpha.0",
"description": "Just a blogging platform.",
"author": "Ghost Foundation",
"homepage": "http://ghost.org",
Expand Down

0 comments on commit 9a520f3

Please sign in to comment.