fixing issues with improper queuing on the schema queries

This commit is contained in:
Tim Griesser 2013-05-07 13:31:42 -04:00
parent 4e57b8d5a9
commit cbc25c62fa
7 changed files with 31 additions and 27 deletions

View File

@ -28,6 +28,7 @@ _.extend(MysqlClient.prototype, base.protoProps, {
// Call the querystring and then release the client
conn.query(builder.sql, builder.bindings, function (err, resp) {
if (err) { return dfd.reject(err); }
if (builder._source === 'SchemaBuilder') {
if (builder.type === 'tableExists') {
if (resp.length > 0) return dfd.resolve(_.pick(resp, _.keys(resp)));

View File

@ -455,8 +455,6 @@ SqliteDB('users')
</pre>
<h3>Additional Where Methods:</h3>
<ul class="small">
@ -599,7 +597,7 @@ Knex('users')
<b class="header">del / delete</b><code>.del()</code>
<br />
Aliased to <tt>del</tt> as <tt>delete</tt> is a reserved word in javascript, this method deletes
one or more rows, based on the conditions specified in the query.
one or more rows, based on the other conditions specified in the query.
</p>
<p id="Builder-count">

39
knex.js
View File

@ -1,4 +1,4 @@
// Knex.js 0.1.0
// Knex.js 0.0.0
//
// (c) 2013 Tim Griesser
// Knex may be freely distributed under the MIT license.
@ -23,7 +23,7 @@
Knex.VERSION = '0.0.0';
// Methods common to both the `Grammar` and `SchemaGrammar` interfaces,
// that is used to generate the sql in one form or another.
// used to generate the sql in one form or another.
var Common = {
_debug: false,
@ -1450,24 +1450,29 @@
// Prep the SQL associated with the builder.
builder.sql = builder.toSql();
builder.bindings = builder._cleanBindings();
// Used to handle the schema builder cases, where there is an array of
// sql statements used in the table creation. These definitely need
// to be processed on the same connection.
if (_.isArray(builder.sql)) {
var emptyConnection = !builder._connection;
return Q.resolve(builder._connection || builder.client.getConnection()).then(function(conn) {
builder._connection = conn;
return _.reduce(builder.sql, function(memo, sql) {
return memo.then(function () { builder.client.query(_.extend({}, builder, {sql: sql})); });
}, Q.resolve()).fin(function() {
if (emptyConnection) builder.client.pool.release(conn);
});
});
if (!_.isArray(builder.sql)) builder.sql = [builder.sql];
var chain;
for (var i = 0, l = builder.sql.length; i < l; i++) {
if (chain) {
chain.then(multiQuery(builder, i, chain));
} else {
chain = multiQuery(builder, i);
}
}
// Query on the query builder, which should resolve with a promise.
return builder.client.query(builder);
return chain;
};
// Sets up a multi-query
var multiQuery = function(builder, i, chain) {
if (chain) {
return function() {
return multiQuery(builder, i);
};
}
return builder.client.query(_.extend({}, builder, {sql: builder.sql[i]}));
};
// Knex.Initialize

View File

@ -19,7 +19,7 @@
"generic-pool": "~2.0.3"
},
"scripts": {
"test": "mocha -b -R spec test/index.js"
"test": "mocha -R spec test/index.js"
},
"repository": "https://github.com/tgriesser/knex",
"keywords": [

View File

@ -1,5 +1,5 @@
var Q = require('q');
module.exports = function(Knex, handler, error) {
module.exports = function(Knex, handler, error, type, db) {
var res = null;
return Q.all([

View File

@ -13,8 +13,8 @@ module.exports = function(Knex, type) {
before(function(ok) {
var val = handler(type, 'schema');
require('./lib/schema')(Knex, function() {
setTimeout(function() { ok(); }, 100);
require('./lib/schema')(Knex, function() {
ok();
}, function(err) {
throw new Error(err);
}, type);
@ -83,7 +83,7 @@ var handler = function(instance, section) {
try {
assert.deepEqual(checkData, data);
} catch (e) {
console.log([checkData, data]);
//console.log([checkData, data]);
}
}
item++;

View File

@ -15,7 +15,7 @@ module.exports = function(Knex, type) {
var val = handler(type, 'schema');
require('./lib/schema')(Knex, val(ok, true), function(err) {
throw new Error(err);
});
}, 'String');
});
describe('Knex.Builder', function() {
@ -82,7 +82,7 @@ var handler = function(instance, section) {
try {
assert.deepEqual(a, b);
} catch (e) {
console.log([a, b]);
//console.log([a, b]);
}
}
item++;