Skip to content

Commit 2a19233

Browse files
committed
showFields patch merge
1 parent 9d901b3 commit 2a19233

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

lib/migration.js

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,78 @@ function mixinMigration(PostgreSQL) {
2525
}
2626
options = options || {};
2727

28-
const sql = 'SELECT column_name AS "column", data_type AS "type", ' +
29-
'datetime_precision AS time_precision, ' +
30-
'is_nullable AS "nullable", character_maximum_length as "length"' // , data_default AS "Default"'
31-
+ ' FROM "information_schema"."columns" WHERE table_name=\'' +
32-
this.table(model) + '\' and table_schema=\'' +
33-
this.schema(model) + '\'';
34-
this.execute(sql, [], options, function(err, fields) {
28+
29+
const sql =
30+
'SELECT column_name AS "column", data_type AS "type", ' +
31+
'datetime_precision AS time_precision, ' +
32+
'is_nullable AS "nullable", character_maximum_length as "length" , numeric_precision, numeric_scale' + // , data_default AS "Default"'
33+
' FROM "information_schema"."columns" WHERE table_name=\'' +
34+
this.table(model) +
35+
"' and table_schema='" +
36+
this.schema(model) +
37+
"'";
38+
39+
connector.execute(sql, [], options, function (err, fields) {
3540
if (err) {
3641
return cb(err);
3742
} else {
38-
fields.forEach(function(field) {
39-
field.type = mapPostgreSQLDatatypes(field.type, field.length, field.time_precision);
43+
fields.forEach(function (field) {
44+
const type = field.type.toUpperCase();
45+
46+
let strPrecision = '';
47+
48+
if (field.time_precision < 6) {
49+
// default is 6
50+
strPrecision = '(' + field.time_precision + ') ';
51+
}
52+
53+
switch (type) {
54+
case 'CHARACTER VARYING':
55+
case 'VARCHAR':
56+
field.type = field.length ? 'VARCHAR(' + field.length + ')' : 'VARCHAR(1024)';
57+
break;
58+
case 'NUMERIC':
59+
field.type = 'NUMERIC(' + field.numeric_precision + ',' + field.numeric_scale + ')';
60+
break;
61+
case 'TIMESTAMP WITHOUT TIME ZONE':
62+
field.type = 'TIMESTAMP ' + strPrecision + 'WITHOUT TIME ZONE';
63+
break;
64+
case 'TIMESTAMP WITH TIME ZONE':
65+
field.type = 'TIMESTAMP ' + strPrecision + 'WITH TIME ZONE';
66+
break;
67+
case 'TIME WITHOUT TIME ZONE':
68+
field.type = 'TIME ' + strPrecision + 'WITHOUT TIME ZONE';
69+
break;
70+
case 'TIME WITH TIME ZONE':
71+
field.type = 'TIME ' + strPrecision + 'WITH TIME ZONE';
72+
break;
73+
default:
74+
field.type = field.type;
75+
}
4076
});
4177
cb(err, fields);
4278
}
4379
});
4480
};
4581

82+
// const sql = 'SELECT column_name AS "column", data_type AS "type", ' +
83+
// 'datetime_precision AS time_precision, ' +
84+
// 'is_nullable AS "nullable", character_maximum_length as "length"' // , data_default AS "Default"'
85+
// + ' FROM "information_schema"."columns" WHERE table_name=\'' +
86+
// this.table(model) + '\' and table_schema=\'' +
87+
// this.schema(model) + '\'';
88+
// this.execute(sql, [], options, function(err, fields) {
89+
// if (err) {
90+
// return cb(err);
91+
// } else {
92+
// fields.forEach(function(field) {
93+
// field.type = mapPostgreSQLDatatypes(field.type, field.length, field.time_precision);
94+
// });
95+
// cb(err, fields);
96+
// }
97+
// });
98+
};
99+
46100
PostgreSQL.prototype.showIndexes = function(model, options, cb) {
47101

48102
if ((!cb) && typeof options === 'function') {

0 commit comments

Comments
 (0)