Ghost/core/server/data/exporter/index.js

117 lines
3.4 KiB
JavaScript
Raw Normal View History

const _ = require('lodash');
const Promise = require('bluebird');
const db = require('../../data/db');
const commands = require('../schema').commands;
const ghostVersion = require('../../lib/ghost-version');
const {i18n} = require('../../lib/common');
const logging = require('../../../shared/logging');
Refactored `common` lib import to use destructuring (#11835) * refactored `core/frontend/apps` to destructure common imports * refactored `core/frontend/services/{apps, redirects, routing}` to destructure common imports * refactored `core/frontend/services/settings` to destructure common imports * refactored remaining `core/frontend/services` to destructure common imports * refactored `core/server/adapters` to destructure common imports * refactored `core/server/data/{db, exporter, schema, validation}` to destructure common imports * refactored `core/server/data/importer` to destructure common imports * refactored `core/server/models/{base, plugins, relations}` to destructure common imports * refactored remaining `core/server/models` to destructure common imports * refactored `core/server/api/canary/utils/serializers/output` to destructure common imports * refactored remaining `core/server/api/canary/utils` to destructure common imports * refactored remaining `core/server/api/canary` to destructure common imports * refactored `core/server/api/shared` to destructure common imports * refactored `core/server/api/v2/utils` to destructure common imports * refactored remaining `core/server/api/v2` to destructure common imports * refactored `core/frontend/meta` to destructure common imports * fixed some tests referencing `common.errors` instead of `@tryghost/errors` - Not all of them need to be updated; only updating the ones that are causing failures * fixed errors import being shadowed by local scope
2020-05-22 21:22:20 +03:00
const errors = require('@tryghost/errors');
const security = require('@tryghost/security');
const models = require('../../models');
const EXCLUDED_TABLES = [
'sessions',
'mobiledoc_revisions',
'email_batches',
'email_recipients',
'members_payment_events',
'members_login_events',
'members_email_change_events',
'members_status_events',
'members_paid_subscription_events',
'members_subscribe_events'
];
const EXCLUDED_SETTING_KEYS = [
'stripe_connect_publishable_key',
'stripe_connect_secret_key',
'stripe_connect_account_id',
'stripe_secret_key',
'stripe_publishable_key',
'members_stripe_webhook_id',
'members_stripe_webhook_secret'
];
const modelOptions = {context: {internal: true}};
const exportFileName = async function exportFileName(options) {
const datetime = require('moment')().format('YYYY-MM-DD-HH-mm-ss');
let title = '';
options = options || {};
// custom filename
if (options.filename) {
return options.filename + '.json';
}
try {
const settingsTitle = await models.Settings.findOne({key: 'title'}, _.merge({}, modelOptions, _.pick(options, 'transacting')));
if (settingsTitle) {
title = security.string.safe(settingsTitle.get('value')) + '.';
}
return title + 'ghost.' + datetime + '.json';
} catch (err) {
Refactored `common` lib import to use destructuring (#11835) * refactored `core/frontend/apps` to destructure common imports * refactored `core/frontend/services/{apps, redirects, routing}` to destructure common imports * refactored `core/frontend/services/settings` to destructure common imports * refactored remaining `core/frontend/services` to destructure common imports * refactored `core/server/adapters` to destructure common imports * refactored `core/server/data/{db, exporter, schema, validation}` to destructure common imports * refactored `core/server/data/importer` to destructure common imports * refactored `core/server/models/{base, plugins, relations}` to destructure common imports * refactored remaining `core/server/models` to destructure common imports * refactored `core/server/api/canary/utils/serializers/output` to destructure common imports * refactored remaining `core/server/api/canary/utils` to destructure common imports * refactored remaining `core/server/api/canary` to destructure common imports * refactored `core/server/api/shared` to destructure common imports * refactored `core/server/api/v2/utils` to destructure common imports * refactored remaining `core/server/api/v2` to destructure common imports * refactored `core/frontend/meta` to destructure common imports * fixed some tests referencing `common.errors` instead of `@tryghost/errors` - Not all of them need to be updated; only updating the ones that are causing failures * fixed errors import being shadowed by local scope
2020-05-22 21:22:20 +03:00
logging.error(new errors.GhostError({err: err}));
return 'ghost.' + datetime + '.json';
}
};
2013-06-16 00:52:03 +04:00
const exportTable = function exportTable(tableName, options) {
if (EXCLUDED_TABLES.indexOf(tableName) < 0 ||
(options.include && _.isArray(options.include) && options.include.indexOf(tableName) !== -1)) {
const query = (options.transacting || db.knex)(tableName);
return query.select();
}
};
const getSettingsTableData = function getSettingsTableData(settingsData) {
return settingsData && settingsData.filter((setting) => {
return !EXCLUDED_SETTING_KEYS.includes(setting.key);
});
};
const doExport = async function doExport(options) {
options = options || {include: []};
try {
const tables = await commands.getTables(options.transacting);
const tableData = await Promise.mapSeries(tables, function (tableName) {
return exportTable(tableName, options);
});
const exportData = {
meta: {
exported_on: new Date().getTime(),
version: ghostVersion.full
},
data: {
// Filled below
}
};
tables.forEach((name, i) => {
if (name === 'settings') {
exportData.data[name] = getSettingsTableData(tableData[i]);
} else {
exportData.data[name] = tableData[i];
}
});
return exportData;
} catch (err) {
throw new errors.DataExportError({
err: err,
Refactored `common` lib import to use destructuring (#11835) * refactored `core/frontend/apps` to destructure common imports * refactored `core/frontend/services/{apps, redirects, routing}` to destructure common imports * refactored `core/frontend/services/settings` to destructure common imports * refactored remaining `core/frontend/services` to destructure common imports * refactored `core/server/adapters` to destructure common imports * refactored `core/server/data/{db, exporter, schema, validation}` to destructure common imports * refactored `core/server/data/importer` to destructure common imports * refactored `core/server/models/{base, plugins, relations}` to destructure common imports * refactored remaining `core/server/models` to destructure common imports * refactored `core/server/api/canary/utils/serializers/output` to destructure common imports * refactored remaining `core/server/api/canary/utils` to destructure common imports * refactored remaining `core/server/api/canary` to destructure common imports * refactored `core/server/api/shared` to destructure common imports * refactored `core/server/api/v2/utils` to destructure common imports * refactored remaining `core/server/api/v2` to destructure common imports * refactored `core/frontend/meta` to destructure common imports * fixed some tests referencing `common.errors` instead of `@tryghost/errors` - Not all of them need to be updated; only updating the ones that are causing failures * fixed errors import being shadowed by local scope
2020-05-22 21:22:20 +03:00
context: i18n.t('errors.data.export.errorExportingData')
});
}
};
module.exports = {
doExport: doExport,
fileName: exportFileName,
EXCLUDED_TABLES: EXCLUDED_TABLES
};