Repository navigation
Conversation
make NpgsqlConnection to parse the parameter as a connection URI if it is a URI
remove Category attribute from test code, which was only for debugging during dev
|
What's the latest on this PR? It looks like it hasn't yet been merged, but it sure would be a nice feature to have. I'd love to not have to re-write my heroku connection strings. Oh boy would that be nice. :) |
|
I'm sorry this hasn't received more attention - it definitely does look like a lot of effort has gone into it, and I do think it's a valuable addition to Npgsql. There's just so much going on at the moment - but I promise to try to make some time for this soon. |
|
In the meantime, if anyone else doesn't like manually rewriting heroku connection strings here's a bit of code to pull out the connection string from the heroku environment variable and rewrite it for you. https://gist.github.com/snow-jallen/bbcef9a7c50056e227e066010c043a4c |
|
|
||
| // Connection string hasn't been seen before. Parse it. | ||
| Settings = new NpgsqlConnectionStringBuilder(_connectionString); | ||
| if (Regex.IsMatch(_connectionString, @"^\w+://")) |
There was a problem hiding this comment.
@y-code In #2512 I used the following check: https://github.com/npgsql/npgsql/pull/2512/files#diff-6daf78c3f8b7fabad257e702ed1ef44eR188 - do you think it makes sense to use this here?
There was a problem hiding this comment.
@jgilchrist Thank you for reviewing my PR. Yes, I tried System.Uri at first stage, and recognized that there's several problems with it to cover all types of PostgreSQL connection URIs that are documented in PostgreSQL's documentation. I've listed up the problems with System.Uri in Notes section of my first comment in this PR, so please have a look at it. The Notes section is near the bottom of the comment.
There was a problem hiding this comment.
Ah! Sorry, I missed that part of your notes. Thanks - makes sense.
|
I'm sure glad to see this PR hasn't died or been closed or anything. It'd sure be nice if there were a way to get this wrapped up and into the shipping version of npgsql. Here's hoping. 👍 |
roji
left a comment
There was a problem hiding this comment.
@y-code apologies once again that it's taken me so long to properly review this.
The PR looks very good - you've obviously invested a lot of work in understanding how PG works around these options, writing thorough tests, etc. I'm hoping you're still interested in getting this merged - I'd like to finally do this.
I've done a first round of reviewing, please take a look. If you want to proceed, can you please rebase and address my comments? We'll go from there.
BTW in some cases, you say that Npgsql looks for files in different places compared to PG (passfile, sslcert), can you provide more details on that? We should match the PG behavior on these.
| { | ||
| var convertedValue = p.PropertyType.GetTypeInfo().IsEnum && value is string str | ||
| ? Enum.Parse(p.PropertyType, str) | ||
| ? Enum.Parse(p.PropertyType, str, true) |
There was a problem hiding this comment.
nit:
| ? Enum.Parse(p.PropertyType, str, true) | |
| ? Enum.Parse(p.PropertyType, str, ignoreCase: true) |
| [Description("The optional application name parameter to be sent to the backend during connection initiation")] | ||
| [DisplayName("Application Name")] | ||
| [NpgsqlConnectionStringProperty] | ||
| [NpgsqlConnectionURIParameter("application_name", "fallback_application_name")] |
There was a problem hiding this comment.
Not sure this should have fallback_application_name - it has slightly different behavior compared to application_name (even if we currently don't implement the environment variable).
| [Description("The username to connect with. Not required if using IntegratedSecurity.")] | ||
| [DisplayName("Username")] | ||
| [NpgsqlConnectionStringProperty("User Name", "UserId", "User Id", "UID")] | ||
| [NpgsqlConnectionURIParameter("user", "username", "user_name", "user_id", "uid")] |
There was a problem hiding this comment.
Are all these aliases actually supported by PG? If not, I'm not sure we should introduce our own aliases, what would the value of that be?
This is a general question for other properties too.
| [Description("Controls whether SSL is required, disabled or preferred, depending on server support.")] | ||
| [DisplayName("SSL Mode")] | ||
| [NpgsqlConnectionStringProperty] | ||
| [NpgsqlConnectionURIParameter("sslmode", "ssl")] |
There was a problem hiding this comment.
Note that Npgsql only supports disable/prefer/require, which is a subset of what PG allows. We may want to throw a specific exception informing users about this.
| [Description("The maximum connection pool size.")] | ||
| [DisplayName("Maximum Pool Size")] | ||
| [NpgsqlConnectionStringProperty] | ||
| [NpgsqlConnectionURIParameter("maximum_pool_size")] |
There was a problem hiding this comment.
Rather than manually specifying the snake_case for each option, why not just do the conversion automatically in the attribute when no parameters are given? We even already have NpgsqlSnakeCaseNameTranslator.
| /// </summary> | ||
| public string? Password { get; protected set; } | ||
|
|
||
| public IReadOnlyList<KeyValuePair<string, string?>> Parameters => _parameters; |
There was a problem hiding this comment.
Doesn't seem to be actually used - we can remove it.
| Database = Uri.UnescapeDataString(path); | ||
|
|
||
| _parameters = uri.Query | ||
| .Split(new[] { '?', '&' }) |
|
|
||
| namespace Npgsql.Tests | ||
| { | ||
| public class ConnectionUriTests : TestBase |
There was a problem hiding this comment.
I think TestBase can be removed, this is a pure unit test suite (TestBase is about utilities and stuff for actually connecting to PG and executing queries).
| } | ||
|
|
||
| [Test] | ||
| public void InvalidConnectionUri() |
There was a problem hiding this comment.
nit: if you want, InvalidArgument and InvalidConnectionURi can be a single parameterized test, where each TestCase gives the URI and the expected exception
| } | ||
|
|
||
| [Test] | ||
| public void ValidConnectionUri3() |
There was a problem hiding this comment.
A lot of these can be collapsed to a single parameterized test, accepting a URI and, e.g. a 5-value tuple or something. I'd look for opportunities to reduce code here for maintainability.
|
@roji Thanks for reviewing it. I'm glad to hear from you. |
|
any plans here? |
|
I for one am definitely looking forward to this feature. :) |
|
@y-code do you intent to continue working on this? |
|
Will this ever be merged? 3 years old, basic feature, ... At this point, maybe just provide a converter to parse "postgres://" stuff into db-context style "host=". |
|
I'm going to go ahead and close this PR, as the submitter hasn't addressed the review for a few years now. I'd be happy to review a new PR. |
resolves #2090
Based on the comments in #2090, I assumed the following requirements and designed the specification as follows.
However, a couple of questions remained. Please refer the questions on the bottom and give me an advice.
Requirements
When initializing NpgsqlConnection with a Connection URI, Npgsql should behave as defined in PostgreSQL's documentation. However, the following features are not supported at this phase.
High-Level Specification
Refer PostgreSQL's documentation.
For the individual behaviours which the document does not specify, Npgsql conforms to the behaviour of psql. Those are:
URI Parameters
The followings are specification of connection URI parameters in Npgsql.
Basic Connection
Security and Encryption
* The default in Connection String is "Disabled"
* Not every the SSL modes defined in connection URI are supported. Supported values in Npgsql are "disable", "perfer" and "require".
The default path in Npgsql is different from the definition in connection URI
Pooling
Timeouts
Entity Framework
Advanced
* What should the default value be? If it is defaulted to 0, property keepalives's default value, which is 1 (enabled), cannot be effective.
* The unit of this parameter is "seconds" in Connection URI but it is "milliseconds" in Connection String
Compatibility
Unsupported Parameters
The following URI parameters are defined in PostgreSQL's documentation but Npgsql does not have any settings corresponding to them.
Deprecated Paramters in Connection URI
The following URI parameters are deprecated in PostgreSQL's documentation and Npgsql does not support those.
Notes
In this implementation, I handled the host component of Connection URI separately from the parse by System.Uri. It is because System.Uri throws an exception with such URI as described below.
Questions