Skip to content

Commit

Permalink
(Issue#45) Scope Configuration doesn't work with Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksm committed Jul 28, 2020
1 parent 49f3848 commit 702f1c9
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 47 deletions.
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),
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);
},

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);

0 comments on commit 702f1c9

Please sign in to comment.