Skip to content

Commit

Permalink
fix postgres adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed Jan 7, 2017
1 parent c0ad95a commit fef08c1
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 86 deletions.
36 changes: 19 additions & 17 deletions lib/adapters/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ PG.prototype.create = function (model, data, callback) {

this.query(sql, function (err, info) {
if (err) {
return callback(err);
return callback && callback(err);
}
callback(err, info && info[0] && info[0].id);
return callback && callback(err, info && info[0] && info[0].id);
}.bind(this));
};

Expand Down Expand Up @@ -170,7 +170,7 @@ PG.prototype.update = function (model, filter, data, callback) {
sql += ' SET ' + combined.join(', ');
sql += ' ' + self.buildWhere(filter, self, model);
this.query(sql, function (err, affected) {
callback(err, affected);
return callback && callback(err, affected);
});
};

Expand Down Expand Up @@ -201,12 +201,12 @@ PG.prototype.all = function all(model, filter, callback) {

this.query(sql, function (err, data) {
if (err) {
return callback(err, []);
return callback && callback(err, []);
}
if (filter && filter.include) {
this._models[model].model.include(data, filter.include, callback);
} else {
callback(null, data);
return callback && callback(null, data);
}
}.bind(this));
};
Expand All @@ -228,12 +228,12 @@ PG.prototype.remove = function remove(model, filter, callback) {
var sql = 'DELETE FROM ' + this.tableEscaped(model) + ' ' + this.toFilter(model, filter);
this.query(sql, function (err, data) {
if (err) {
return callback(err, []);
return callback && callback(err, []);
}
if (filter && filter.include) {
this._models[model].model.include(data, filter.include, callback);
} else {
callback(null, data);
return callback && callback(null, data);
}
}.bind(this));
};
Expand Down Expand Up @@ -402,17 +402,17 @@ PG.prototype.toFilter = function (model, filter) {
};

PG.prototype.autoupdate = function (callback) {
var self = this;
var wait = 0;
var self = this, wait = 0;
Object.keys(self._models).forEach(function (model) {
wait += 1;
var indexes = [];
var sql = 'SELECT column_name as "Field", udt_name as "Type", ' +
'is_nullable as "Null", column_default as "Default" ' +
'FROM information_schema.COLUMNS WHERE table_name = \'' + self.table(model) + '\';';
'FROM information_schema.COLUMNS WHERE table_name = "' + self.table(model) + '"';
self.client.query(sql, function (err, fields) {
if(err){
console.log(err);
// console.log('autoupdate err', wait, err.message, sql);
return done && done();
}
if (!err && fields.length) {
fields.forEach(function (field) {
Expand All @@ -422,9 +422,8 @@ PG.prototype.autoupdate = function (callback) {
} else {
self.createTable(model, indexes, done);
}

});
}.bind(self));
});

function done(err) {
if (err) {
Expand All @@ -448,13 +447,13 @@ PG.prototype.isActual = function (cb) {
});
});

function done(err, fields) {
function done(err) {
if (err) {
console.log(err);
}
if (--wait === 0 && cb) {
var actual = (changes.length === 0);
cb(null, actual);
return cb && cb(null, actual);
}
}
};
Expand Down Expand Up @@ -525,8 +524,9 @@ function getPropertiesToModify(model, actualFields) {

function datatypeChanged(propName, oldSettings) {
var newSettings = m.properties[propName];
if (!newSettings)
if (!newSettings) {
return false;
}
return oldSettings.Type.toLowerCase() !== datatype(newSettings);
}

Expand Down Expand Up @@ -611,7 +611,9 @@ function getTableStatus(model, done) {
});
return done && done(err, data);
}
this.query('SELECT column_name as "Field", udt_name as "Type", is_nullable as "Null", column_default as "Default" FROM information_schema.COLUMNS WHERE table_name = \'' + this.table(model) + '\'', decoratedCallback);
this.query('SELECT column_name as "Field", udt_name as "Type", ' +
'is_nullable as "Null", column_default as "Default" ' +
'FROM information_schema.COLUMNS WHERE table_name = \'' + this.table(model) + '\'', decoratedCallback);
}

