Skip to content

Commit 2b82a34

Browse files
author
Raymond Feng
committed
Fix autoupdate
1 parent 1befb7a commit 2b82a34

File tree

3 files changed

+27
-173
lines changed

3 files changed

+27
-173
lines changed

lib/postgresql.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,8 @@ function getTableStatus(model, cb) {
836836
cb(err, data);
837837
}
838838

839-
this.query('SELECT column_name AS "column", data_type AS "type", nullable AS "nullable"' // , data_default AS "Default"'
840-
+ ' FROM "information_schema"."tables" WHERE table_name=\'' + this.table(model) + '\'', decoratedCallback);
839+
this.query('SELECT column_name AS "column", data_type AS "type", is_nullable AS "nullable"' // , data_default AS "Default"'
840+
+ ' FROM "information_schema"."columns" WHERE table_name=\'' + this.table(model) + '\'', decoratedCallback);
841841

842842
}
843843

@@ -956,7 +956,11 @@ function getAddModifyColumns(model, actualFields) {
956956
var sql = [];
957957
var self = this;
958958
sql = sql.concat(getColumnsToAdd.call(self, model, actualFields));
959-
sql = sql.concat(getPropertiesToModify.call(self, model, actualFields));
959+
var drops = getPropertiesToModify.call(self, model, actualFields);
960+
if(drops.length > 0) {
961+
sql = sql.concat(', ');
962+
sql = sql.concat(drops);
963+
}
960964
// sql = sql.concat(getColumnsToDrop.call(self, model, actualFields));
961965
return sql;
962966
}
@@ -977,11 +981,11 @@ function getColumnsToAdd(model, actualFields) {
977981
if (self.id(model, propName)) return;
978982
var found = searchForPropertyInActual.call(self, model, self.column(model, propName), actualFields);
979983
if (!found && propertyHasNotBeenDeleted.call(self, model, propName)) {
980-
sql.push(addPropertyToActual.call(self, model, propName));
984+
sql.push('ADD COLUMN ' + addPropertyToActual.call(self, model, propName));
981985
}
982986
});
983987
if (sql.length > 0) {
984-
sql = ['ADD', '(' + sql.join(',') + ')'];
988+
sql = [sql.join(', ')];
985989
}
986990
return sql;
987991
}
@@ -1018,17 +1022,18 @@ function getPropertiesToModify(model, actualFields) {
10181022
found = searchForPropertyInActual.call(self, model, propName, actualFields);
10191023
if (found && propertyHasNotBeenDeleted.call(self, model, propName)) {
10201024
if (datatypeChanged(propName, found)) {
1021-
sql.push(modifyDatatypeInActual.call(self, model, propName));
1025+
sql.push('ALTER COLUMN ' + modifyDatatypeInActual.call(self, model, propName));
10221026
}
10231027
if (nullabilityChanged(propName, found)) {
1024-
sql.push(modifyNullabilityInActual.call(self, model, propName));
1028+
sql.push('ALTER COLUMN' + modifyNullabilityInActual.call(self, model, propName));
10251029
}
10261030
}
10271031
});
10281032

1029-
if (sql.length > 0) {
1030-
sql = ['MODIFY', '(' + sql.join(',') + ')'];
1033+
if(sql.length >0) {
1034+
sql = [sql.join(', ')];
10311035
}
1036+
10321037
return sql;
10331038

10341039
function datatypeChanged(propName, oldSettings) {
@@ -1053,7 +1058,7 @@ function getPropertiesToModify(model, actualFields) {
10531058

10541059
function modifyDatatypeInActual(model, propName) {
10551060
var self = this;
1056-
var sqlCommand = self.columnEscaped(model, propName) + ' ' + self.columnDataType(model, propName);
1061+
var sqlCommand = self.columnEscaped(model, propName) + ' TYPE ' + self.columnDataType(model, propName);
10571062
return sqlCommand;
10581063
}
10591064

@@ -1077,11 +1082,11 @@ function getColumnsToDrop(model, actualFields) {
10771082
return;
10781083
}
10791084
if (actualFieldNotPresentInModel(actualField, model)) {
1080-
sql.push(self.escapeName(actualField.column));
1085+
sql.push('DROP COLUMN ' + self.escapeName(actualField.column));
10811086
}
10821087
});
1083-
if (sql.length > 0) {
1084-
sql = ['DROP', '(' + sql.join(',') + ')'];
1088+
if(sql.length >0) {
1089+
sql = [sql.join(', ')];
10851090
}
10861091
return sql;
10871092

@@ -1203,7 +1208,7 @@ PostgreSQL.prototype.disconnect = function disconnect(cb) {
12031208
}
12041209
var conn = this.connection;
12051210
this.connection = null;
1206-
conn.close(); // This is sync
1211+
conn.end(); // This is sync
12071212
}
12081213

12091214
if (cb) {

test/postgresql.discover.sync.test.js

-151
This file was deleted.

test/postgresql.mapping.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var async = require('async');
66
var db;
77

88
before(function () {
9-
db = getDataSource();
9+
db = getSchema();
1010
});
1111

1212
describe('Mapping models', function () {
@@ -25,28 +25,28 @@ describe('Mapping models', function () {
2525
/*
2626
"id": {
2727
"type": "String", "required": true, "length": 20, "id": 1, "postgresql": {
28-
"columnName": "INVENTORY_ID", "dataType": "VARCHAR2", "nullable": "N"
28+
"columnName": "INVENTORY_ID", "dataType": "VARCHAR", "nullable": "N"
2929
}
3030
},
3131
*/
3232
"productId": {
3333
"type": "String", "required": true, "length": 20, "id": 1, "postgresql": {
34-
"columnName": "PRODUCT_ID", "dataType": "VARCHAR2", "nullable": "N"
34+
"columnName": "PRODUCT_ID", "dataType": "VARCHAR", "nullable": "N"
3535
}
3636
},
3737
"locationId": {
3838
"type": "String", "required": true, "length": 20, "id": 2, "postgresql": {
39-
"columnName": "LOCATION_ID", "dataType": "VARCHAR2", "nullable": "N"
39+
"columnName": "LOCATION_ID", "dataType": "VARCHAR", "nullable": "N"
4040
}
4141
},
4242
"available": {
43-
"type": "Number", "required": false, "length": 22, "postgresql": {
44-
"columnName": "AVAILABLE", "dataType": "NUMBER", "nullable": "Y"
43+
"type": "Number", "required": false, "postgresql": {
44+
"columnName": "AVAILABLE", "dataType": "INTEGER", "nullable": "Y"
4545
}
4646
},
4747
"total": {
48-
"type": "Number", "required": false, "length": 22, "postgresql": {
49-
"columnName": "TOTAL", "dataType": "NUMBER", "nullable": "Y"
48+
"type": "Number", "required": false, "postgresql": {
49+
"columnName": "TOTAL", "dataType": "INTEGER", "nullable": "Y"
5050
}
5151
}
5252
}

0 commit comments

Comments
 (0)