2017-11-01 16:44:54 +03:00
|
|
|
var _ = require('lodash'),
|
2016-02-12 14:56:27 +03:00
|
|
|
db = require('../../../data/db'),
|
2014-07-10 19:10:00 +04:00
|
|
|
|
|
|
|
// private
|
|
|
|
doRawFlattenAndPluck,
|
|
|
|
|
|
|
|
// public
|
|
|
|
getTables,
|
|
|
|
getIndexes,
|
|
|
|
getColumns;
|
|
|
|
|
2016-07-14 13:59:42 +03:00
|
|
|
doRawFlattenAndPluck = function doRaw(query, name, transaction) {
|
|
|
|
return (transaction || db.knex).raw(query).then(function (response) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.flatten(_.map(response.rows, name));
|
2014-07-10 19:10:00 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-07-14 13:59:42 +03:00
|
|
|
getTables = function getTables(transaction) {
|
2014-07-10 19:10:00 +04:00
|
|
|
return doRawFlattenAndPluck(
|
2016-07-14 13:59:42 +03:00
|
|
|
'SELECT table_name FROM information_schema.tables WHERE table_schema = CURRENT_SCHEMA()',
|
|
|
|
'table_name',
|
|
|
|
transaction
|
2014-07-10 19:10:00 +04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-07-14 13:59:42 +03:00
|
|
|
getIndexes = function getIndexes(table, transaction) {
|
2014-07-10 19:10:00 +04:00
|
|
|
var selectIndexes = 'SELECT t.relname as table_name, i.relname as index_name, a.attname as column_name' +
|
|
|
|
' FROM pg_class t, pg_class i, pg_index ix, pg_attribute a' +
|
|
|
|
' WHERE t.oid = ix.indrelid and i.oid = ix.indexrelid and' +
|
2014-07-31 16:52:57 +04:00
|
|
|
' a.attrelid = t.oid and a.attnum = ANY(ix.indkey) and t.relname = \'' + table + '\'';
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2016-07-14 13:59:42 +03:00
|
|
|
return doRawFlattenAndPluck(selectIndexes, 'index_name', transaction);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
|
2016-07-14 13:59:42 +03:00
|
|
|
getColumns = function getColumns(table, transaction) {
|
2014-07-31 16:52:57 +04:00
|
|
|
var selectIndexes = 'SELECT column_name FROM information_schema.columns WHERE table_name = \'' + table + '\'';
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2016-07-14 13:59:42 +03:00
|
|
|
return doRawFlattenAndPluck(selectIndexes, 'column_name', transaction);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2017-11-01 16:44:54 +03:00
|
|
|
getTables: getTables,
|
2014-07-10 19:10:00 +04:00
|
|
|
getIndexes: getIndexes,
|
|
|
|
getColumns: getColumns
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|