Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Issue#45) Scope Configuration doesn't work with Postgres #54

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,17 @@ var PgDriver = Base.extend({
if (typeof options === 'object') {
if (typeof options.database === 'string') {
this.log.info(
'Ignore database option, not available with postgres. Use schema instead!'
'Ignore database option, not available with postgres. Use schema instead!'
);
}
if (typeof options.schema === 'string') {
this.runSql(
util.format('SET search_path TO `%s`', options.database),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will certainly break a lot of dbs connected using the pg wire protocol, so that can't just be overwritten here (also in pg dbs exist though

util.format('SET search_path TO %s,%s', options.schema, this.schema),
callback
);
}
} else if (typeof options === 'string') {
this.runSql(util.format('SET search_path TO `%s`', options), callback);
this.runSql(util.format('SET search_path TO %s,%s', options, this.schema), callback);
} else callback(null);
},

Expand Down
150 changes: 106 additions & 44 deletions test/pg_schema_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,59 +150,121 @@ vows
}
}
})
.addBatch({
'create schema and connect': {
topic: function() {
var callback = this.callback;
var client = new pg.Client(config);
.addBatch({
'create schema and connect': {
topic: function() {
var callback = this.callback;
var client = new pg.Client(config);

client.connect(function(err) {
if (err) {
return callback(err);
}
client.query('CREATE SCHEMA test_schema', function(err) {
driver.connect(
config,
internals,
function(err, db) {
callback(err, db, client);
}
);
client.connect(function(err) {
if (err) {
return callback(err);
}
client.query('CREATE SCHEMA test_schema', function(err) {
driver.connect(
config,
internals,
function(err, db) {
callback(err, db, client);
}
);
});
});
});
},
},

'migrations table': {
topic: function(db, client) {
'migrations table': {
topic: function(db, client) {
var callback = this.callback;

db.createMigrationsTable(function() {
client.query(
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'test_schema' AND table_name = 'migrations'",
function(err, result) {
callback(err, result);
}
);
});
},

'is in test_schema': function(err, result) {
assert.isNull(err);
assert.isNotNull(result);
assert.equal(result.rowCount, 1);
}
},

teardown: function(db, client) {
var callback = this.callback;
client.query('DROP SCHEMA test_schema CASCADE', function(err) {
if (err) {
return callback(err);
}
client.end();
callback();
});
}
}
})
.addBatch({
'switch database': {
topic: function() {
var callback = this.callback;
var client = new pg.Client(config);

db.createMigrationsTable(function() {
client.query(
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'test_schema' AND table_name = 'migrations'",
function(err, result) {
callback(err, result);
}
);
client.connect(function(err) {
if (err) {
return callback(err);
}

client.query('CREATE SCHEMA test_schema', function(err) {
driver.connect(
config,
internals,
function(err, db) {
client.query("CREATE SCHEMA switch_schema",
function(err, result) {
db.switchDatabase({schema: 'switch_schema'}, function(err) {
callback(err, db, client);
})
}
);
}
);
});
});
},

'is in test_schema': function(err, result) {
assert.isNull(err);
assert.isNotNull(result);
assert.equal(result.rowCount, 1);
}
},
'migrations table': {
topic: function(db, client) {
var callback = this.callback;

teardown: function(db, client) {
var callback = this.callback;
client.query('DROP SCHEMA test_schema CASCADE', function(err) {
if (err) {
return callback(err);
db.createMigrationsTable(function() {
client.query(
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'test_schema' AND table_name = 'migrations'",
function(err, result) {
callback(err, result);
}
);
});
},

'is in test_schema': function(err, result) {
assert.isNull(err);
assert.isNotNull(result);
assert.equal(result.rowCount, 1);
}
client.end();
callback();
});
},

teardown: function(db, client) {
var callback = this.callback;
client.query('DROP SCHEMA test_schema, switch_schema CASCADE', function(err) {
if (err) {
return callback(err);
}
client.end();
callback();
});
}
}
}
})
})
.export(module);