PG.prototype.propertiesSQL = function (model) {
Expand Down
19 changes: 11 additions & 8 deletions lib/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ BaseSQL.prototype.automigrate = function (cb) {
if (err) {
console.log(err);
}
done();
return done && done();
});
});
});
Expand All @@ -177,7 +177,6 @@ BaseSQL.prototype.createTable = function (model, indexes, cb) {
var self = this, m = self._models[model];
if ('function' === typeof indexes) {
cb = indexes;
indexes = [];
}
var sql = 'CREATE TABLE ' + self.tableEscaped(model) +
' (\n ' + self.propertiesSQL(model) + '\n)';
Expand All @@ -188,24 +187,28 @@ BaseSQL.prototype.createTable = function (model, indexes, cb) {
// sql = 'PRAGMA encoding = "UTF-8"; ' + sql;
} else if (self.name === 'cassandra') {
// add sorting indexes
if(m.settings.orderBy && m.settings.orderBy.columns) {
if (m.settings.orderBy && m.settings.orderBy.columns) {
var oda = m.settings.orderBy;
var odd = oda.direction ? oda.direction.toUpperCase() : 'ASC';
sql += ' WITH CLUSTERING ORDER BY ('+oda.columns+' '+odd+')';
sql += ' WITH CLUSTERING ORDER BY (' + oda.columns + ' ' + odd + ')';
}
}

try {
self.command(sql, function () {
self.command(sql, function (err) {
if (err) {
// console.log('ERROR CREATE TABLE 1: ', model, sql, err);
}
// || self.name === 'cassandra'
if (self.name === 'sqlite3' || self.name === 'mysql') {
self.createIndexes(model, self._models[model], cb)
self.createIndexes(model, self._models[model], cb);
} else {
cb();
return cb && cb();
}
});
} catch (err) {
console.log('ERROR CREATE TABLE: ', self._models[model].properties, err);
// console.log('ERROR CREATE TABLE 2: ', model, sql, err);
return cb && cb();
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports.mariadb = {

module.exports.postgres = {
driver : 'postgres',
host : gitlab ? 'postgres' : '127.0.0.1',
host : gitlab ? 'postgres' : '192.168.99.100',
port : '5432',
username : 'test',
password : 'test',
Expand Down
4 changes: 3 additions & 1 deletion test/model/article-promised.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ describe(driver + ' - Promised Article model:', function () {
var id, newArticle = samples.articles[1];

before(function (done) {
schema.autoupdate(done);
schema.autoupdate(function(){
return done && done();
});
});

after(function (done) {
Expand Down
8 changes: 6 additions & 2 deletions test/model/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ describe(driver + ' - Article model:', function () {
var id, newArticle = samples.articles[1];

before(function (done) {
schema.autoupdate(done);
schema.autoupdate(function(){
return done && done();
});
});

after(function (done) {
Article.destroyAll(done);
Article.destroyAll(function(){
return done && done();
});
});

it('#create', function (done) {
Expand Down
4 changes: 3 additions & 1 deletion test/model/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ describe(driver + ' - Category model:', function () {
var id, newCategory = samples.categories[0];

before(function (done) {
schema.autoupdate(done);
schema.autoupdate(function(){
return done && done();
});
});

after(function (done) {
Expand Down
6 changes: 5 additions & 1 deletion test/model/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ describe(driver + ' - User model:', function () {
var id, newUser = samples.users[0];

before(function (done) {
schema.autoupdate(done);
setTimeout(function() {
schema.autoupdate(function () {
return done && done();
});
}, 500);
});

after(function (done) {
Expand Down
10 changes: 8 additions & 2 deletions test/schema/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ describe(driver + ' - schema hooks:', function () {
var user, nuser, newUser = samples.users[0];

before(function (done) {
schema.autoupdate(done);
setTimeout(function(){
schema.autoupdate(function () {
return done && done();
});
}, 500);
});

after(function (done) {
User.destroyAll(done);
User.destroyAll(function(){
return done && done();
});
});

it("#afterInitialize", function (done) {
Expand Down
34 changes: 19 additions & 15 deletions test/schema/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,30 @@ describe(driver + ' - queries:', function () {
var newCategories = samples.categories, total = newCategories.length;

before(function (done) {
schema.autoupdate(function () {
var cnt = newCategories.length;
if (!cnt) {
done();
}
newCategories.forEach(function (category) {
Category.create(category, function (err) {
if (err) {
console.log(err);
}
if (--cnt === 0) {
done();
}
setTimeout(function(){
schema.autoupdate(function () {
var cnt = newCategories.length;
if (!cnt) {
return done && done();
}
newCategories.forEach(function (category) {
Category.create(category, function (err) {
if (err) {
console.log(err);
}
if (--cnt === 0) {
return done && done();
}
});
});
});
});
}, 500);
});

after(function (done) {
Category.destroyAll(done);
Category.destroyAll(function(){
return done && done();
});
});

describe('#order', function () {
Expand Down
16 changes: 10 additions & 6 deletions test/schema/relation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,21 @@ describe(driver + ' - relation:', function () {
Article.belongsTo(User, {as: 'author', foreignKey: 'create_id'});

before(function (done) {
schema.autoupdate(function () {
user = new User(newUser);
user.save(function () {
done();
setTimeout(function(){
schema.autoupdate(function () {
user = new User(newUser);
user.save(function () {
return done && done();
});
});
});
}, 500);
});

after(function (done) {
User.destroyAll(function () {
Article.destroyAll(done);
Article.destroyAll(function(){
return done && done();
});
});
});

Expand Down
16 changes: 11 additions & 5 deletions test/schema/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ describe(driver + ' - schema scope:', function () {
var category, newCategory = samples.categories[0];

before(function (done) {
category = new Category(newCategory);
schema.autoupdate(function () {
category.save(done);
});
setTimeout(function(){
category = new Category(newCategory);
schema.autoupdate(function () {
category.save(function () {
return done && done();
});
});
}, 500);
});

after(function (done) {
Category.destroyAll(done);
Category.destroyAll(function(){
return done && done();
});
});

describe('#scope', function () {
Expand Down
16 changes: 9 additions & 7 deletions test/schema/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ describe(driver + ' - schema types:', function () {
var id, article, newArticle = samples.articles[1];

before(function (done) {
schema.autoupdate(function () {
Article.create(newArticle, function(err, created){
id = created.id;
Article.findById(id, function(err, found) {
article = found;
done();
setTimeout(function(){
schema.autoupdate(function () {
Article.create(newArticle, function (err, created) {
id = created.id;
Article.findById(id, function (err, found) {
article = found;
return done && done();
});
});
});
});
}, 500);
});

after(function (done) {
Expand Down
12 changes: 7 additions & 5 deletions test/schema/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ describe(driver + ' - schema validation:', function () {
newUser2 = samples.users[0];

before(function (done) {
user1 = new User(newUser1);
user2 = new User(newUser2);
schema.autoupdate(function(){
user1.save(done);
});
setTimeout(function(){
user1 = new User(newUser1);
user2 = new User(newUser2);
schema.autoupdate(function () {
user1.save(done);
});
}, 500);
});

after(function (done) {
Expand Down
Loading

0 comments on commit fef08c1

Please sign in to comment.