2013-08-03 19:11:16 +04:00
|
|
|
var when = require('when'),
|
2013-09-15 20:04:42 +04:00
|
|
|
_ = require('underscore'),
|
|
|
|
migration = require('../migration'),
|
|
|
|
client = require('../../models/base').client,
|
|
|
|
knex = require('../../models/base').Knex,
|
2013-06-16 00:52:03 +04:00
|
|
|
|
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
|
|
|
function getTablesFromSqlite3() {
|
|
|
|
return knex.Raw("select * from sqlite_master where type = 'table'").then(function (response) {
|
|
|
|
return _.reject(_.pluck(response, 'tbl_name'), function (name) {
|
|
|
|
return name === 'sqlite_sequence';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-08-03 19:11:16 +04:00
|
|
|
|
2013-09-15 20:04:42 +04:00
|
|
|
function getTablesFromMySQL() {
|
2013-09-25 12:38:03 +04:00
|
|
|
return knex.Raw("show tables").then(function (response) {
|
2013-09-15 20:04:42 +04:00
|
|
|
return _.flatten(_.map(response, function (entry) {
|
|
|
|
return _.values(entry);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2013-09-15 20:04:42 +04:00
|
|
|
exporter = function () {
|
|
|
|
var tablesToExport;
|
|
|
|
|
|
|
|
if (client === 'sqlite3') {
|
|
|
|
tablesToExport = getTablesFromSqlite3();
|
|
|
|
} else if (client === 'mysql') {
|
|
|
|
tablesToExport = getTablesFromMySQL();
|
|
|
|
} else {
|
|
|
|
return when.reject("No exporter for database client " + client);
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
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) {
|
|
|
|
return knex(name).select();
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|