Skip to content

Commit

Permalink
Use user name as default database when user is non-default
Browse files Browse the repository at this point in the history
Not entirely backwards-compatible.
  • Loading branch information
charmander committed Jun 20, 2018
1 parent 3ac356a commit 610ecd2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/connection-parameters.js
Expand Up @@ -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)
this.password = val('password', config)
Expand Down
2 changes: 1 addition & 1 deletion lib/defaults.js
Expand Up @@ -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,
Expand Down
24 changes: 23 additions & 1 deletion test/integration/client/configuration-tests.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion test/unit/connection-parameters/creation-tests.js
Expand Up @@ -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')
Expand Down

0 comments on commit 610ecd2

Please sign in to comment.