2013-09-24 12:46:30 +02:00
|
|
|
var when = require('when'),
|
2014-02-05 00:40:30 -08:00
|
|
|
_ = require('lodash'),
|
2013-09-15 17:04:42 +01:00
|
|
|
migration = require('../migration'),
|
2013-09-22 18:20:08 -04:00
|
|
|
knex = require('../../models/base').knex,
|
2014-01-03 00:37:21 +00:00
|
|
|
schema = require('../schema').tables,
|
2013-06-15 20:52:03 +00:00
|
|
|
|
2013-11-24 15:29:36 +01:00
|
|
|
excludedTables = ['sessions'],
|
2013-09-15 17:04:42 +01:00
|
|
|
exporter;
|
2013-06-15 20:52:03 +00:00
|
|
|
|
2013-09-15 17:04:42 +01:00
|
|
|
exporter = function () {
|
2013-11-18 15:21:15 +01:00
|
|
|
var tablesToExport = _.keys(schema);
|
2013-06-15 20:52:03 +00:00
|
|
|
|
2013-09-15 17:04:42 +01: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 15:29:36 +01:00
|
|
|
if (excludedTables.indexOf(name) < 0) {
|
|
|
|
return knex(name).select();
|
|
|
|
}
|
2013-09-15 17:04:42 +01: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 12:43:15 +01:00
|
|
|
};
|
2013-09-15 17:04:42 +01:00
|
|
|
|
|
|
|
module.exports = exporter;
|