From 4b5cfbde2ef122889d3ff51a2695bb58489450d7 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Wed, 25 Sep 2013 09:36:45 +0100 Subject: [PATCH 1/3] Adding missing multipart to importer route closes #869 --- core/server.js | 1 + 1 file changed, 1 insertion(+) diff --git a/core/server.js b/core/server.js index f204436b9c..36a0f2afd0 100644 --- a/core/server.js +++ b/core/server.js @@ -264,6 +264,7 @@ when.all([ghost.init(), helpers.loadCoreHelpers(ghost)]).then(function () { server.use(express.urlencoded()); server.use('/ghost/upload/', express.multipart()); server.use('/ghost/upload/', express.multipart({uploadDir: __dirname + '/content/images'})); + server.use('/ghost/debug/db/import/', express.multipart()); server.use(express.cookieParser(ghost.dbHash)); server.use(express.cookieSession({ cookie: { maxAge: 60000000 }})); From f68633df205b848d5455bdd4403357d72167eab9 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Wed, 25 Sep 2013 09:38:03 +0100 Subject: [PATCH 2/3] Adding missing return to MySQL exporter closes #888 --- core/server/data/export/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/server/data/export/index.js b/core/server/data/export/index.js index ed33f11135..cea94bc1b8 100644 --- a/core/server/data/export/index.js +++ b/core/server/data/export/index.js @@ -15,7 +15,7 @@ function getTablesFromSqlite3() { } function getTablesFromMySQL() { - knex.Raw("show tables").then(function (response) { + return knex.Raw("show tables").then(function (response) { return _.flatten(_.map(response, function (entry) { return _.values(entry); })); From 02a02054e8a05cb9cbbf92570bcd6504cedd867d Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Wed, 25 Sep 2013 11:30:59 +0100 Subject: [PATCH 3/3] Handle duplicate tags on import closes #890 - importer only adds tags which don't exist. - added back the import unit tests - these are basic for now --- core/server/data/import/000.js | 7 +- core/test/unit/import_spec.js | 300 +++++++++++---------------------- 2 files changed, 104 insertions(+), 203 deletions(-) diff --git a/core/server/data/import/000.js b/core/server/data/import/000.js index 9c2bdd8fa8..874ce4db0c 100644 --- a/core/server/data/import/000.js +++ b/core/server/data/import/000.js @@ -81,7 +81,12 @@ function preProcessPostTags(tableData) { function importTags(ops, tableData) { tableData = stripProperties(['id'], tableData); _.each(tableData, function (tag) { - ops.push(models.Tag.add(tag)); + ops.push(models.Tag.read({name: tag.name}).then(function (_tag) { + if (!_tag) { + return models.Tag.add(tag); + } + return when.resolve(_tag); + })); }); } diff --git a/core/test/unit/import_spec.js b/core/test/unit/import_spec.js index d52370e295..bd23779a6c 100644 --- a/core/test/unit/import_spec.js +++ b/core/test/unit/import_spec.js @@ -1,202 +1,98 @@ -///*globals describe, beforeEach, it*/ -//var testUtils = require('./testUtils'), -// should = require('should'), -// sinon = require('sinon'), -// when = require('when'), -// _ = require("underscore"), -// errors = require('../../server/errorHandling'), -// -// // Stuff we are testing -// knex = require("../../server/models/base").Knex, -// migration = require('../../server/data/migration'), -// exporter = require('../../server/data/export'), -// importer = require('../../server/data/import'), -// Importer001 = require('../../server/data/import/001'), -// Importer002 = require('../../server/data/import/002'), -// Settings = require('../../server/models/settings').Settings; -// -//describe("Import", function () { -// -// should.exist(exporter); -// should.exist(importer); -// -// beforeEach(function (done) { -// // clear database... we need to initialise it manually for each test -// testUtils.clearData().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(); -// }).then(null, done); -// }); -// -// describe("001", function () { -// this.timeout(4000); -// -// should.exist(Importer001); -// -// it("imports data from 001", function (done) { -// var exportData; -// -// // initialise database to version 001 - confusingly we have to set the max version to be one higher -// // than the migration version we want -// migration.migrateUpFromVersion('001', '002').then(function () { -// return Settings.populateDefaults(); -// }).then(function () { -// // export the version 001 data ready to import -// // TODO: Should have static test data here? -// return exporter("001"); -// }).then(function (exported) { -// exportData = exported; -// -// // Version 001 exporter required the database be empty... -// 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 () { -// // 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); -// -// // we always have 0 users as there isn't one in fixtures -// importedData[0].length.should.equal(0); -// importedData[1].length.should.equal(exportData.data.posts.length); -// importedData[2].length.should.be.above(0); -// -// _.findWhere(exportData.data.settings, {key: "databaseVersion"}).value.should.equal("001"); -// -// done(); -// }).then(null, done); -// }); -// }); -// -// it("resolves 002", function (done) { -// var importStub = sinon.stub(Importer002, "importData", function () { -// return when.resolve(); -// }), -// fakeData = { test: true }; -// -// importer("002", fakeData).then(function () { -// importStub.calledWith(fakeData).should.equal(true); -// -// importStub.restore(); -// -// done(); -// }).then(null, done); -// }); -// -// describe("002", function () { -// this.timeout(4000); -// -// should.exist(Importer002); -// -// it("imports data from 001", function (done) { -// var exportData; -// -// // initialise database to version 001 - confusingly we have to set the max version to be one higher -// // than the migration version we want -// migration.migrateUpFromVersion('001', '002').then(function () { -// return Settings.populateDefaults(); -// }).then(function () { -// // export the version 001 data ready to import -// // TODO: Should have static test data here? -// return exporter("001"); -// }).then(function (exported) { -// exportData = exported; -// -// // now migrate up to the proper version ready for importing - confusingly we have to set the max version -// // to be one higher than the migration version we want -// return migration.migrateUpFromVersion('002', '003'); -// }).then(function () { -// return importer("002", exportData); -// }).then(function () { -// // 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); -// -// // we always have 0 users as there isn't one in fixtures -// importedData[0].length.should.equal(0); -// // import no longer requires all data to be dropped, and adds posts -// importedData[1].length.should.equal(exportData.data.posts.length + 1); -// importedData[2].length.should.be.above(0); -// -// _.findWhere(importedData[2], {key: "databaseVersion"}).value.should.equal("002"); -// -// done(); -// }).then(null, done); -// }); -// -// it("imports data from 002", function (done) { -// var exportData; -// -// // initialise database to version 001 - confusingly we have to set the max version to be one higher -// // than the migration version we want -// migration.migrateUpFromVersion('001', '003').then(function () { -// return Settings.populateDefaults(); -// }).then(function () { -// // export the version 002 data ready to import -// // TODO: Should have static test data here? -// return exporter("002"); -// }).then(function (exported) { -// exportData = exported; -// -// return importer("002", exportData); -// }).then(function () { -// // 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); -// -// // we always have 0 users as there isn't one in fixtures -// importedData[0].length.should.equal(0); -// // import no longer requires all data to be dropped, and adds posts -// importedData[1].length.should.equal(exportData.data.posts.length + 1); -// importedData[2].length.should.be.above(0); -// -// _.findWhere(importedData[2], {key: "databaseVersion"}).value.should.equal("002"); -// -// done(); -// }).then(null, done); -// }); -// }); -//}); +/*globals describe, beforeEach, it*/ +var testUtils = require('./testUtils'), + should = require('should'), + sinon = require('sinon'), + when = require('when'), + _ = require("underscore"), + errors = require('../../server/errorHandling'), + + // Stuff we are testing + knex = require("../../server/models/base").Knex, + migration = require('../../server/data/migration'), + exporter = require('../../server/data/export'), + importer = require('../../server/data/import'), + Importer000 = require('../../server/data/import/000'), + fixtures = require('../../server/data/fixtures'), + Settings = require('../../server/models/settings').Settings; + +describe("Import", function () { + + should.exist(exporter); + should.exist(importer); + + beforeEach(function (done) { + // clear database... we need to initialise it manually for each test + testUtils.clearData().then(function () { + done(); + }, done); + }); + + it("resolves 000", function (done) { + var importStub = sinon.stub(Importer000, "importData", function () { + return when.resolve(); + }), + fakeData = { test: true }; + + importer("000", fakeData).then(function () { + importStub.calledWith(fakeData).should.equal(true); + + importStub.restore(); + + done(); + }).then(null, done); + }); + + describe("000", function () { + this.timeout(4000); + + should.exist(Importer000); + + it("imports data from 000", function (done) { + var exportData; + + // initialise database to version 000 - confusingly we have to set the max version to be one higher + // than the migration version we want. Could just use migrate from fresh here... but this is more explicit + migration.migrateUpFromVersion('000', '001').then(function () { + // Load the fixtures + return fixtures.populateFixtures(); + }).then(function () { + // Initialise the default settings + return Settings.populateDefaults(); + }).then(function () { + // export the version 000 data ready to import + // TODO: Should have static test data here? + return exporter(); + }).then(function (exported) { + exportData = exported; + + return importer("000", exportData); + }).then(function () { + // Grab the data from tables + return when.all([ + knex("users").select(), + knex("posts").select(), + knex("settings").select(), + knex("tags").select() + ]); + }).then(function (importedData) { + + should.exist(importedData); + importedData.length.should.equal(4, 'Did not get data successfully'); + + // we always have 0 users as there isn't one in fixtures + importedData[0].length.should.equal(0, 'There should not be a user'); + // import no longer requires all data to be dropped, and adds posts + importedData[1].length.should.equal(exportData.data.posts.length + 1, 'Wrong number of posts'); + + // test settings + importedData[2].length.should.be.above(0, 'Wrong number of settings'); + _.findWhere(importedData[2], {key: "databaseVersion"}).value.should.equal("000", 'Wrong database version'); + + // test tags + importedData[3].length.should.equal(exportData.data.tags.length, 'no new tags'); + + done(); + }).then(null, done); + }); + }); +});