mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 03:14:03 +03:00
ffd866cedc
refs https://github.com/TryGhost/Team/issues/555 - Export files included a lot of data which was not used in the importer, for example: members, labels, migrations and many more. This lead to a lot of clutter in the import files and made it hard to reason about their purpose. - The main purpose of exports - is to export importable resources. These are posts, tags, and users. The rest of data like members or migrations either have their own importer (like CSV importer for members) or does not and should not have any ways to be imported. - These changes are in now way complete. It's a first step towards resource-based exports which could be properly versioned in the future on API level and not be a mirror of the DB structure. - This is sort of a breaking change. But we are doing it because: (1) its an internal API that should not be used by external clients, (2) there was no public contract to have this API stable at any point, (3) we really need to get back the control over export files structure and size - In case an external client was dependent on some structure of the exported json file they can still pass in ALL of previously exported data by passing table names in `include` query parameter.
168 lines
6.2 KiB
JavaScript
168 lines
6.2 KiB
JavaScript
const should = require('should');
|
|
const sinon = require('sinon');
|
|
const rewire = require('rewire');
|
|
const Promise = require('bluebird');
|
|
const errors = require('@tryghost/errors');
|
|
const db = require('../../../../core/server/data/db');
|
|
const exporter = rewire('../../../../core/server/data/exporter');
|
|
const schema = require('../../../../core/server/data/schema');
|
|
const models = require('../../../../core/server/models');
|
|
const schemaTables = Object.keys(schema.tables);
|
|
|
|
describe('Exporter', function () {
|
|
let tablesStub;
|
|
let queryMock;
|
|
let knexMock;
|
|
|
|
before(function () {
|
|
models.init();
|
|
});
|
|
|
|
afterEach(function () {
|
|
sinon.restore();
|
|
});
|
|
|
|
describe('doExport', function () {
|
|
beforeEach(function () {
|
|
tablesStub = sinon.stub(schema.commands, 'getTables').returns(schemaTables);
|
|
|
|
queryMock = {
|
|
whereNot: sinon.stub(),
|
|
select: sinon.stub()
|
|
};
|
|
|
|
knexMock = sinon.stub().returns(queryMock);
|
|
|
|
sinon.stub(db, 'knex').get(function () {
|
|
return knexMock;
|
|
});
|
|
});
|
|
|
|
it('should try to export all the correct tables (without excluded)', function (done) {
|
|
exporter.doExport().then(function (exportData) {
|
|
// NOTE: 9 default tables
|
|
const expectedCallCount = 9;
|
|
|
|
should.exist(exportData);
|
|
|
|
exportData.meta.version.should.match(/\d+.\d+.\d+/gi);
|
|
|
|
tablesStub.calledOnce.should.be.true();
|
|
db.knex.called.should.be.true();
|
|
|
|
knexMock.callCount.should.eql(expectedCallCount);
|
|
queryMock.select.callCount.should.have.eql(expectedCallCount);
|
|
|
|
knexMock.getCall(0).args[0].should.eql('posts');
|
|
knexMock.getCall(1).args[0].should.eql('posts_meta');
|
|
knexMock.getCall(2).args[0].should.eql('users');
|
|
knexMock.getCall(3).args[0].should.eql('posts_authors');
|
|
knexMock.getCall(4).args[0].should.eql('roles');
|
|
knexMock.getCall(5).args[0].should.eql('roles_users');
|
|
knexMock.getCall(6).args[0].should.eql('settings');
|
|
knexMock.getCall(7).args[0].should.eql('tags');
|
|
knexMock.getCall(8).args[0].should.eql('posts_tags');
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('should try to export all the correct tables with extra tables', function (done) {
|
|
const include = ['mobiledoc_revisions', 'email_recipients'];
|
|
|
|
exporter.doExport({include}).then(function (exportData) {
|
|
// NOTE: 9 default tables + 2 includes
|
|
const expectedCallCount = 11;
|
|
|
|
should.exist(exportData);
|
|
|
|
exportData.meta.version.should.match(/\d+.\d+.\d+/gi);
|
|
|
|
tablesStub.calledOnce.should.be.true();
|
|
db.knex.called.should.be.true();
|
|
queryMock.select.called.should.be.true();
|
|
|
|
knexMock.callCount.should.eql(expectedCallCount);
|
|
queryMock.select.callCount.should.have.eql(expectedCallCount);
|
|
|
|
knexMock.getCall(0).args[0].should.eql('posts');
|
|
knexMock.getCall(1).args[0].should.eql('posts_meta');
|
|
knexMock.getCall(2).args[0].should.eql('users');
|
|
knexMock.getCall(3).args[0].should.eql('posts_authors');
|
|
knexMock.getCall(4).args[0].should.eql('roles');
|
|
knexMock.getCall(5).args[0].should.eql('roles_users');
|
|
knexMock.getCall(6).args[0].should.eql('settings');
|
|
knexMock.getCall(7).args[0].should.eql('tags');
|
|
knexMock.getCall(8).args[0].should.eql('posts_tags');
|
|
knexMock.getCall(9).args[0].should.eql('mobiledoc_revisions');
|
|
knexMock.getCall(10).args[0].should.eql('email_recipients');
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('should catch and log any errors', function (done) {
|
|
// Setup for failure
|
|
queryMock.select.returns(Promise.reject({}));
|
|
|
|
// Execute
|
|
exporter.doExport()
|
|
.then(function () {
|
|
done(new Error('expected error for export'));
|
|
})
|
|
.catch(function (err) {
|
|
(err instanceof errors.DataExportError).should.eql(true);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('exportFileName', function () {
|
|
it('should return a correctly structured filename', function (done) {
|
|
const settingsStub = sinon.stub(models.Settings, 'findOne').returns(
|
|
new Promise.resolve({
|
|
get: function () {
|
|
return 'testblog';
|
|
}
|
|
})
|
|
);
|
|
|
|
exporter.fileName().then(function (result) {
|
|
should.exist(result);
|
|
settingsStub.calledOnce.should.be.true();
|
|
result.should.match(/^testblog\.ghost\.[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}\.json$/);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('should return a correctly structured filename if settings is empty', function (done) {
|
|
const settingsStub = sinon.stub(models.Settings, 'findOne').returns(
|
|
new Promise.resolve()
|
|
);
|
|
|
|
exporter.fileName().then(function (result) {
|
|
should.exist(result);
|
|
settingsStub.calledOnce.should.be.true();
|
|
result.should.match(/^ghost\.[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}\.json$/);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('should return a correctly structured filename if settings errors', function (done) {
|
|
const settingsStub = sinon.stub(models.Settings, 'findOne').returns(
|
|
new Promise.reject()
|
|
);
|
|
|
|
exporter.fileName().then(function (result) {
|
|
should.exist(result);
|
|
settingsStub.calledOnce.should.be.true();
|
|
result.should.match(/^ghost\.[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}\.json$/);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
});
|
|
});
|