2016-10-06 15:27:35 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
db = require('../../data/db'),
|
|
|
|
commands = require('../schema').commands,
|
2017-12-11 21:14:05 +03:00
|
|
|
globalUtils = require('../../utils'),
|
2017-01-26 15:12:00 +03:00
|
|
|
ghostVersion = require('../../utils/ghost-version'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../lib/common'),
|
2017-12-11 21:14:05 +03:00
|
|
|
models = require('../../models'),
|
2016-03-12 21:54:06 +03:00
|
|
|
excludedTables = ['accesstokens', 'refreshtokens', 'clients', 'client_trusted_domains'],
|
|
|
|
modelOptions = {context: {internal: true}},
|
|
|
|
|
|
|
|
// private
|
|
|
|
getVersionAndTables,
|
|
|
|
exportTable,
|
|
|
|
|
|
|
|
// public
|
|
|
|
doExport,
|
2014-07-19 16:20:16 +04:00
|
|
|
exportFileName;
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
exportFileName = function exportFileName(options) {
|
2014-07-19 16:20:16 +04:00
|
|
|
var datetime = (new Date()).toJSON().substring(0, 10),
|
|
|
|
title = '';
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
return models.Settings.findOne({key: 'title'}, _.merge({}, modelOptions, options)).then(function (result) {
|
2014-07-19 16:20:16 +04:00
|
|
|
if (result) {
|
2017-12-11 21:14:05 +03:00
|
|
|
title = globalUtils.safeString(result.get('value')) + '.';
|
2014-07-19 16:20:16 +04:00
|
|
|
}
|
2017-03-08 13:26:57 +03:00
|
|
|
|
2014-07-19 16:20:16 +04:00
|
|
|
return title + 'ghost.' + datetime + '.json';
|
|
|
|
}).catch(function (err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.error(new common.errors.GhostError({err: err}));
|
2014-07-19 16:20:16 +04:00
|
|
|
return 'ghost.' + datetime + '.json';
|
|
|
|
});
|
|
|
|
};
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
getVersionAndTables = function getVersionAndTables(options) {
|
2016-03-12 21:54:06 +03:00
|
|
|
var props = {
|
2017-01-26 15:12:00 +03:00
|
|
|
version: ghostVersion.full,
|
2017-08-01 16:27:13 +03:00
|
|
|
tables: commands.getTables(options.transacting)
|
2016-03-12 21:54:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.props(props);
|
|
|
|
};
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
exportTable = function exportTable(tableName, options) {
|
2016-03-12 21:54:06 +03:00
|
|
|
if (excludedTables.indexOf(tableName) < 0) {
|
2017-08-01 16:27:13 +03:00
|
|
|
return (options.transacting || db.knex)(tableName).select();
|
2016-03-12 21:54:06 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
doExport = function doExport(options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
var tables, version;
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
return getVersionAndTables(options).then(function exportAllTables(result) {
|
2016-03-12 21:54:06 +03:00
|
|
|
tables = result.tables;
|
|
|
|
version = result.version;
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
return Promise.mapSeries(tables, function (tableName) {
|
|
|
|
return exportTable(tableName, options);
|
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
}).then(function formatData(tableData) {
|
|
|
|
var exportData = {
|
|
|
|
meta: {
|
|
|
|
exported_on: new Date().getTime(),
|
|
|
|
version: version
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
// Filled below
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_.each(tables, function (name, i) {
|
|
|
|
exportData.data[name] = tableData[i];
|
2013-09-15 20:04:42 +04:00
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
return exportData;
|
|
|
|
}).catch(function (err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.DataExportError({
|
2016-10-06 15:27:35 +03:00
|
|
|
err: err,
|
2017-12-12 00:47:46 +03:00
|
|
|
context: common.i18n.t('errors.data.export.errorExportingData')
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2013-09-15 20:04:42 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-09-15 20:04:42 +04:00
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
module.exports = {
|
|
|
|
doExport: doExport,
|
|
|
|
fileName: exportFileName
|
|
|
|
};
|