2017-03-21 11:24:11 +03:00
|
|
|
var should = require('should'), // jshint ignore:line
|
|
|
|
sinon = require('sinon'),
|
2016-07-15 19:22:41 +03:00
|
|
|
rewire = require('rewire'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
crypto = require('crypto'),
|
|
|
|
fs = require('fs'),
|
2016-07-14 13:59:42 +03:00
|
|
|
models = require('../../server/models'),
|
|
|
|
exporter = require('../../server/data/export'),
|
|
|
|
schema = require('../../server/data/schema'),
|
2017-01-25 16:47:49 +03:00
|
|
|
backupDatabase = rewire('../../server/data/db/backup'),
|
|
|
|
fixtures = require('../../server/data/schema/fixtures'),
|
2014-12-20 13:16:29 +03:00
|
|
|
|
2017-03-21 11:24:11 +03:00
|
|
|
sandbox = sinon.sandbox.create();
|
2017-02-14 15:41:28 +03:00
|
|
|
|
2017-12-11 14:35:27 +03:00
|
|
|
/**
|
|
|
|
* @NOTE
|
|
|
|
*
|
|
|
|
* If this test fails for you, you have modified the database schema or fixtures.
|
|
|
|
* When you make a change, please test that:
|
|
|
|
*
|
|
|
|
* 1. A new blog get's installed and the database looks correct and complete.
|
|
|
|
* 2. A blog get's updated from a lower Ghost version and the database looks correct and complete.
|
|
|
|
*
|
|
|
|
* Typical cases:
|
|
|
|
* You have to add a migration script if you've added/modified permissions.
|
|
|
|
* You have to add a migration script if you've add a new table.
|
|
|
|
*/
|
2017-01-25 16:47:49 +03:00
|
|
|
describe('DB version integrity', function () {
|
2016-03-21 15:44:23 +03:00
|
|
|
// Only these variables should need updating
|
2017-11-21 18:43:14 +03:00
|
|
|
var currentSchemaHash = '329f9b498944c459040426e16fc65b11',
|
|
|
|
currentFixturesHash = '90925e0004a0cedd1e6ea789c81ec67d';
|
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');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-05 22:16:28 +03:00
|
|
|
schemaHash = crypto.createHash('md5').update(JSON.stringify(tablesNoValidation), 'binary').digest('hex');
|
|
|
|
fixturesHash = crypto.createHash('md5').update(JSON.stringify(fixtures), 'binary').digest('hex');
|
2016-03-21 15:44:23 +03:00
|
|
|
|
|
|
|
schemaHash.should.eql(currentSchemaHash);
|
|
|
|
fixturesHash.should.eql(currentFixturesHash);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
describe('Migrations', function () {
|
2016-07-14 13:59:42 +03:00
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2016-02-25 10:10:36 +03:00
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
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) {
|
2017-01-25 16:47:49 +03:00
|
|
|
backupDatabase().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();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
2014-12-20 13:16:29 +03:00
|
|
|
});
|