-
Notifications
You must be signed in to change notification settings - Fork 580
fix(knex): connection attrs missing if connectionString used #1932
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,3 +88,16 @@ export const extractTableName = (builder: any): string => { | |
} | ||
return table; | ||
}; | ||
|
||
export const parseConnectionString = (connectionString: string | undefined) => { | ||
if (!connectionString) { | ||
return undefined; | ||
} | ||
const url = new URL(connectionString); | ||
return { | ||
host: url.hostname, | ||
port: parseInt(url.port || '5432', 10), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm interested in this pull request getting merged, so I thought I'd add some notes. It looks like I think you can check the URL protocol which should be
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the comment. Yes I can do that. However we cannot then cover the case of CockroachDB which uses I will see how knex handles redshift connection string when port is not provided and try to match that functionality. In the end that is the right thing to do. |
||
user: url.username, | ||
database: url.pathname.replace(/^\//, ''), | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import assert = require('assert'); | ||
import { parseConnectionString } from '../src/utils'; | ||
|
||
describe('utils', () => { | ||
describe('parseConnectionString', () => { | ||
it('should return undefined if connectionString is undefined', () => { | ||
const connection = parseConnectionString(undefined); | ||
|
||
assert.strictEqual(connection, undefined); | ||
}); | ||
|
||
it('should return object with connection properties', () => { | ||
const connection = parseConnectionString( | ||
'postgres://user:password@localhost:5555/mydb' | ||
); | ||
|
||
assert.deepEqual(connection, { | ||
host: 'localhost', | ||
port: 5555, | ||
user: 'user', | ||
database: 'mydb', | ||
}); | ||
}); | ||
|
||
it('should assume default port of 5432 if not provided', () => { | ||
const connection = parseConnectionString( | ||
'postgres://user@localhost/mydb' | ||
); | ||
|
||
assert.deepEqual(connection, { | ||
host: 'localhost', | ||
port: 5432, | ||
user: 'user', | ||
database: 'mydb', | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could pass the entire config.connection here and return the resulting values instead of having undefined checks in the attribute assignment further down?