2016-03-14 20:39:00 +03:00
|
|
|
/*globals describe, it, afterEach, beforeEach*/
|
2014-12-20 13:16:29 +03:00
|
|
|
var should = require('should'),
|
2016-02-25 10:10:36 +03:00
|
|
|
sinon = require('sinon'),
|
2016-03-14 20:39:00 +03:00
|
|
|
rewire = require('rewire'),
|
2014-12-20 13:16:29 +03:00
|
|
|
_ = require('lodash'),
|
2016-03-13 23:49:30 +03:00
|
|
|
Promise = require('bluebird'),
|
2014-12-20 13:16:29 +03:00
|
|
|
crypto = require('crypto'),
|
2016-03-13 23:49:30 +03:00
|
|
|
fs = require('fs'),
|
2014-12-20 13:16:29 +03:00
|
|
|
|
|
|
|
// Stuff we are testing
|
2016-03-14 20:39:00 +03:00
|
|
|
db = require('../../server/data/db'),
|
|
|
|
errors = require('../../server/errors/'),
|
2016-03-13 23:49:30 +03:00
|
|
|
exporter = require('../../server/data/export'),
|
2016-03-14 20:39:00 +03:00
|
|
|
schema = require('../../server/data/schema'),
|
|
|
|
|
|
|
|
migration = rewire('../../server/data/migration'),
|
2016-02-25 10:10:36 +03:00
|
|
|
fixtures = require('../../server/data/migration/fixtures'),
|
2016-03-13 23:49:30 +03:00
|
|
|
populate = require('../../server/data/migration/populate'),
|
2016-03-14 20:39:00 +03:00
|
|
|
update = rewire('../../server/data/migration/update'),
|
|
|
|
updates004 = require('../../server/data/migration/004'),
|
2016-03-22 21:02:00 +03:00
|
|
|
updates005 = require('../../server/data/migration/005'),
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-02-25 10:10:36 +03:00
|
|
|
defaultSettings = schema.defaultSettings,
|
2016-03-13 23:49:30 +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.
|
|
|
|
describe('DB version integrity', function () {
|
|
|
|
// Only these variables should need updating
|
2016-03-22 21:02:00 +03:00
|
|
|
var currentDbVersion = '005',
|
2016-03-26 17:28:52 +03:00
|
|
|
currentSchemaHash = 'be706cdbeb06103d90703ee733efc556',
|
2016-04-08 12:09:26 +03:00
|
|
|
currentFixturesHash = 'ba195b645386b019a69c4b79e6854138';
|
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-03-21 15:44:23 +03:00
|
|
|
var loggerStub;
|
|
|
|
|
2016-02-25 10:10:36 +03:00
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
loggerStub = {
|
|
|
|
info: sandbox.stub(),
|
|
|
|
warn: sandbox.stub()
|
|
|
|
};
|
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-03-23 18:02:40 +03:00
|
|
|
var createStub, fixturesStub, settingsStub;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
createStub = sandbox.stub(schema.commands, 'createTable').returns(new Promise.resolve());
|
|
|
|
fixturesStub = sandbox.stub(fixtures, 'populate').returns(new Promise.resolve());
|
|
|
|
settingsStub = sandbox.stub(fixtures, 'ensureDefaultSettings').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-03-21 15:44:23 +03:00
|
|
|
populate(loggerStub).then(function (result) {
|
2016-03-13 23:49:30 +03:00
|
|
|
should.not.exist(result);
|
|
|
|
|
|
|
|
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();
|
|
|
|
settingsStub.calledOnce.should.be.true();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
|
|
|
|
2016-03-13 23:49:30 +03:00
|
|
|
done();
|
2016-03-14 20:39:00 +03:00
|
|
|
}).catch(done);
|
2016-03-13 23:49:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should should only create tables, with tablesOnly setting', function (done) {
|
2016-03-21 15:44:23 +03:00
|
|
|
populate(loggerStub, true).then(function (result) {
|
2016-03-13 23:49:30 +03:00
|
|
|
should.exist(result);
|
|
|
|
result.should.be.an.Array().with.lengthOf(schemaTables.length);
|
|
|
|
|
|
|
|
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.called.should.be.false();
|
|
|
|
settingsStub.called.should.be.false();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
|
|
|
|
2016-03-13 23:49:30 +03:00
|
|
|
done();
|
2016-03-14 20:39:00 +03:00
|
|
|
}).catch(done);
|
2016-03-13 23:49:30 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Update', function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
describe('Update function', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
var reset, backupStub, settingsStub, fixturesStub, setDbStub, errorStub, versionsSpy, tasksSpy;
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
// Stubs
|
|
|
|
backupStub = sandbox.stub().returns(new Promise.resolve());
|
|
|
|
settingsStub = sandbox.stub(fixtures, 'ensureDefaultSettings').returns(new Promise.resolve());
|
|
|
|
fixturesStub = sandbox.stub(fixtures, 'update').returns(new Promise.resolve());
|
|
|
|
setDbStub = sandbox.stub(schema.versioning, 'setDatabaseVersion').returns(new Promise.resolve());
|
|
|
|
errorStub = sandbox.stub(schema.versioning, 'showCannotMigrateError').returns(new Promise.resolve());
|
|
|
|
// Spys
|
|
|
|
versionsSpy = sandbox.spy(schema.versioning, 'getMigrationVersions');
|
2016-03-23 18:02:40 +03:00
|
|
|
tasksSpy = sandbox.spy(schema.versioning, 'getUpdateDatabaseTasks');
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
// Internal overrides
|
|
|
|
reset = update.__set__('backup', backupStub);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Pre & post update process', function () {
|
|
|
|
var updateStub, updateReset;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
// For these tests, stub out the actual update task
|
|
|
|
updateStub = sandbox.stub().returns(new Promise.resolve());
|
|
|
|
updateReset = update.__set__('updateDatabaseSchema', updateStub);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
updateReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should attempt to run the pre & post update tasks correctly', function (done) {
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
update('100', '102', loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
// Before the update, it does some tasks...
|
|
|
|
// It should not show an error for these versions
|
|
|
|
errorStub.called.should.be.false();
|
|
|
|
// getMigrationVersions should be called with the correct versions
|
|
|
|
versionsSpy.calledOnce.should.be.true();
|
|
|
|
versionsSpy.calledWith('100', '102').should.be.true();
|
|
|
|
// It should attempt to do a backup
|
|
|
|
backupStub.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
// Now it's going to try to actually do the update...
|
|
|
|
updateStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
updateStub.calledWith(['101', '102'], loggerStub).should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
// And now there are some final tasks to wrap up...
|
|
|
|
// First, the ensure default settings task
|
|
|
|
settingsStub.calledOnce.should.be.true();
|
|
|
|
// Then fixture updates
|
|
|
|
fixturesStub.calledOnce.should.be.true();
|
|
|
|
// And finally, set the new DB version
|
|
|
|
setDbStub.calledOnce.should.be.true();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
// Because we stubbed everything, loggerStub didn't get called
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
// Just to be sure, lets assert the call order
|
|
|
|
sinon.assert.callOrder(
|
|
|
|
versionsSpy, backupStub, updateStub, settingsStub, fixturesStub, setDbStub
|
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw error if versions are too old', function (done) {
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
update('000', '002', loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
// It should show an error for these versions
|
|
|
|
errorStub.called.should.be.true();
|
|
|
|
|
|
|
|
// And so should not do the update...
|
|
|
|
updateStub.calledOnce.should.be.false();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
// Because we stubbed everything, loggerStub didn't get called
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should upgrade from minimum version, if FORCE_MIGRATION is set', function (done) {
|
|
|
|
// Setup
|
|
|
|
process.env.FORCE_MIGRATION = true;
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
update('005', '006', loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
// It should not show an error for these versions
|
|
|
|
errorStub.called.should.be.false();
|
|
|
|
|
|
|
|
// getMigrationVersions should be called with the correct versions
|
|
|
|
versionsSpy.calledOnce.should.be.true();
|
|
|
|
versionsSpy.calledWith('003', '006').should.be.true();
|
|
|
|
versionsSpy.returned(['003', '004', '005', '006']).should.be.true();
|
|
|
|
|
|
|
|
// It should try to do the update
|
|
|
|
updateStub.calledOnce.should.be.true();
|
|
|
|
updateStub.calledWith(['004', '005', '006']).should.be.true();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
// Because we stubbed everything, loggerStub didn't get called
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
// Restore
|
|
|
|
delete process.env.FORCE_MIGRATION;
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Update to 004', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
it('should call all the 004 database upgrade tasks', function (done) {
|
2016-03-14 20:39:00 +03:00
|
|
|
// Setup
|
2016-03-23 18:02:40 +03:00
|
|
|
// Create a new stub, this will replace sequence, so that db calls don't actually get run
|
|
|
|
var sequenceStub = sandbox.stub(),
|
|
|
|
sequenceReset = update.__set__('sequence', sequenceStub);
|
|
|
|
|
|
|
|
// The first time we call sequence, it should be to execute a top level version, e.g 004
|
|
|
|
// yieldsTo('0') means this stub will execute the function at index 0 of the array passed as the
|
|
|
|
// first argument. In short the `runVersionTasks` function gets executed, and sequence gets called
|
|
|
|
// again with the array of tasks to execute for 004, which is what we want to check
|
2016-03-22 21:02:00 +03:00
|
|
|
sequenceStub.onFirstCall().yieldsTo('0').returns(Promise.resolve([]));
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
update('003', '004', loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
errorStub.called.should.be.false();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledTwice.should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
versionsSpy.calledOnce.should.be.true();
|
|
|
|
versionsSpy.calledWith('003', '004').should.be.true();
|
|
|
|
versionsSpy.returned(['003', '004']).should.be.true();
|
|
|
|
|
2016-03-23 18:02:40 +03:00
|
|
|
tasksSpy.calledOnce.should.be.true();
|
|
|
|
tasksSpy.calledWith('004', loggerStub).should.be.true();
|
|
|
|
tasksSpy.firstCall.returnValue.should.be.an.Array().with.lengthOf(5);
|
|
|
|
|
|
|
|
sequenceStub.calledTwice.should.be.true();
|
|
|
|
|
2016-03-22 21:02:00 +03:00
|
|
|
sequenceStub.firstCall.calledWith(sinon.match.array, loggerStub).should.be.true();
|
2016-03-23 18:02:40 +03:00
|
|
|
sequenceStub.firstCall.args[0].should.be.an.Array().with.lengthOf(1);
|
|
|
|
sequenceStub.firstCall.args[0][0].should.be.a.Function().with.property('name', 'runVersionTasks');
|
|
|
|
|
2016-03-22 21:02:00 +03:00
|
|
|
sequenceStub.secondCall.calledWith(sinon.match.array, loggerStub).should.be.true();
|
|
|
|
sequenceStub.secondCall.args[0].should.be.an.Array().with.lengthOf(5);
|
2016-03-23 18:02:40 +03:00
|
|
|
sequenceStub.secondCall.args[0][0].should.be.a.Function().with.property('name', 'addTourColumnToUsers');
|
|
|
|
sequenceStub.secondCall.args[0][1].should.be.a.Function().with.property('name', 'addSortOrderColumnToPostsTags');
|
|
|
|
sequenceStub.secondCall.args[0][2].should.be.a.Function().with.property('name', 'addManyColumnsToClients');
|
|
|
|
sequenceStub.secondCall.args[0][3].should.be.a.Function().with.property('name', 'addClientTrustedDomainsTable');
|
|
|
|
sequenceStub.secondCall.args[0][4].should.be.a.Function().with.property('name', 'dropUniqueOnClientsSecret');
|
|
|
|
|
|
|
|
// Reset sequence
|
|
|
|
sequenceReset();
|
2016-03-14 20:39:00 +03:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Tasks:', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
var addColumnStub, createTableStub, getIndexesStub, dropUniqueStub,
|
|
|
|
knexStub, knexMock;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
knexMock = sandbox.stub().returns({});
|
|
|
|
knexMock.schema = {
|
|
|
|
hasTable: sandbox.stub(),
|
|
|
|
hasColumn: sandbox.stub()
|
|
|
|
};
|
|
|
|
// this MUST use sinon, not sandbox, see sinonjs/sinon#781
|
|
|
|
knexStub = sinon.stub(db, 'knex', {get: function () { return knexMock; }});
|
|
|
|
|
|
|
|
addColumnStub = sandbox.stub(schema.commands, 'addColumn');
|
|
|
|
createTableStub = sandbox.stub(schema.commands, 'createTable');
|
|
|
|
getIndexesStub = sandbox.stub(schema.commands, 'getIndexes');
|
|
|
|
dropUniqueStub = sandbox.stub(schema.commands, 'dropUnique');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
knexStub.restore();
|
|
|
|
});
|
|
|
|
|
2016-03-22 21:02:00 +03:00
|
|
|
it('should have tasks for 004', function () {
|
|
|
|
should.exist(updates004);
|
|
|
|
updates004.should.be.an.Array().with.lengthOf(5);
|
|
|
|
});
|
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
describe('01-add-tour-column-to-users', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
it('does not try to add a new column if the table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(new Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates004[0](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.called.should.be.false();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
it('does not try to add a new column if the column already exists', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'tour').returns(new Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[0](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'tour').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('tries to add a new column if table is present but column is not', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'tour').returns(new Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[0](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'tour').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.calledOnce.should.be.true();
|
|
|
|
addColumnStub.calledWith('users', 'tour').should.be.true();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('02-add-sortorder-column-to-poststags', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
it('does not try to add a new column if the table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts_tags').returns(new Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates004[1](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts_tags').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.called.should.be.false();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
it('does not try to add a new column if the column already exists', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts_tags').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('posts_tags', 'sort_order').returns(new Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[1](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts_tags').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('posts_tags', 'sort_order').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('tries to add a new column if table is present but column is not', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts_tags').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('posts_tags', 'sort_order').returns(new Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[1](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts_tags').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('posts_tags', 'sort_order').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.calledOnce.should.be.true();
|
|
|
|
addColumnStub.calledWith('posts_tags', 'sort_order').should.be.true();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('03-add-many-columns-to-clients', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
it('does not try to add new columns if the table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('clients').returns(new Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates004[2](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('clients').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.called.should.be.false();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
it('does not try to add new columns if the columns already exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('clients').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'redirection_uri').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'logo').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'status').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'type').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'description').returns(new Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[2](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('clients').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.callCount.should.eql(5);
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'redirection_uri').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'logo').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'status').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'type').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'description').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.callCount.should.eql(5);
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('tries to add new columns if table is present but columns are not', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('clients').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'redirection_uri').returns(new Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'logo').returns(new Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'status').returns(new Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'type').returns(new Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'description').returns(new Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[2](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('clients').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.callCount.should.eql(5);
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'redirection_uri').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'logo').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'status').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'type').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'description').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.callCount.should.eql(5);
|
|
|
|
addColumnStub.calledWith('clients', 'redirection_uri').should.be.true();
|
|
|
|
addColumnStub.calledWith('clients', 'logo').should.be.true();
|
|
|
|
addColumnStub.calledWith('clients', 'status').should.be.true();
|
|
|
|
addColumnStub.calledWith('clients', 'type').should.be.true();
|
|
|
|
addColumnStub.calledWith('clients', 'description').should.be.true();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.callCount.should.eql(5);
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('will only try to add columns that do not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('clients').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'redirection_uri').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'logo').returns(new Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'status').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'type').returns(new Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('clients', 'description').returns(new Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[2](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('clients').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.callCount.should.eql(5);
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'redirection_uri').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'logo').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'status').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'type').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('clients', 'description').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.callCount.should.eql(2);
|
|
|
|
addColumnStub.calledWith('clients', 'logo').should.be.true();
|
|
|
|
addColumnStub.calledWith('clients', 'type').should.be.true();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.callCount.should.eql(2);
|
|
|
|
loggerStub.warn.callCount.should.eql(3);
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('04-add-clienttrusteddomains-table', function () {
|
|
|
|
it('does not try to add a new table if the table already exists', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('client_trusted_domains').returns(new Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[3](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('client_trusted_domains').should.be.true();
|
|
|
|
|
|
|
|
createTableStub.called.should.be.false();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('tries to add a new table if the table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('client_trusted_domains').returns(new Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[3](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('client_trusted_domains').should.be.true();
|
|
|
|
|
|
|
|
createTableStub.calledOnce.should.be.true();
|
|
|
|
createTableStub.calledWith('client_trusted_domains').should.be.true();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('05-drop-unique-on-clients-secret', function () {
|
|
|
|
it('does not try to drop unique if the table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
getIndexesStub.withArgs('clients').returns(new Promise.resolve(
|
|
|
|
['clients_slug_unique', 'clients_name_unique', 'clients_secret_unique'])
|
|
|
|
);
|
|
|
|
knexMock.schema.hasTable.withArgs('clients').returns(new Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[4](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('clients').should.be.true();
|
|
|
|
|
|
|
|
getIndexesStub.called.should.be.false();
|
|
|
|
|
|
|
|
dropUniqueStub.called.should.be.false();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not try to drop unique if the index does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('clients').returns(new Promise.resolve(true));
|
|
|
|
|
|
|
|
getIndexesStub.withArgs('clients').returns(new Promise.resolve(
|
|
|
|
['clients_slug_unique', 'clients_name_unique'])
|
|
|
|
);
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[4](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('clients').should.be.true();
|
|
|
|
|
|
|
|
getIndexesStub.calledOnce.should.be.true();
|
|
|
|
getIndexesStub.calledWith('clients').should.be.true();
|
|
|
|
|
|
|
|
dropUniqueStub.called.should.be.false();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('tries to add a drop unique if table and index both exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('clients').returns(new Promise.resolve(true));
|
|
|
|
|
|
|
|
getIndexesStub.withArgs('clients').returns(new Promise.resolve(
|
|
|
|
['clients_slug_unique', 'clients_name_unique', 'clients_secret_unique'])
|
|
|
|
);
|
|
|
|
|
|
|
|
// Execute
|
2016-03-21 15:44:23 +03:00
|
|
|
updates004[4](loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('clients').should.be.true();
|
|
|
|
|
|
|
|
getIndexesStub.calledOnce.should.be.true();
|
|
|
|
getIndexesStub.calledWith('clients').should.be.true();
|
|
|
|
|
|
|
|
dropUniqueStub.calledOnce.should.be.true();
|
|
|
|
dropUniqueStub.calledWith('clients', 'secret').should.be.true();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
done();
|
2016-03-21 15:44:23 +03:00
|
|
|
}).catch(done);
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-03-22 21:02:00 +03:00
|
|
|
|
|
|
|
describe('Update to 005', function () {
|
|
|
|
it('should call all the 005 database upgrade tasks', function (done) {
|
|
|
|
// Setup
|
|
|
|
// Create a new stub, this will replace sequence, so that db calls don't actually get run
|
|
|
|
var sequenceStub = sandbox.stub(),
|
|
|
|
sequenceReset = update.__set__('sequence', sequenceStub);
|
|
|
|
|
|
|
|
// The first time we call sequence, it should be to execute a top level version, e.g 005
|
|
|
|
// yieldsTo('0') means this stub will execute the function at index 0 of the array passed as the
|
|
|
|
// first argument. In short the `runVersionTasks` function gets executed, and sequence gets called
|
|
|
|
// again with the array of tasks to execute for 005, which is what we want to check
|
2016-03-25 13:57:50 +03:00
|
|
|
sequenceStub.onFirstCall().yieldsTo('0').returns(Promise.resolve([]));
|
2016-03-22 21:02:00 +03:00
|
|
|
|
|
|
|
// Execute
|
|
|
|
update('004', '005', loggerStub).then(function () {
|
|
|
|
errorStub.called.should.be.false();
|
2016-03-25 13:57:50 +03:00
|
|
|
loggerStub.info.calledTwice.should.be.true();
|
2016-03-22 21:02:00 +03:00
|
|
|
|
|
|
|
versionsSpy.calledOnce.should.be.true();
|
|
|
|
versionsSpy.calledWith('004', '005').should.be.true();
|
|
|
|
versionsSpy.returned(['004', '005']).should.be.true();
|
|
|
|
|
|
|
|
tasksSpy.calledOnce.should.be.true();
|
|
|
|
tasksSpy.calledWith('005', loggerStub).should.be.true();
|
2016-03-26 17:28:52 +03:00
|
|
|
tasksSpy.firstCall.returnValue.should.be.an.Array().with.lengthOf(4);
|
2016-03-22 21:02:00 +03:00
|
|
|
|
2016-03-25 13:57:50 +03:00
|
|
|
sequenceStub.calledTwice.should.be.true();
|
2016-03-22 21:02:00 +03:00
|
|
|
|
|
|
|
sequenceStub.firstCall.calledWith(sinon.match.array, loggerStub).should.be.true();
|
2016-03-25 13:57:50 +03:00
|
|
|
sequenceStub.firstCall.args[0].should.be.an.Array().with.lengthOf(1);
|
|
|
|
sequenceStub.firstCall.args[0][0].should.be.a.Function().with.property('name', 'runVersionTasks');
|
|
|
|
|
|
|
|
sequenceStub.secondCall.calledWith(sinon.match.array, loggerStub).should.be.true();
|
2016-03-26 17:28:52 +03:00
|
|
|
sequenceStub.secondCall.args[0].should.be.an.Array().with.lengthOf(4);
|
2016-03-25 13:57:50 +03:00
|
|
|
sequenceStub.secondCall.args[0][0].should.be.a.Function().with.property('name', 'dropHiddenColumnFromTags');
|
2016-03-25 23:45:40 +03:00
|
|
|
sequenceStub.secondCall.args[0][1].should.be.a.Function().with.property('name', 'addVisibilityColumnToKeyTables');
|
2016-03-26 17:07:59 +03:00
|
|
|
sequenceStub.secondCall.args[0][2].should.be.a.Function().with.property('name', 'addMobiledocColumnToPosts');
|
2016-03-26 17:28:52 +03:00
|
|
|
sequenceStub.secondCall.args[0][3].should.be.a.Function().with.property('name', 'addSocialMediaColumnsToUsers');
|
2016-03-22 21:02:00 +03:00
|
|
|
|
|
|
|
// Reset sequence
|
|
|
|
sequenceReset();
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Tasks:', function () {
|
2016-03-25 23:45:40 +03:00
|
|
|
var dropColumnStub, addColumnStub,
|
2016-03-25 13:57:50 +03:00
|
|
|
knexStub, knexMock;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
knexMock = sandbox.stub().returns({});
|
|
|
|
knexMock.schema = {
|
|
|
|
hasTable: sandbox.stub(),
|
|
|
|
hasColumn: sandbox.stub()
|
|
|
|
};
|
|
|
|
// this MUST use sinon, not sandbox, see sinonjs/sinon#781
|
|
|
|
knexStub = sinon.stub(db, 'knex', {get: function () { return knexMock; }});
|
|
|
|
|
|
|
|
dropColumnStub = sandbox.stub(schema.commands, 'dropColumn');
|
2016-03-25 23:45:40 +03:00
|
|
|
addColumnStub = sandbox.stub(schema.commands, 'addColumn');
|
2016-03-25 13:57:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
knexStub.restore();
|
|
|
|
});
|
|
|
|
|
2016-03-22 21:02:00 +03:00
|
|
|
it('should have tasks for 005', function () {
|
|
|
|
should.exist(updates005);
|
2016-03-26 17:28:52 +03:00
|
|
|
updates005.should.be.an.Array().with.lengthOf(4);
|
2016-03-25 13:57:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('01-drop-hidden-column-from-tags', function () {
|
|
|
|
it('does not try to drop column if the table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[0](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('tags').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.called.should.be.false();
|
|
|
|
|
|
|
|
dropColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not try to drop column if the column does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('tags', 'hidden').returns(Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[0](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('tags').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('tags', 'hidden').should.be.true();
|
|
|
|
|
|
|
|
dropColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('tries to add drop column if table and column are both present', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('tags', 'hidden').returns(Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[0](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('tags').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('tags', 'hidden').should.be.true();
|
|
|
|
|
|
|
|
dropColumnStub.calledOnce.should.be.true();
|
|
|
|
dropColumnStub.calledWith('tags', 'hidden').should.be.true();
|
|
|
|
|
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2016-03-22 21:02:00 +03:00
|
|
|
});
|
2016-03-25 23:45:40 +03:00
|
|
|
|
|
|
|
describe('02-add-visibility-column-to-key-tables', function () {
|
|
|
|
it('does not try to add new column if the table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(false));
|
|
|
|
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(false));
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[1](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledThrice.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts').should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('tags').should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.called.should.be.false();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledThrice.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not try to add new columns if the columns already exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(true));
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.withArgs('posts', 'visibility').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('tags', 'visibility').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'visibility').returns(Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[1](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledThrice.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts').should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('tags').should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledThrice.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('posts', 'visibility').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('tags', 'visibility').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'visibility').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledThrice.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('tries to add new columns if table is present but columns are not', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(true));
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.withArgs('posts', 'visibility').returns(Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('tags', 'visibility').returns(Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'visibility').returns(Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[1](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledThrice.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts').should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('tags').should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledThrice.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('posts', 'visibility').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('tags', 'visibility').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'visibility').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.calledThrice.should.be.true();
|
|
|
|
addColumnStub.calledWith('posts', 'visibility').should.be.true();
|
|
|
|
addColumnStub.calledWith('tags', 'visibility').should.be.true();
|
|
|
|
addColumnStub.calledWith('users', 'visibility').should.be.true();
|
|
|
|
|
|
|
|
loggerStub.info.calledThrice.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will only try to add columns that do not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasTable.withArgs('tags').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(true));
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.withArgs('posts', 'visibility').returns(Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('tags', 'visibility').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'visibility').returns(Promise.resolve(false));
|
|
|
|
// Execute
|
|
|
|
updates005[1](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledThrice.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts').should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('tags').should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledThrice.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('posts', 'visibility').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('tags', 'visibility').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'visibility').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.calledTwice.should.be.true();
|
|
|
|
addColumnStub.calledWith('posts', 'visibility').should.be.true();
|
|
|
|
addColumnStub.calledWith('tags', 'visibility').should.be.false();
|
|
|
|
addColumnStub.calledWith('users', 'visibility').should.be.true();
|
|
|
|
|
|
|
|
loggerStub.info.calledTwice.should.be.true();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
2016-03-26 17:07:59 +03:00
|
|
|
|
|
|
|
describe('03-add-mobiledoc-column-to-posts', function () {
|
|
|
|
it('does not try to add a new column if the table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[2](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.called.should.be.false();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not try to add a new column if the column already exists', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts').returns(new Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('posts', 'mobiledoc').returns(Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[2](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('posts', 'mobiledoc').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('tries to add a new column if table is present but column is not', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('posts').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('posts', 'mobiledoc').returns(Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[2](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('posts').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('posts', 'mobiledoc').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.calledOnce.should.be.true();
|
|
|
|
addColumnStub.calledWith('posts', 'mobiledoc').should.be.true();
|
|
|
|
|
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
2016-03-26 17:28:52 +03:00
|
|
|
|
|
|
|
describe('04-add-social-media-columns-to-users', function () {
|
|
|
|
it('does not try to add new columns if the table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[3](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.called.should.be.false();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not try to add new columns if the columns already exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'facebook').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'twitter').returns(Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[3](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledTwice.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'facebook').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'twitter').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.called.should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.calledTwice.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('tries to add new columns if table is present but columns are not', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'facebook').returns(Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'twitter').returns(Promise.resolve(false));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[3](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledTwice.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'facebook').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'twitter').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.calledTwice.should.be.true();
|
|
|
|
addColumnStub.calledWith('users', 'facebook').should.be.true();
|
|
|
|
addColumnStub.calledWith('users', 'twitter').should.be.true();
|
|
|
|
|
|
|
|
loggerStub.info.calledTwice.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will only try to add columns that do not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
knexMock.schema.hasTable.withArgs('users').returns(Promise.resolve(true));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'facebook').returns(Promise.resolve(false));
|
|
|
|
knexMock.schema.hasColumn.withArgs('users', 'twitter').returns(Promise.resolve(true));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
updates005[3](loggerStub).then(function () {
|
|
|
|
knexMock.schema.hasTable.calledOnce.should.be.true();
|
|
|
|
knexMock.schema.hasTable.calledWith('users').should.be.true();
|
|
|
|
|
|
|
|
knexMock.schema.hasColumn.calledTwice.should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'facebook').should.be.true();
|
|
|
|
knexMock.schema.hasColumn.calledWith('users', 'twitter').should.be.true();
|
|
|
|
|
|
|
|
addColumnStub.callCount.should.eql(1);
|
|
|
|
addColumnStub.calledWith('users', 'facebook').should.be.true();
|
|
|
|
addColumnStub.calledWith('users', 'twitter').should.be.false();
|
|
|
|
|
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
2016-03-22 21:02:00 +03:00
|
|
|
});
|
|
|
|
});
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Update Database Schema', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
var updateDatabaseSchema = update.__get__('updateDatabaseSchema'),
|
|
|
|
getVersionTasksStub;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
getVersionTasksStub = sandbox.stub(schema.versioning, 'getUpdateDatabaseTasks');
|
|
|
|
});
|
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
it('should not do anything if there are no tasks', function (done) {
|
2016-03-23 18:02:40 +03:00
|
|
|
getVersionTasksStub.returns([]);
|
2016-03-14 20:39:00 +03:00
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
updateDatabaseSchema(['001'], loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
getVersionTasksStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call the tasks if they are provided', function (done) {
|
2016-03-23 18:02:40 +03:00
|
|
|
var task1Stub = sandbox.stub().returns(new Promise.resolve()),
|
|
|
|
task2Stub = sandbox.stub().returns(new Promise.resolve());
|
|
|
|
|
|
|
|
getVersionTasksStub.returns([task1Stub, task2Stub]);
|
2016-03-14 20:39:00 +03:00
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
updateDatabaseSchema(['001'], loggerStub).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
getVersionTasksStub.calledOnce.should.be.true();
|
|
|
|
task1Stub.calledOnce.should.be.true();
|
|
|
|
task2Stub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledTwice.should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
2016-03-23 18:02:40 +03:00
|
|
|
sinon.assert.callOrder(getVersionTasksStub, loggerStub.info, loggerStub.info, task1Stub, task2Stub);
|
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Init', function () {
|
2016-03-22 21:27:48 +03:00
|
|
|
var defaultVersionStub, databaseVersionStub, errorStub, updateStub, populateStub,
|
|
|
|
resetLog, resetUpdate, resetPopulate;
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
defaultVersionStub = sandbox.stub(schema.versioning, 'getDefaultDatabaseVersion');
|
|
|
|
databaseVersionStub = sandbox.stub(schema.versioning, 'getDatabaseVersion');
|
|
|
|
errorStub = sandbox.stub(errors, 'logErrorAndExit');
|
|
|
|
updateStub = sandbox.stub();
|
|
|
|
populateStub = sandbox.stub();
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
resetLog = migration.__set__('logger', loggerStub);
|
2016-03-14 20:39:00 +03:00
|
|
|
resetUpdate = migration.__set__('update', updateStub);
|
|
|
|
resetPopulate = migration.__set__('populate', populateStub);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
resetLog();
|
|
|
|
resetUpdate();
|
|
|
|
resetPopulate();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do an UPDATE if default version is higher', function (done) {
|
|
|
|
// Setup
|
|
|
|
defaultVersionStub.returns('005');
|
|
|
|
databaseVersionStub.returns(new Promise.resolve('004'));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
migration.init().then(function () {
|
|
|
|
defaultVersionStub.calledOnce.should.be.true();
|
|
|
|
databaseVersionStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
updateStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
updateStub.calledWith('004', '005', loggerStub).should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
errorStub.called.should.be.false();
|
|
|
|
populateStub.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do an UPDATE if default version is significantly higher', function (done) {
|
|
|
|
// Setup
|
|
|
|
defaultVersionStub.returns('010');
|
|
|
|
databaseVersionStub.returns(new Promise.resolve('004'));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
migration.init().then(function () {
|
|
|
|
defaultVersionStub.calledOnce.should.be.true();
|
|
|
|
databaseVersionStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
updateStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
updateStub.calledWith('004', '010', loggerStub).should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
errorStub.called.should.be.false();
|
|
|
|
populateStub.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-03-22 21:27:48 +03:00
|
|
|
it('should just return if versions are the same', function (done) {
|
2016-03-14 20:39:00 +03:00
|
|
|
// Setup
|
|
|
|
defaultVersionStub.returns('004');
|
|
|
|
databaseVersionStub.returns(new Promise.resolve('004'));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
migration.init().then(function () {
|
|
|
|
defaultVersionStub.calledOnce.should.be.true();
|
|
|
|
databaseVersionStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
errorStub.called.should.be.false();
|
|
|
|
updateStub.called.should.be.false();
|
|
|
|
populateStub.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do an UPDATE even if versions are the same, when FORCE_MIGRATION set', function (done) {
|
|
|
|
// Setup
|
|
|
|
defaultVersionStub.returns('004');
|
|
|
|
databaseVersionStub.returns(new Promise.resolve('004'));
|
|
|
|
process.env.FORCE_MIGRATION = true;
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
migration.init().then(function () {
|
|
|
|
defaultVersionStub.calledOnce.should.be.true();
|
|
|
|
databaseVersionStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
updateStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
updateStub.calledWith('004', '004', loggerStub).should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
errorStub.called.should.be.false();
|
|
|
|
populateStub.called.should.be.false();
|
|
|
|
|
|
|
|
delete process.env.FORCE_MIGRATION;
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do a POPULATE if settings table does not exist', function (done) {
|
|
|
|
// Setup
|
|
|
|
defaultVersionStub.returns('004');
|
|
|
|
databaseVersionStub.returns(new Promise.reject(new Error('Settings table does not exist')));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
migration.init().then(function () {
|
|
|
|
defaultVersionStub.calledOnce.should.be.true();
|
|
|
|
databaseVersionStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
populateStub.called.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
populateStub.calledWith(loggerStub, false).should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
errorStub.called.should.be.false();
|
|
|
|
updateStub.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do a POPULATE with TABLES ONLY if settings table does not exist & tablesOnly is set', function (done) {
|
|
|
|
// Setup
|
|
|
|
defaultVersionStub.returns('004');
|
|
|
|
databaseVersionStub.returns(new Promise.reject(new Error('Settings table does not exist')));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
migration.init(true).then(function () {
|
|
|
|
defaultVersionStub.calledOnce.should.be.true();
|
|
|
|
databaseVersionStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.calledOnce.should.be.true();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
populateStub.called.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
populateStub.calledWith(loggerStub, true).should.be.true();
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
errorStub.called.should.be.false();
|
|
|
|
updateStub.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if the database version is higher than the default', function (done) {
|
|
|
|
// Setup
|
|
|
|
defaultVersionStub.returns('004');
|
|
|
|
databaseVersionStub.returns(new Promise.resolve('010'));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
migration.init().then(function () {
|
|
|
|
defaultVersionStub.calledOnce.should.be.true();
|
|
|
|
databaseVersionStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
errorStub.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
populateStub.called.should.be.false();
|
|
|
|
updateStub.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if the database version returns an error other than settings not existing', function (done) {
|
|
|
|
// Setup
|
|
|
|
defaultVersionStub.returns('004');
|
|
|
|
databaseVersionStub.returns(new Promise.reject(new Error('Something went wrong')));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
migration.init().then(function () {
|
|
|
|
databaseVersionStub.calledOnce.should.be.true();
|
2016-03-21 15:44:23 +03:00
|
|
|
loggerStub.info.called.should.be.false();
|
|
|
|
loggerStub.warn.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
errorStub.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
defaultVersionStub.calledOnce.should.be.false();
|
|
|
|
populateStub.called.should.be.false();
|
|
|
|
updateStub.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
describe('Logger', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
var logger, errorsInfoStub, errorsWarnStub;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
logger = migration.__get__('logger');
|
|
|
|
errorsInfoStub = sandbox.stub(errors, 'logComponentInfo');
|
|
|
|
errorsWarnStub = sandbox.stub(errors, 'logComponentWarn');
|
|
|
|
});
|
2016-03-14 20:39:00 +03:00
|
|
|
|
2016-03-23 18:02:40 +03:00
|
|
|
it('should output an info message prefixed with "Migrations"', function () {
|
2016-03-21 15:44:23 +03:00
|
|
|
logger.info('Stuff');
|
2016-03-14 20:39:00 +03:00
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
errorsInfoStub.calledOnce.should.be.true();
|
|
|
|
errorsInfoStub.calledWith('Migrations', 'Stuff').should.be.true();
|
|
|
|
errorsWarnStub.called.should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should output a warn message prefixed with "Skipped Migrations"', function () {
|
|
|
|
logger.warn('Stuff');
|
|
|
|
|
|
|
|
errorsWarnStub.calledOnce.should.be.true();
|
|
|
|
errorsWarnStub.calledWith('Skipping Migrations', 'Stuff').should.be.true();
|
|
|
|
errorsInfoStub.called.should.be.false();
|
2016-03-14 20:39:00 +03:00
|
|
|
});
|
|
|
|
});
|
2014-12-20 13:16:29 +03:00
|
|
|
});
|