Use user name as default database when user is non-default (#1679)

Not entirely backwards-compatible.
This commit is contained in:
Charmander 2020-01-23 09:03:50 -08:00 committed by Brian M. Carlson
parent 05c7665138
commit c26caa80d2
4 changed files with 35 additions and 3 deletions

View File

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

View File

@ -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,

View File

@ -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) {

View File

@ -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')