Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/pg/lib/connection-parameters.js
Original file line number Diff line number Diff line change
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 packages/pg/lib/defaults.js
Original file line number Diff line number Diff line change
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 packages/pg/test/integration/client/configuration-tests.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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