0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Merge pull request #163 from jgable/exportData

Data import and export
This commit is contained in:
Hannah Wolfe 2013-06-17 01:19:48 -07:00
commit f58c8eb6c3
9 changed files with 315 additions and 2 deletions

View file

@ -114,7 +114,6 @@
};
};
module.exports.posts = posts;
module.exports.users = users;
module.exports.settings = settings;

View file

@ -0,0 +1,49 @@
(function () {
"use strict";
var _ = require("underscore"),
when = require("when"),
knex = require('../../models/base').Knex,
Exporter001;
Exporter001 = function () {
this.version = "001";
};
Exporter001.prototype.exportData = function () {
var self = this,
tables = ['posts', 'users', 'roles', 'roles_users', 'permissions', 'permissions_roles', 'settings'],
selectOps = _.map(tables, function (name) {
return knex(name).select();
});
return when.all(selectOps).then(function (tableData) {
var exportData = {
meta: {
exported_on: new Date().getTime(),
version: self.version
},
data: {
// Filled below
}
};
_.each(tables, function (name, i) {
exportData.data[name] = tableData[i];
});
return when.resolve(exportData);
}, function (err) {
console.log("Error exporting data: " + err);
});
};
module.exports = {
// Make available for unit tests
Exporter001: Exporter001,
exportData: function () {
return new Exporter001().exportData();
}
};
}());

View file

@ -0,0 +1,21 @@
(function () {
"use strict";
var when = require('when');
module.exports = function (version) {
var exporter;
try {
exporter = require("./" + version);
} catch (ignore) {
// Zero effs given
}
if (!exporter) {
return when.reject("No exporter found");
}
return exporter.exportData();
};
}());

View file

@ -92,6 +92,14 @@ module.exports = {
"created_by": 1,
"updated_by": 1,
"type": "general"
},
{
"uuid": uuid.v4(),
"key": "currentVersion",
"value": "001",
"created_by": 1,
"updated_by": 1,
"type": "general"
}
],

View file

@ -0,0 +1,58 @@
(function () {
"use strict";
var when = require("when"),
_ = require("underscore"),
knex = require('../../models/base').Knex,
Importer001;
Importer001 = function () {
_.bindAll(this, "importFrom001");
this.version = "001";
this.importFrom = {
"001": this.importFrom001
};
};
Importer001.prototype.importData = function (data) {
return this.canImport(data)
.then(function (importerFunc) {
return importerFunc(data);
}, function (reason) {
return when.reject(reason);
});
};
Importer001.prototype.canImport = function (data) {
if (data.meta && data.meta.version && this.importFrom[data.meta.version]) {
return when.resolve(this.importFrom[data.meta.version]);
}
return when.reject("Unsupported version of data");
};
Importer001.prototype.importFrom001 = function (data) {
var insertOps = [];
_.each(data.data, function (tableData, name) {
if (tableData && tableData.length) {
insertOps.push(knex(name).insert(tableData));
}
});
return when.all(insertOps).then(function (results) {
return when.resolve(results);
}, function (err) {
console.log("Error inserting imported data: ", err.message || err, err.stack);
});
};
module.exports = {
Importer001: Importer001,
importData: function (data) {
new Importer001().importData(data);
}
};
}());

View file

@ -0,0 +1,21 @@
(function () {
"use strict";
var when = require('when');
module.exports = function (version, data) {
var importer;
try {
importer = require("./" + version);
} catch (ignore) {
// Zero effs given
}
if (!importer) {
return when.reject("No importer found");
}
return importer.importData(data);
};
}());

View file

@ -0,0 +1,67 @@
/*globals describe, beforeEach, it*/
(function () {
"use strict";
var _ = require("underscore"),
should = require('should'),
when = require('when'),
sinon = require('sinon'),
helpers = require('./helpers'),
exporter = require('../../shared/data/export'),
Exporter001 = require('../../shared/data/export/001'),
errors = require('../../shared/errorHandling');
describe("Export", function () {
should.exist(exporter);
beforeEach(function (done) {
helpers.resetData().then(function () {
done();
}, done);
});
it("resolves 001", function (done) {
var exportStub = sinon.stub(Exporter001, "exportData", function () {
return when.resolve();
});
exporter("001").then(function () {
exportStub.called.should.equal(true);
exportStub.restore();
done();
}, errors.throwError);
});
describe("001", function () {
should.exist(Exporter001);
it("exports data", function (done) {
exporter("001").then(function (exportData) {
var tables = ['posts', 'users', 'roles', 'roles_users', 'permissions', 'permissions_roles', 'settings'];
should.exist(exportData);
should.exist(exportData.meta);
should.exist(exportData.data);
exportData.meta.version.should.equal("001");
_.each(tables, function (name) {
should.exist(exportData.data[name]);
});
done();
}, function () {
console.log("Error in exporter");
});
});
});
});
}());

View file

@ -47,10 +47,15 @@
helpers = {
resetData: function () {
return migrations.one.down().then(function () {
return this.clearData().then(function () {
return migrations.one.up();
});
},
clearData: function () {
return migrations.one.down();
},
insertMorePosts: function () {
var lang, status, posts, promises = [], i, j;
for (i = 0; i < 2; i += 1) {

View file

@ -0,0 +1,85 @@
/*globals describe, beforeEach, it*/
(function () {
"use strict";
var _ = require("underscore"),
should = require('should'),
when = require('when'),
sinon = require('sinon'),
knex = require("../../shared/models/base").Knex,
helpers = require('./helpers'),
exporter = require('../../shared/data/export'),
importer = require('../../shared/data/import'),
Importer001 = require('../../shared/data/import/001'),
errors = require('../../shared/errorHandling');
describe("Import", function () {
should.exist(exporter);
beforeEach(function (done) {
helpers.resetData().then(function () {
done();
}, done);
});
it("resolves 001", function (done) {
var importStub = sinon.stub(Importer001, "importData", function () {
return when.resolve();
}),
fakeData = { test: true };
importer("001", fakeData).then(function () {
importStub.calledWith(fakeData).should.equal(true);
importStub.restore();
done();
}, errors.throwError);
});
describe("001", function () {
this.timeout(4000);
should.exist(Importer001);
it("imports data from 001", function (done) {
var exportData;
// TODO: Should have static test data here?
exporter("001").then(function (exported) {
exportData = exported;
// Clear the data from all tables.
var tables = ['posts', 'users', 'roles', 'roles_users', 'permissions', 'permissions_roles', 'settings'],
truncateOps = _.map(tables, function (name) {
return knex(name).truncate();
});
return when.all(truncateOps);
}).then(function () {
return importer("001", exportData);
}).then(function (importResult) {
// Grab the data from tables
return when.all([
knex("users").select(),
knex("posts").select(),
knex("settings").select()
]);
}).then(function (importedData) {
should.exist(importedData);
importedData.length.should.equal(3);
importedData[0].length.should.equal(exportData.data.users.length);
importedData[1].length.should.equal(exportData.data.posts.length);
importedData[2].length.should.equal(exportData.data.settings.length);
done();
});
});
});
});
}());