Skip to content

Commit b8c2b73

Browse files
committed
Add showIndexes and showFields function
1 parent edf05de commit b8c2b73

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

lib/migration.js

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,67 @@ var async = require('async');
1111
module.exports = mixinMigration;
1212

1313
function mixinMigration(PostgreSQL) {
14+
PostgreSQL.prototype.showFields = function(model, cb) {
15+
var sql = 'SELECT column_name AS "column", data_type AS "type", ' +
16+
'is_nullable AS "nullable", character_maximum_length as "length"' // , data_default AS "Default"'
17+
+ ' FROM "information_schema"."columns" WHERE table_name=\'' +
18+
this.table(model) + '\'';
19+
this.execute(sql, function(err, fields) {
20+
if (err) {
21+
return cb(err);
22+
} else {
23+
cb(err, fields);
24+
}
25+
});
26+
};
27+
28+
PostgreSQL.prototype.showIndexes = function(model, cb) {
29+
var sql = 'SELECT t.relname AS "table", i.relname AS "name", ' +
30+
'am.amname AS "type", ix.indisprimary AS "primary", ' +
31+
'ix.indisunique AS "unique", ' +
32+
'ARRAY(SELECT pg_get_indexdef(ix.indexrelid, k + 1, true) ' +
33+
' FROM generate_subscripts(ix.indkey, 1) AS k ' +
34+
' ORDER BY k ) AS "keys", ' +
35+
'ARRAY(SELECT ' +
36+
' CASE ix.indoption[k] & 1 WHEN 1 THEN \'DESC\' ELSE \'ASC\' END ' +
37+
' FROM generate_subscripts(ix.indoption, 1) AS k ' +
38+
' ORDER BY k ' +
39+
') AS "order" ' +
40+
'FROM pg_class t, pg_class i, pg_index ix, pg_am am ' +
41+
'WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND ' +
42+
'i.relam = am.oid AND ' +
43+
't.relkind=\'r\' AND t.relname=\'' +
44+
this.table(model) + '\'';
45+
this.execute(sql, function(err, indexes) {
46+
if (err) {
47+
return cb(err);
48+
} else {
49+
cb(err, indexes);
50+
}
51+
});
52+
};
53+
1454
/*!
1555
* Discover the properties from a table
1656
* @param {String} model The model name
1757
* @param {Function} cb The callback function
1858
*/
1959
PostgreSQL.prototype.getTableStatus = function(model, cb) {
20-
var fields;
21-
var indexes;
22-
23-
function done(err) {
24-
if (fields && indexes) {
25-
cb(err, fields, indexes);
26-
}
27-
}
60+
var fields, indexes;
61+
var self = this;
2862

29-
function decoratedTableDataCallback(err, data) {
30-
if (err) {
31-
console.error(err);
32-
}
33-
if (!err) {
34-
data.forEach(function(field) {
35-
field.type = mapPostgreSQLDatatypes(field.type, field.length);
36-
});
37-
}
63+
this.showFields(model, function(err, data) {
64+
if (err) return cb(err);
3865
fields = data;
39-
done(err);
40-
}
4166

42-
function decoratedIndexDataCallback(err, data) {
43-
var indexHash = {};
67+
self.showIndexes(model, function(err, data) {
68+
if (err) return cb(err);
69+
indexes = data;
4470

45-
if (err) {
46-
console.log(err);
47-
}
48-
49-
indexes = data;
50-
done(err);
51-
}
52-
53-
this.execute('SELECT column_name AS "column", data_type AS "type", ' +
54-
'is_nullable AS "nullable", character_maximum_length as "length"' // , data_default AS "Default"'
55-
+ ' FROM "information_schema"."columns" WHERE table_name=\'' +
56-
this.table(model) + '\'', decoratedTableDataCallback);
57-
58-
this.execute(
59-
'SELECT t.relname AS "table", i.relname AS "name", ' +
60-
'am.amname AS "type", ix.indisprimary AS "primary", ' +
61-
'ix.indisunique AS "unique", ' +
62-
'ARRAY(SELECT pg_get_indexdef(ix.indexrelid, k + 1, true) ' +
63-
' FROM generate_subscripts(ix.indkey, 1) AS k ' +
64-
' ORDER BY k ) AS "keys", ' +
65-
'ARRAY(SELECT ' +
66-
' CASE ix.indoption[k] & 1 WHEN 1 THEN \'DESC\' ELSE \'ASC\' END ' +
67-
' FROM generate_subscripts(ix.indoption, 1) AS k ' +
68-
' ORDER BY k ' +
69-
') AS "order" ' +
70-
'FROM pg_class t, pg_class i, pg_index ix, pg_am am ' +
71-
'WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND ' +
72-
'i.relam = am.oid AND ' +
73-
't.relkind=\'r\' AND t.relname=\'' +
74-
this.table(model) + '\'', decoratedIndexDataCallback);
71+
if (fields && indexes)
72+
return cb(null, fields, indexes);
73+
});
74+
});
7575
};
7676

7777
/**

0 commit comments

Comments
 (0)