Skip to content

Commit

Permalink
Correctly handle callback after blank automigration
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Mar 11, 2012
1 parent d558dd6 commit f3e80c2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
17 changes: 9 additions & 8 deletions lib/adapters/mongodb.js
Expand Up @@ -24,19 +24,20 @@ exports.initialize = function initializeSchema(schema, callback) {
s.port = parseInt(s.port || '27017', 10);
s.database = s.database || 'test';

schema.adapter = new MongoDB(s, schema, callback);
};

function MongoDB(s, schema, callback) {
this._models = {};
this.collections = {};

var server = new mongodb.Server(s.host, s.port, {});
new mongodb.Db(s.database, server, {}).open(function (err, client) {
if (err) throw err;
this.client = client;
schema.client = client;
schema.adapter = new MongoDB(client);
callback();
});
};

function MongoDB(client) {
this._models = {};
this.client = client;
this.collections = {};
}.bind(this));
}

MongoDB.prototype.define = function (descr) {
Expand Down
1 change: 0 additions & 1 deletion lib/adapters/postgres.js
Expand Up @@ -20,7 +20,6 @@ exports.initialize = function initializeSchema(schema, callback) {
debug: s.debug
});
schema.adapter = new PG(schema.client);
if (s.autoconnect === false) return callback();

schema.adapter.connect(callback);
};
Expand Down
3 changes: 3 additions & 0 deletions lib/sql.js
Expand Up @@ -128,12 +128,15 @@ BaseSQL.prototype.automigrate = function (cb) {
Object.keys(this._models).forEach(function (model) {
wait += 1;
self.dropTable(model, function () {
// console.log('drop', model);
self.createTable(model, function (err) {
// console.log('create', model);
if (err) console.log(err);
done();
});
});
});
if (wait === 0) cb();

function done() {
if (--wait === 0 && cb) {
Expand Down
16 changes: 4 additions & 12 deletions test/common_test.js
Expand Up @@ -29,23 +29,15 @@ Object.keys(schemas).forEach(function (schemaName) {
if (process.env.ONLY && process.env.ONLY !== schemaName) return;
if (process.env.EXCEPT && ~process.env.EXCEPT.indexOf(schemaName)) return;
context(schemaName, function () {
schemas[schemaName].autoconnect = false;
var schema = new Schema(schemaName, schemas[schemaName]);

it('should connect to database', function (test) {
console.log('Connecting:', schemaName);
if (schema.adapter && schema.adapter.connect) {
schema.adapter.connect(function () {
test.done();
});
} else if (!schema.adapter) {
setTimeout(test.done, 1000);
} else {
test.done()
}
if (schema.connected) return test.done();
schema.on('connected', test.done);
});

schema.log = function (a) {
console.log(a);
// console.log(a);
};
testOrm(schema);
if (specificTest[schemaName]) specificTest[schemaName](schema);
Expand Down

0 comments on commit f3e80c2

Please sign in to comment.