2016-07-15 19:22:41 +03:00
|
|
|
var should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
|
|
|
rewire = require('rewire'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
crypto = require('crypto'),
|
|
|
|
fs = require('fs'),
|
2016-07-14 13:59:42 +03:00
|
|
|
errors = require('../../server/errors'),
|
|
|
|
models = require('../../server/models'),
|
|
|
|
exporter = require('../../server/data/export'),
|
|
|
|
schema = require('../../server/data/schema'),
|
|
|
|
migration = rewire('../../server/data/migration'),
|
|
|
|
fixtures = require('../../server/data/migration/fixtures'),
|
|
|
|
populate = require('../../server/data/migration/populate'),
|
|
|
|
update = rewire('../../server/data/migration/update'),
|
2016-02-25 10:10:36 +03:00
|
|
|
defaultSettings = schema.defaultSettings,
|
2016-07-14 13:59:42 +03:00
|
|
|
schemaTables = Object.keys(schema.tables),
|
2016-02-25 10:10:36 +03:00
|
|
|
sandbox = sinon.sandbox.create();
|
2014-12-20 13:16:29 +03:00
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
// Check version integrity
|
|
|
|
// These tests exist to ensure that developers are not able to modify the database schema, or permissions fixtures
|
|
|
|
// without knowing that they also need to update the default database version,
|
|
|
|
// both of which are required for migrations to work properly.
|
2016-09-20 17:52:43 +03:00
|
|
|
describe.skip('DB version integrity', function () {
|
2016-03-21 15:44:23 +03:00
|
|
|
// Only these variables should need updating
|
2016-09-20 16:44:07 +03:00
|
|
|
var currentDbVersion = 'alpha.1',
|
2016-09-14 18:02:35 +03:00
|
|
|
currentSchemaHash = 'b3bdae210526b2d4393359c3e45d7f83',
|
2016-08-23 15:07:25 +03:00
|
|
|
currentFixturesHash = '30b0a956b04e634e7f2cddcae8d2fd20';
|
2016-03-21 15:44:23 +03:00
|
|
|
|
|
|
|
// If this test is failing, then it is likely a change has been made that requires a DB version bump,
|
|
|
|
// and the values above will need updating as confirmation
|
|
|
|
it('should not change without fixing this test', function () {
|
|
|
|
var tablesNoValidation = _.cloneDeep(schema.tables),
|
|
|
|
schemaHash,
|
|
|
|
fixturesHash;
|
|
|
|
|
|
|
|
_.each(tablesNoValidation, function (table) {
|
|
|
|
return _.each(table, function (column, name) {
|
|
|
|
table[name] = _.omit(column, 'validations');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
schemaHash = crypto.createHash('md5').update(JSON.stringify(tablesNoValidation)).digest('hex');
|
|
|
|
fixturesHash = crypto.createHash('md5').update(JSON.stringify(fixtures.fixtures)).digest('hex');
|
|
|
|
|
|
|
|
// Test!
|
|
|
|
defaultSettings.core.databaseVersion.defaultValue.should.eql(currentDbVersion);
|
|
|
|
schemaHash.should.eql(currentSchemaHash);
|
|
|
|
fixturesHash.should.eql(currentFixturesHash);
|
|
|
|
schema.versioning.canMigrateFromVersion.should.eql('003');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-20 13:16:29 +03:00
|
|
|
describe('Migrations', function () {
|
2016-07-15 19:22:41 +03:00
|
|
|
var loggerStub, resetLogger;
|
2016-03-21 15:44:23 +03:00
|
|
|
|
2016-07-14 13:59:42 +03:00
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2016-02-25 10:10:36 +03:00
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
2016-07-15 19:22:41 +03:00
|
|
|
resetLogger();
|
2016-02-25 10:10:36 +03:00
|
|
|
});
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
loggerStub = {
|
|
|
|
info: sandbox.stub(),
|
|
|
|
warn: sandbox.stub()
|
|
|
|
};
|
2016-07-15 19:22:41 +03:00
|
|
|
|
|
|
|
resetLogger = update.__set__('logger', loggerStub);
|
2014-12-20 13:16:29 +03:00
|
|
|
});
|
2016-02-25 10:10:36 +03:00
|
|
|
|
2016-03-13 23:49:30 +03:00
|
|
|
describe('Backup', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
var exportStub, filenameStub, fsStub;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
exportStub = sandbox.stub(exporter, 'doExport').returns(new Promise.resolve());
|
|
|
|
filenameStub = sandbox.stub(exporter, 'fileName').returns(new Promise.resolve('test'));
|
|
|
|
fsStub = sandbox.stub(fs, 'writeFile').yields();
|
|
|
|
});
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-03-23 18:02:40 +03:00
|
|
|
it('should create a backup JSON file', function (done) {
|
2016-03-21 15:44:23 +03:00
|
|
|
migration.backupDatabase(loggerStub).then(function () {
|
2016-03-13 23:49:30 +03:00
|
|
|
exportStub.calledOnce.should.be.true();
|
|
|
|
filenameStub.calledOnce.should.be.true();
|
|
|
|
fsStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledTwice.should.be.true();
|
2016-03-13 23:49:30 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fall back to console.log if no logger provided', function (done) {
|
2016-03-23 18:02:40 +03:00
|
|
|
var noopStub = sandbox.stub(_, 'noop');
|
2016-03-13 23:49:30 +03:00
|
|
|
|
|
|
|
migration.backupDatabase().then(function () {
|
|
|
|
exportStub.calledOnce.should.be.true();
|
|
|
|
filenameStub.calledOnce.should.be.true();
|
|
|
|
fsStub.calledOnce.should.be.true();
|
|
|
|
noopStub.calledTwice.should.be.true();
|
|
|
|
// restore early so we get the test output
|
|
|
|
noopStub.restore();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Reset', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
var deleteStub;
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-03-23 18:02:40 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
deleteStub = sandbox.stub(schema.commands, 'deleteTable').returns(new Promise.resolve());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete all tables in reverse order', function (done) {
|
2016-03-13 23:49:30 +03:00
|
|
|
migration.reset().then(function (result) {
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.an.Array().with.lengthOf(schemaTables.length);
|
|
|
|
|
|
|
|
deleteStub.called.should.be.true();
|
|
|
|
deleteStub.callCount.should.be.eql(schemaTables.length);
|
|
|
|
// First call should be called with the last table
|
|
|
|
deleteStub.firstCall.calledWith(schemaTables[schemaTables.length - 1]).should.be.true();
|
|
|
|
// Last call should be called with the first table
|
|
|
|
deleteStub.lastCall.calledWith(schemaTables[0]).should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete all tables in reverse order when called twice in a row', function (done) {
|
|
|
|
migration.reset().then(function (result) {
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.an.Array().with.lengthOf(schemaTables.length);
|
|
|
|
|
|
|
|
deleteStub.called.should.be.true();
|
|
|
|
deleteStub.callCount.should.be.eql(schemaTables.length);
|
|
|
|
// First call should be called with the last table
|
|
|
|
deleteStub.firstCall.calledWith(schemaTables[schemaTables.length - 1]).should.be.true();
|
|
|
|
// Last call should be called with the first table
|
|
|
|
deleteStub.lastCall.calledWith(schemaTables[0]).should.be.true();
|
|
|
|
|
|
|
|
return migration.reset();
|
|
|
|
}).then(function (result) {
|
|
|
|
should.exist(result);
|
|
|
|
result.should.be.an.Array().with.lengthOf(schemaTables.length);
|
|
|
|
|
|
|
|
deleteStub.called.should.be.true();
|
|
|
|
deleteStub.callCount.should.be.eql(schemaTables.length * 2);
|
|
|
|
// First call (second set) should be called with the last table
|
|
|
|
deleteStub.getCall(schemaTables.length).calledWith(schemaTables[schemaTables.length - 1]).should.be.true();
|
|
|
|
// Last call (second Set) should be called with the first table
|
|
|
|
deleteStub.getCall(schemaTables.length * 2 - 1).calledWith(schemaTables[0]).should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Populate', function () {
|
2016-10-06 16:50:55 +03:00
|
|
|
var createStub, fixturesStub, setDatabaseVersionStub, populateDefaultsStub;
|
2016-03-23 18:02:40 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
fixturesStub = sandbox.stub(fixtures, 'populate').returns(new Promise.resolve());
|
|
|
|
});
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-03-23 18:02:40 +03:00
|
|
|
it('should create all tables, and populate fixtures', function (done) {
|
2016-09-30 16:05:17 +03:00
|
|
|
createStub = sandbox.stub(schema.commands, 'createTable').returns(new Promise.resolve());
|
2016-10-06 16:50:55 +03:00
|
|
|
setDatabaseVersionStub = sandbox.stub(schema.versioning, 'setDatabaseVersion').returns(new Promise.resolve());
|
|
|
|
populateDefaultsStub = sandbox.stub(models.Settings, 'populateDefaults').returns(new Promise.resolve());
|
2016-09-30 16:05:17 +03:00
|
|
|
|
2016-07-15 19:22:41 +03:00
|
|
|
populate().then(function (result) {
|
2016-03-13 23:49:30 +03:00
|
|
|
should.not.exist(result);
|
|
|
|
|
2016-10-06 16:50:55 +03:00
|
|
|
populateDefaultsStub.called.should.be.true();
|
|
|
|
setDatabaseVersionStub.called.should.be.true();
|
2016-03-13 23:49:30 +03:00
|
|
|
createStub.called.should.be.true();
|
|
|
|
createStub.callCount.should.be.eql(schemaTables.length);
|
|
|
|
createStub.firstCall.calledWith(schemaTables[0]).should.be.true();
|
|
|
|
createStub.lastCall.calledWith(schemaTables[schemaTables.length - 1]).should.be.true();
|
|
|
|
fixturesStub.calledOnce.should.be.true();
|
|
|
|
done();
|
2016-03-14 20:39:00 +03:00
|
|
|
}).catch(done);
|
2016-03-13 23:49:30 +03:00
|
|
|
});
|
|
|
|
|
2016-09-30 16:05:17 +03:00
|
|
|
it('should rollback if error occurs', function (done) {
|
|
|
|
var i = 0;
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-09-30 16:05:17 +03:00
|
|
|
createStub = sandbox.stub(schema.commands, 'createTable', function () {
|
|
|
|
i = i + 1;
|
|
|
|
|
|
|
|
if (i > 10) {
|
|
|
|
return new Promise.reject(new Error('error on table creation :('));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
populate()
|
|
|
|
.then(function () {
|
|
|
|
done(new Error('should throw an error for database population'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
should.exist(err);
|
2016-10-06 15:27:35 +03:00
|
|
|
(err instanceof errors.GhostError).should.eql(true);
|
2016-09-30 16:05:17 +03:00
|
|
|
createStub.callCount.should.eql(11);
|
|
|
|
done();
|
|
|
|
});
|
2016-03-13 23:49:30 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-07-26 11:56:07 +03:00
|
|
|
describe('isDatabaseOutOfDate', function () {
|
|
|
|
var updateDatabaseSchemaStub, updateDatabaseSchemaReset, versionsSpy;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
versionsSpy = sandbox.spy(schema.versioning, 'getMigrationVersions');
|
|
|
|
|
|
|
|
// For these tests, stub out the actual update task
|
|
|
|
updateDatabaseSchemaStub = sandbox.stub().returns(new Promise.resolve());
|
|
|
|
updateDatabaseSchemaReset = update.__set__('updateDatabaseSchema', updateDatabaseSchemaStub);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
updateDatabaseSchemaReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw error if versions are too old', function () {
|
2016-10-06 16:50:55 +03:00
|
|
|
var response = update.isDatabaseOutOfDate({fromVersion: '0.8', toVersion: '1.0'});
|
2016-07-26 11:56:07 +03:00
|
|
|
updateDatabaseSchemaStub.calledOnce.should.be.false();
|
2016-10-06 15:27:35 +03:00
|
|
|
(response.error instanceof errors.DatabaseVersionError).should.eql(true);
|
2016-07-26 11:56:07 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should just return if versions are the same', function () {
|
|
|
|
var migrateToDatabaseVersionStub = sandbox.stub().returns(new Promise.resolve()),
|
|
|
|
migrateToDatabaseVersionReset = update.__set__('migrateToDatabaseVersion', migrateToDatabaseVersionStub),
|
2016-10-06 16:50:55 +03:00
|
|
|
response = update.isDatabaseOutOfDate({fromVersion: '1.0', toVersion: '1.0'});
|
2016-07-26 11:56:07 +03:00
|
|
|
|
|
|
|
response.migrate.should.eql(false);
|
|
|
|
versionsSpy.calledOnce.should.be.false();
|
|
|
|
migrateToDatabaseVersionStub.callCount.should.eql(0);
|
|
|
|
migrateToDatabaseVersionReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if the database version is higher than the default', function () {
|
2016-10-06 16:50:55 +03:00
|
|
|
var response = update.isDatabaseOutOfDate({fromVersion: '1.3', toVersion: '1.2'});
|
2016-07-26 11:56:07 +03:00
|
|
|
updateDatabaseSchemaStub.calledOnce.should.be.false();
|
2016-10-06 15:27:35 +03:00
|
|
|
(response.error instanceof errors.DatabaseVersionError).should.eql(true);
|
2016-07-26 11:56:07 +03:00
|
|
|
});
|
|
|
|
});
|
2014-12-20 13:16:29 +03:00
|
|
|
});
|