Ghost/core/test/unit/migration_spec.js
Katharina Irrgang 584bd15b76 🔥 remove database version (#7894)
refs #7489

- as we are now using a different migration approach (knex-migrator), we don't need to remember the database version anymore
- it was once used to check the state of a database and based on it we decided to migrate or not
- with knex-migrator everything depends on the migration table entries and the current ghost version you are on
- on current master the leftover usage is to add the db version when exporting the database, which can be replaced by reading the ghost version
- removing this solves also an interesting migration case with knex-migrator:
  - you are on 1.0
  - you update to 1.1, but 1.1 has no migrations
  - the db version would remain in 1.0
  - because the db version was only updated when knex migrator executed a migration
2017-01-26 12:12:00 +00:00

73 lines
2.7 KiB
JavaScript

var sinon = require('sinon'),
rewire = require('rewire'),
_ = require('lodash'),
Promise = require('bluebird'),
crypto = require('crypto'),
fs = require('fs'),
models = require('../../server/models'),
exporter = require('../../server/data/export'),
schema = require('../../server/data/schema'),
backupDatabase = rewire('../../server/data/db/backup'),
fixtures = require('../../server/data/schema/fixtures'),
sandbox = sinon.sandbox.create();
// 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
var currentSchemaHash = '71d6b843f798352f804db09e5478eef5',
currentFixturesHash = 'b9e684a87353c592df9b23948e364c05';
// 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)).digest('hex');
schemaHash.should.eql(currentSchemaHash);
fixturesHash.should.eql(currentFixturesHash);
});
});
describe('Migrations', function () {
before(function () {
models.init();
});
afterEach(function () {
sandbox.restore();
});
describe('Backup', function () {
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();
});
it('should create a backup JSON file', function (done) {
backupDatabase().then(function () {
exportStub.calledOnce.should.be.true();
filenameStub.calledOnce.should.be.true();
fsStub.calledOnce.should.be.true();
done();
}).catch(done);
});
});
});