Skip to content

Commit

Permalink
[DPE-2278] Add libpq's connection string URI format to uri field in…
Browse files Browse the repository at this point in the history
… relation databag (#545)

* set URI on charm

* test uri in connection

* add await

* debug

* revert test changes
  • Loading branch information
lucasgameiroborges authored Jul 15, 2024
1 parent eeb6145 commit 6bdee4e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/relations/postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
f"{self.charm.primary_endpoint}:{DATABASE_PORT}",
)

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

# Update the read-only endpoint.
self.update_read_only_endpoint(event)

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test_postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def test_on_database_requested(harness):
"username": user,
"password": "test-password",
"read-only-endpoints": "postgresql-k8s-replicas.None.svc.cluster.local:5432",
"uris": f"postgresql://{user}:[email protected]:5432/{DATABASE}",
"version": POSTGRESQL_VERSION,
"database": f"{DATABASE}",
}
Expand All @@ -140,6 +141,7 @@ def test_on_database_requested(harness):
assert harness.get_relation_data(rel_id, harness.charm.app.name) == {
"data": f'{{"database": "{DATABASE}", "extra-user-roles": "{EXTRA_USER_ROLES}"}}',
"endpoints": "postgresql-k8s-primary.None.svc.cluster.local:5432",
"uris": f"postgresql://{user}:[email protected]:5432/{DATABASE}",
"read-only-endpoints": "postgresql-k8s-replicas.None.svc.cluster.local:5432",
}

Expand All @@ -151,6 +153,7 @@ def test_on_database_requested(harness):
"data": f'{{"database": "{DATABASE}", "extra-user-roles": "{EXTRA_USER_ROLES}"}}',
"endpoints": "postgresql-k8s-primary.None.svc.cluster.local:5432",
"read-only-endpoints": "postgresql-k8s-replicas.None.svc.cluster.local:5432",
"uris": f"postgresql://{user}:[email protected]:5432/{DATABASE}",
}

# BlockedStatus due to a PostgreSQLGetPostgreSQLVersionError.
Expand Down

0 comments on commit 6bdee4e

Please sign in to comment.