@@ -11,67 +11,67 @@ var async = require('async');
11
11
module . exports = mixinMigration ;
12
12
13
13
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
+
14
54
/*!
15
55
* Discover the properties from a table
16
56
* @param {String } model The model name
17
57
* @param {Function } cb The callback function
18
58
*/
19
59
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 ;
28
62
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 ) ;
38
65
fields = data ;
39
- done ( err ) ;
40
- }
41
66
42
- function decoratedIndexDataCallback ( err , data ) {
43
- var indexHash = { } ;
67
+ self . showIndexes ( model , function ( err , data ) {
68
+ if ( err ) return cb ( err ) ;
69
+ indexes = data ;
44
70
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
+ } ) ;
75
75
} ;
76
76
77
77
/**
0 commit comments