Skip to content

Commit

Permalink
add libpq connection string (canonical#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgameiroborges authored Jul 15, 2024
1 parent eef3555 commit dd72f65
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/relations/postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
# Set the database name
self.database_provides.set_database(event.relation.id, database)

# Set connection string URI.
self.database_provides.set_uris(
event.relation.id,
f"postgresql://{user}:{password}@{self.charm.primary_endpoint}:{DATABASE_PORT}/{database}",
)

self._update_unit_status(event.relation)
except (
PostgreSQLCreateDatabaseError,
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/test_postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def request_database(_harness):
_harness.update_relation_data(
rel_id,
_harness.charm.app.name,
{"data": "", "username": "", "password": "", "version": "", "database": ""},
{"data": "", "username": "", "password": "", "uris": "", "version": "", "database": ""},
)

# Simulate the request of a new database plus extra user roles.
Expand Down Expand Up @@ -94,10 +94,11 @@ def test_on_database_requested(harness):
_member_started.side_effect = [False, True, True, True, True, True]
_primary_endpoint.side_effect = [
None,
{"1.1.1.1"},
{"1.1.1.1"},
{"1.1.1.1"},
{"1.1.1.1"},
"1.1.1.1",
"1.1.1.1",
"1.1.1.1",
"1.1.1.1",
"1.1.1.1",
]
postgresql_mock.create_user = PropertyMock(
side_effect=[None, PostgreSQLCreateUserError, None, None]
Expand Down Expand Up @@ -141,6 +142,7 @@ def test_on_database_requested(harness):
"data": f'{{"database": "{DATABASE}", "extra-user-roles": "{EXTRA_USER_ROLES}"}}',
"username": user,
"password": "test-password",
"uris": f"postgresql://{user}:[email protected]:5432/{DATABASE}",
"version": POSTGRESQL_VERSION,
"database": f"{DATABASE}",
}
Expand Down

0 comments on commit dd72f65

Please sign in to comment.