0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/test/unit/server_spec.js
Katharina Irrgang e2e83a0f7b Migration: New database versioning (#7499)
refs #7489

- new database versioning scheme which is based upon the Ghost version, and so easier to reason about
- massive refactor of all the version related code 

Summary of changes:

*   new error: DatabaseNotSeeded
* 🎨  change versioning module
  - versioning is based on Ghost Version
* 🎨  change bootUp file
  - add big picture description
  - version error get's trigger from versioning module
* 🎨  default setting for database version is null
  - very important change: this is caused by the big picture
  - see bootUp description
  - the database version get's set by the seed script later
  - db version is by default null
  - 1. population happens (we ensure that this has finished, by checking if each table exists)   
  - 2. seeds happening (we ensure that seeds happend if database version is set to X.X)
* 🎨  temporary change for population logic
  - set database version after population happens
  - ensure population of default settings happend before
  - both: get's removed in next iteration
* 🎨  adapt tests && mark TODO's
* 🎨  err instance checking
2016-10-06 14:50:55 +01:00

129 lines
5.4 KiB
JavaScript

/*jshint unused:false*/
var should = require('should'),
sinon = require('sinon'),
Promise = require('bluebird'),
rewire = require('rewire'),
config = require('../../server/config'),
versioning = require(config.get('paths').corePath + '/server/data/schema/versioning'),
migration = require(config.get('paths').corePath + '/server/data/migration'),
models = require(config.get('paths').corePath + '/server/models'),
permissions = require(config.get('paths').corePath + '/server/permissions'),
api = require(config.get('paths').corePath + '/server/api'),
apps = require(config.get('paths').corePath + '/server/apps'),
i18n = require(config.get('paths').corePath + '/server/i18n'),
xmlrpc = require(config.get('paths').corePath + '/server/data/xml/xmlrpc'),
slack = require(config.get('paths').corePath + '/server/data/slack'),
scheduling = require(config.get('paths').corePath + '/server/scheduling'),
bootstrap = rewire(config.get('paths').corePath + '/server'),
sandbox = sinon.sandbox.create();
describe('server bootstrap', function () {
var middlewareStub, resetMiddlewareStub, initDbHashAndFirstRunStub, resetInitDbHashAndFirstRunStub,
populateStub;
before(function () {
models.init();
});
beforeEach(function () {
middlewareStub = sandbox.stub();
initDbHashAndFirstRunStub = sandbox.stub();
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());
sandbox.stub(i18n, 'init');
sandbox.stub(apps, 'init').returns(Promise.resolve());
sandbox.stub(slack, 'listen').returns(Promise.resolve());
sandbox.stub(xmlrpc, 'listen').returns(Promise.resolve());
sandbox.stub(scheduling, 'init').returns(Promise.resolve());
resetMiddlewareStub = bootstrap.__set__('middleware', middlewareStub);
resetInitDbHashAndFirstRunStub = bootstrap.__set__('initDbHashAndFirstRun', initDbHashAndFirstRunStub);
});
afterEach(function () {
sandbox.restore();
resetMiddlewareStub();
resetInitDbHashAndFirstRunStub();
});
describe('migrations', function () {
it('database does not exist: expect database population', function (done) {
sandbox.stub(migration.update, 'isDatabaseOutOfDate').returns({migrate:false});
sandbox.stub(versioning, 'getDatabaseVersion', function () {
return Promise.reject();
});
bootstrap()
.then(function () {
migration.populate.calledOnce.should.eql(true);
migration.update.execute.calledOnce.should.eql(false);
models.Settings.populateDefaults.callCount.should.eql(1);
config.get('maintenance').enabled.should.eql(false);
done();
})
.catch(function (err) {
done(err);
});
});
// @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');
sandbox.stub(versioning, 'getDatabaseVersion', function () {
return Promise.resolve('006');
});
bootstrap()
.then(function () {
migration.update.isDatabaseOutOfDate.calledOnce.should.eql(true);
migration.update.execute.called.should.eql(false);
models.Settings.populateDefaults.callCount.should.eql(1);
migration.populate.calledOnce.should.eql(false);
done();
})
.catch(function (err) {
done(err);
});
});
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());
sandbox.stub(versioning, 'getDatabaseVersion', function () {
return Promise.resolve('006');
});
bootstrap()
.then(function () {
migration.update.isDatabaseOutOfDate.calledOnce.should.eql(true);
migration.update.execute.calledOnce.should.eql(true);
migration.update.execute.calledWith({
fromVersion: '006',
toVersion: '008',
forceMigration: undefined
}).should.eql(true);
models.Settings.populateDefaults.callCount.should.eql(1);
migration.populate.calledOnce.should.eql(false);
config.get('maintenance').enabled.should.eql(false);
done();
})
.catch(function (err) {
done(err);
});
});
});
});