Skip to content

Commit dd72f65

Browse files
author
Lucas Gameiro
authored
add libpq connection string (#527)
1 parent eef3555 commit dd72f65

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/relations/postgresql_provider.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
115115
# Set the database name
116116
self.database_provides.set_database(event.relation.id, database)
117117

118+
# Set connection string URI.
119+
self.database_provides.set_uris(
120+
event.relation.id,
121+
f"postgresql://{user}:{password}@{self.charm.primary_endpoint}:{DATABASE_PORT}/{database}",
122+
)
123+
118124
self._update_unit_status(event.relation)
119125
except (
120126
PostgreSQLCreateDatabaseError,

tests/unit/test_postgresql_provider.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def request_database(_harness):
6262
_harness.update_relation_data(
6363
rel_id,
6464
_harness.charm.app.name,
65-
{"data": "", "username": "", "password": "", "version": "", "database": ""},
65+
{"data": "", "username": "", "password": "", "uris": "", "version": "", "database": ""},
6666
)
6767

6868
# Simulate the request of a new database plus extra user roles.
@@ -94,10 +94,11 @@ def test_on_database_requested(harness):
9494
_member_started.side_effect = [False, True, True, True, True, True]
9595
_primary_endpoint.side_effect = [
9696
None,
97-
{"1.1.1.1"},
98-
{"1.1.1.1"},
99-
{"1.1.1.1"},
100-
{"1.1.1.1"},
97+
"1.1.1.1",
98+
"1.1.1.1",
99+
"1.1.1.1",
100+
"1.1.1.1",
101+
"1.1.1.1",
101102
]
102103
postgresql_mock.create_user = PropertyMock(
103104
side_effect=[None, PostgreSQLCreateUserError, None, None]
@@ -141,6 +142,7 @@ def test_on_database_requested(harness):
141142
"data": f'{{"database": "{DATABASE}", "extra-user-roles": "{EXTRA_USER_ROLES}"}}',
142143
"username": user,
143144
"password": "test-password",
145+
"uris": f"postgresql://{user}:[email protected]:5432/{DATABASE}",
144146
"version": POSTGRESQL_VERSION,
145147
"database": f"{DATABASE}",
146148
}

0 commit comments

Comments
 (0)