2013-09-24 14:46:30 +04:00
|
|
|
var when = require('when'),
|
|
|
|
_ = require('underscore'),
|
2013-09-15 20:04:42 +04:00
|
|
|
migration = require('../migration'),
|
2013-09-23 02:20:08 +04:00
|
|
|
knex = require('../../models/base').knex,
|
2013-11-18 18:21:15 +04:00
|
|
|
schema = require('../schema'),
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2013-11-24 18:29:36 +04:00
|
|
|
excludedTables = ['sessions'],
|
2013-09-15 20:04:42 +04:00
|
|
|
exporter;
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2013-09-15 20:04:42 +04:00
|
|
|
exporter = function () {
|
2013-11-18 18:21:15 +04:00
|
|
|
var tablesToExport = _.keys(schema);
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2013-09-15 20:04:42 +04:00
|
|
|
return when.join(migration.getDatabaseVersion(), tablesToExport).then(function (results) {
|
|
|
|
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) {
|
|
|
|
return knex(name).select();
|
|
|
|
}
|
2013-09-15 20:04:42 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
return when.all(selectOps).then(function (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 when.resolve(exportData);
|
|
|
|
}, function (err) {
|
|
|
|
console.log("Error exporting data: " + err);
|
|
|
|
});
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-09-15 20:04:42 +04:00
|
|
|
|
|
|
|
module.exports = exporter;
|