Skip to content

Commit

Permalink
Make sure that failure to set the defaultSchema results in a connecti…
Browse files Browse the repository at this point in the history
…on error
  • Loading branch information
R. Cijvat committed Feb 16, 2016
1 parent 3c7d163 commit a3a0f81
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
23 changes: 23 additions & 0 deletions Tests/test.js
Expand Up @@ -304,6 +304,29 @@ describe("#Connection", function() {
.that.is.a("string");
});

it("should fail on non existing defaultSchema", function() {
var conn = new MDB({defaultSchema: "non_existant"});
return conn.connect().should.be.rejected;
});

it("should pass on existing defaultSchema", function() {
var conn1 = new MDB();
var conn2 = new MDB({defaultSchema: "some_schema"});
conns.push(conn1);
conns.push(conn2);
conn1.connect();
return conn1.query("START TRANSACTION; " +
"CREATE SCHEMA some_schema; " +
"SET SCHEMA some_schema; " +
"CREATE TABLE a (a INT); " +
"INSERT INTO a VALUES (1); " +
"COMMIT;").then(function() {
return conn2.connect();
}).then(function() {
return conn2.query("SELECT * FROM a");
}).should.eventually.have.property("rows", 1);
});

it("should have the right aliases", function() {
var conn = new MDB();
conn.open.should.equal(conn.connect);
Expand Down
19 changes: 11 additions & 8 deletions src/mapi-connection.js
Expand Up @@ -515,16 +515,19 @@ module.exports = function MapiConnection(options) {
_request('Xreply_size -1', _messageQueue);
_request('Xauto_commit 1', _messageQueue);

// Set the schema, if other than 'sys'
if(options.defaultSchema != 'sys') {
_request(utils.packQuery('SET SCHEMA ' + options.defaultSchema), _messageQueue);
}

// Set the time zone interval, we do not check whether or not that succeeds.
_request(utils.packQuery("SET TIME ZONE INTERVAL '" + options.timezoneOffset + "' MINUTE"), _messageQueue);

// try to execute a simple query, and resolve/reject connection promise
return _request(utils.packQuery('SELECT 42'), _messageQueue).then(function () {
var schemaReq = Q.when(true);
// Set the schema, if other than 'sys'
if(options.defaultSchema != 'sys') {
schemaReq = _request(utils.packQuery('SET SCHEMA ' + options.defaultSchema), _messageQueue);
}
// try to execute a simple query, after the schema has been set (if required at all) and resolve/reject connection promise
return schemaReq.then(function() {
return _request(utils.packQuery('SELECT 42'), _messageQueue);
}).then(function () {
// At this point, the message queue should be empty, since 'select 42' was the
// last request placed by the connect method, and that one has been successfully
// completed.
Expand All @@ -536,9 +539,9 @@ module.exports = function MapiConnection(options) {
_connectDeferred.resolve();
}, function (err) {
if (options.warnings) {
options.warningFn(options.logger, 'Error on executing test query "SELECT 42": ' + err);
options.warningFn(options.logger, 'Error on opening connection: ' + err);
}
_connectDeferred.reject(new Error('Could not connect to MonetDB'));
_connectDeferred.reject(new Error('Could not connect to MonetDB: ' + err));
}).done();
});
_socket.on('data', _onData);
Expand Down

0 comments on commit a3a0f81

Please sign in to comment.