2014-07-19 16:20:16 +04:00
|
|
|
var _ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-07-19 16:20:16 +04:00
|
|
|
versioning = require('../versioning'),
|
|
|
|
config = require('../../config'),
|
|
|
|
utils = require('../utils'),
|
|
|
|
serverUtils = require('../../utils'),
|
|
|
|
errors = require('../../errors'),
|
|
|
|
settings = require('../../api/settings'),
|
|
|
|
|
|
|
|
excludedTables = ['accesstokens', 'refreshtokens', 'clients'],
|
|
|
|
exporter,
|
|
|
|
exportFileName;
|
|
|
|
|
|
|
|
exportFileName = function () {
|
|
|
|
var datetime = (new Date()).toJSON().substring(0, 10),
|
|
|
|
title = '';
|
|
|
|
|
|
|
|
return settings.read({key: 'title', context: {internal: true}}).then(function (result) {
|
|
|
|
if (result) {
|
|
|
|
title = serverUtils.safeString(result.settings[0].value) + '.';
|
|
|
|
}
|
|
|
|
return title + 'ghost.' + datetime + '.json';
|
|
|
|
}).catch(function (err) {
|
|
|
|
errors.logError(err);
|
|
|
|
return 'ghost.' + datetime + '.json';
|
|
|
|
});
|
|
|
|
};
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2013-09-15 20:04:42 +04:00
|
|
|
exporter = function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.join(versioning.getDatabaseVersion(), utils.getTables()).then(function (results) {
|
2013-09-15 20:04:42 +04:00
|
|
|
var version = results[0],
|
|
|
|
tables = results[1],
|
|
|
|
selectOps = _.map(tables, function (name) {
|
2013-11-24 18:29:36 +04:00
|
|
|
if (excludedTables.indexOf(name) < 0) {
|
2014-07-17 18:33:21 +04:00
|
|
|
return config.database.knex(name).select();
|
2013-11-24 18:29:36 +04:00
|
|
|
}
|
2013-09-15 20:04:42 +04:00
|
|
|
});
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(selectOps).then(function (tableData) {
|
2013-09-15 20:04:42 +04:00
|
|
|
var exportData = {
|
|
|
|
meta: {
|
|
|
|
exported_on: new Date().getTime(),
|
|
|
|
version: version
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
// Filled below
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_.each(tables, function (name, i) {
|
|
|
|
exportData.data[name] = tableData[i];
|
|
|
|
});
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return exportData;
|
2014-06-10 20:06:08 +04:00
|
|
|
}).catch(function (err) {
|
2014-07-19 16:20:16 +04:00
|
|
|
errors.logAndThrowError(err, 'Error exporting data', '');
|
2013-09-15 20:04:42 +04:00
|
|
|
});
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-09-15 20:04:42 +04:00
|
|
|
|
2014-06-10 00:37:44 +04:00
|
|
|
module.exports = exporter;
|
2014-07-19 16:20:16 +04:00
|
|
|
module.exports.fileName = exportFileName;
|