mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
Use user name as default database when user is non-default (#1679)
Not entirely backwards-compatible.
This commit is contained in:
parent
05c7665138
commit
c26caa80d2
@ -52,6 +52,11 @@ var ConnectionParameters = function (config) {
|
||||
|
||||
this.user = val('user', config)
|
||||
this.database = val('database', config)
|
||||
|
||||
if (this.database === undefined) {
|
||||
this.database = this.user
|
||||
}
|
||||
|
||||
this.port = parseInt(val('port', config), 10)
|
||||
this.host = val('host', config)
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ module.exports = {
|
||||
user: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
|
||||
|
||||
// name of database to connect
|
||||
database: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
|
||||
database: undefined,
|
||||
|
||||
// database user's password
|
||||
password: null,
|
||||
|
||||
@ -14,7 +14,7 @@ for (var key in process.env) {
|
||||
suite.test('default values are used in new clients', function () {
|
||||
assert.same(pg.defaults, {
|
||||
user: process.env.USER,
|
||||
database: process.env.USER,
|
||||
database: undefined,
|
||||
password: null,
|
||||
port: 5432,
|
||||
rows: 0,
|
||||
@ -54,6 +54,28 @@ suite.test('modified values are passed to created clients', function () {
|
||||
})
|
||||
})
|
||||
|
||||
suite.test('database defaults to user when user is non-default', () => {
|
||||
{
|
||||
pg.defaults.database = undefined
|
||||
|
||||
const client = new Client({
|
||||
user: 'foo',
|
||||
})
|
||||
|
||||
assert.strictEqual(client.database, 'foo')
|
||||
}
|
||||
|
||||
{
|
||||
pg.defaults.database = 'bar'
|
||||
|
||||
const client = new Client({
|
||||
user: 'foo',
|
||||
})
|
||||
|
||||
assert.strictEqual(client.database, 'bar')
|
||||
}
|
||||
})
|
||||
|
||||
suite.test('cleanup', () => {
|
||||
// restore process.env
|
||||
for (var key in realEnv) {
|
||||
|
||||
@ -16,8 +16,13 @@ test('ConnectionParameters construction', function () {
|
||||
})
|
||||
|
||||
var compare = function (actual, expected, type) {
|
||||
const expectedDatabase =
|
||||
expected.database === undefined
|
||||
? expected.user
|
||||
: expected.database
|
||||
|
||||
assert.equal(actual.user, expected.user, type + ' user')
|
||||
assert.equal(actual.database, expected.database, type + ' database')
|
||||
assert.equal(actual.database, expectedDatabase, type + ' database')
|
||||
assert.equal(actual.port, expected.port, type + ' port')
|
||||
assert.equal(actual.host, expected.host, type + ' host')
|
||||
assert.equal(actual.password, expected.password, type + ' password')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user