diff --git a/index.js b/index.js index dd262b4..758d67b 100644 --- a/index.js +++ b/index.js @@ -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), + util.format('SET search_path TO %s', options.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', options), callback); } else callback(null); }, diff --git a/test/pg_schema_test.js b/test/pg_schema_test.js index c283a52..86ce3e2 100644 --- a/test/pg_schema_test.js +++ b/test/pg_schema_test.js @@ -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);