mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
584bd15b76
refs #7489 - as we are now using a different migration approach (knex-migrator), we don't need to remember the database version anymore - it was once used to check the state of a database and based on it we decided to migrate or not - with knex-migrator everything depends on the migration table entries and the current ghost version you are on - on current master the leftover usage is to add the db version when exporting the database, which can be replaced by reading the ghost version - removing this solves also an interesting migration case with knex-migrator: - you are on 1.0 - you update to 1.1, but 1.1 has no migrations - the db version would remain in 1.0 - because the db version was only updated when knex migrator executed a migration
87 lines
2.4 KiB
JavaScript
87 lines
2.4 KiB
JavaScript
var _ = require('lodash'),
|
|
Promise = require('bluebird'),
|
|
db = require('../../data/db'),
|
|
commands = require('../schema').commands,
|
|
serverUtils = require('../../utils'),
|
|
ghostVersion = require('../../utils/ghost-version'),
|
|
errors = require('../../errors'),
|
|
logging = require('../../logging'),
|
|
settings = require('../../api/settings'),
|
|
i18n = require('../../i18n'),
|
|
excludedTables = ['accesstokens', 'refreshtokens', 'clients', 'client_trusted_domains'],
|
|
modelOptions = {context: {internal: true}},
|
|
|
|
// private
|
|
getVersionAndTables,
|
|
exportTable,
|
|
|
|
// public
|
|
doExport,
|
|
exportFileName;
|
|
|
|
exportFileName = function exportFileName() {
|
|
var datetime = (new Date()).toJSON().substring(0, 10),
|
|
title = '';
|
|
|
|
return settings.read(_.extend({}, {key: 'title'}, modelOptions)).then(function (result) {
|
|
if (result) {
|
|
title = serverUtils.safeString(result.settings[0].value) + '.';
|
|
}
|
|
return title + 'ghost.' + datetime + '.json';
|
|
}).catch(function (err) {
|
|
logging.error(new errors.GhostError({err: err}));
|
|
return 'ghost.' + datetime + '.json';
|
|
});
|
|
};
|
|
|
|
getVersionAndTables = function getVersionAndTables() {
|
|
var props = {
|
|
version: ghostVersion.full,
|
|
tables: commands.getTables()
|
|
};
|
|
|
|
return Promise.props(props);
|
|
};
|
|
|
|
exportTable = function exportTable(tableName) {
|
|
if (excludedTables.indexOf(tableName) < 0) {
|
|
return db.knex(tableName).select();
|
|
}
|
|
};
|
|
|
|
doExport = function doExport() {
|
|
var tables, version;
|
|
|
|
return getVersionAndTables().then(function exportAllTables(result) {
|
|
tables = result.tables;
|
|
version = result.version;
|
|
|
|
return Promise.mapSeries(tables, exportTable);
|
|
}).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];
|
|
});
|
|
|
|
return exportData;
|
|
}).catch(function (err) {
|
|
return Promise.reject(new errors.DataExportError({
|
|
err: err,
|
|
context: i18n.t('errors.data.export.errorExportingData')
|
|
}));
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
doExport: doExport,
|
|
fileName: exportFileName
|
|
};
|