-
Notifications
You must be signed in to change notification settings - Fork 0
feat(java): support postgres URI forms for DATABASE_URL #369
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,17 +10,86 @@ | |
| class DataSourceConfigTest { | ||
|
|
||
| @Test | ||
| void hikariConfig_ShouldReturnConfiguredHikariConfig() throws Exception { | ||
| void hikariConfig_ShouldUseSpringDatasourceUrlAsIs() throws Exception { | ||
| DataSourceConfig config = new DataSourceConfig(); | ||
|
|
||
| setField(config, "jdbcUrl", "jdbc:postgresql://localhost:5432/lamp"); | ||
| setField(config, "springDatasourceUrl", "postgresql://localhost:5432/lamp"); | ||
| setField(config, "databaseUrl", "postgres://ignored:5432/ignored"); | ||
| setField(config, "fallbackJdbcUrl", "jdbc:postgresql://localhost:5432/fallback"); | ||
| setField(config, "username", "user"); | ||
| setField(config, "password", "pass"); | ||
| setField(config, "driverClassName", "org.postgresql.Driver"); | ||
|
|
||
| HikariConfig result = config.hikariConfig(); | ||
|
|
||
| assertThat(result.getJdbcUrl()).isEqualTo("postgresql://localhost:5432/lamp"); | ||
| assertThat(result.getUsername()).isEqualTo("user"); | ||
| assertThat(result.getPassword()).isEqualTo("pass"); | ||
| assertThat(result.getDriverClassName()).isEqualTo("org.postgresql.Driver"); | ||
| } | ||
|
|
||
| @Test | ||
| void hikariConfig_ShouldNormalizeDatabaseUrlPostgresqlScheme() throws Exception { | ||
| DataSourceConfig config = new DataSourceConfig(); | ||
|
|
||
| setField(config, "springDatasourceUrl", ""); | ||
| setField(config, "databaseUrl", "postgresql://localhost:5432/lamp"); | ||
| setField(config, "fallbackJdbcUrl", "jdbc:postgresql://localhost:5432/fallback"); | ||
| setField(config, "username", "user"); | ||
| setField(config, "password", "pass"); | ||
| setField(config, "driverClassName", "org.postgresql.Driver"); | ||
|
|
||
| HikariConfig result = config.hikariConfig(); | ||
|
|
||
| assertThat(result.getJdbcUrl()).isEqualTo("jdbc:postgresql://localhost:5432/lamp"); | ||
|
Comment on lines
+32
to
44
|
||
| } | ||
|
|
||
| @Test | ||
| void hikariConfig_ShouldNormalizeDatabaseUrlPostgresScheme() throws Exception { | ||
| DataSourceConfig config = new DataSourceConfig(); | ||
|
|
||
| setField(config, "springDatasourceUrl", ""); | ||
| setField(config, "databaseUrl", "postgres://localhost:5432/lamp"); | ||
| setField(config, "fallbackJdbcUrl", "jdbc:postgresql://localhost:5432/fallback"); | ||
| setField(config, "username", "user"); | ||
| setField(config, "password", "pass"); | ||
| setField(config, "driverClassName", "org.postgresql.Driver"); | ||
|
|
||
| HikariConfig result = config.hikariConfig(); | ||
|
|
||
| assertThat(result.getJdbcUrl()).isEqualTo("jdbc:postgresql://localhost:5432/lamp"); | ||
| } | ||
|
|
||
| @Test | ||
| void hikariConfig_ShouldKeepJdbcDatabaseUrlUnchanged() throws Exception { | ||
| DataSourceConfig config = new DataSourceConfig(); | ||
|
|
||
| setField(config, "springDatasourceUrl", ""); | ||
| setField(config, "databaseUrl", "jdbc:postgresql://localhost:5432/lamp"); | ||
| setField(config, "fallbackJdbcUrl", "jdbc:postgresql://localhost:5432/fallback"); | ||
| setField(config, "username", "user"); | ||
| setField(config, "password", "pass"); | ||
| setField(config, "driverClassName", "org.postgresql.Driver"); | ||
|
|
||
| HikariConfig result = config.hikariConfig(); | ||
|
|
||
| assertThat(result.getJdbcUrl()).isEqualTo("jdbc:postgresql://localhost:5432/lamp"); | ||
| } | ||
|
|
||
| @Test | ||
| void hikariConfig_ShouldFallbackToSpringDatasourceProperty() throws Exception { | ||
| DataSourceConfig config = new DataSourceConfig(); | ||
|
|
||
| setField(config, "springDatasourceUrl", ""); | ||
| setField(config, "databaseUrl", ""); | ||
| setField(config, "fallbackJdbcUrl", "jdbc:postgresql://localhost:5432/fallback"); | ||
| setField(config, "username", "user"); | ||
| setField(config, "password", "pass"); | ||
| setField(config, "driverClassName", "org.postgresql.Driver"); | ||
|
|
||
| HikariConfig result = config.hikariConfig(); | ||
|
|
||
| assertThat(result.getJdbcUrl()).isEqualTo("jdbc:postgresql://localhost:5432/fallback"); | ||
| assertThat(result.getUsername()).isEqualTo("user"); | ||
| assertThat(result.getPassword()).isEqualTo("pass"); | ||
| assertThat(result.getDriverClassName()).isEqualTo("org.postgresql.Driver"); | ||
|
|
||
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.
normalizeDatabaseUrl() currently only rewrites the URI scheme by prefixing/replacing strings. If DATABASE_URL is a full Postgres URI with userinfo (e.g. postgresql://user:pass@host:5432/db?sslmode=require), the resulting JDBC URL (jdbc:postgresql://user:pass@host:5432/db...) is not a valid PostgreSQL JDBC URL and will fail to connect. Consider parsing the URI (e.g., via java.net.URI) and constructing a proper jdbc:postgresql://host:port/db URL, mapping user/password into either Hikari username/password or query parameters, and preserving query params safely (including URL-decoding/encoding as needed).
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.
Thanks for the review. This PR intentionally scopes compatibility to scheme normalization only (
postgresql://andpostgres://->jdbc:postgresql://) forDATABASE_URL, while leavingSPRING_DATASOURCE_URLbehavior unchanged. Handling full URI parsing (userinfo/query remapping) is a valid enhancement, but it is out of scope for this change and we can track it as a follow-up.