Skip to content

Conversation

@dragomirp
Copy link
Contributor

@dragomirp dragomirp commented Oct 19, 2025

Add support for prefix databases.

Depends on:

Checklist

  • I have added or updated any relevant documentation.
  • I have cleaned any remaining cloud resources from my accounts.

@dragomirp dragomirp added the enhancement New feature, UI change, or workload upgrade label Oct 19, 2025
@codecov
Copy link

codecov bot commented Oct 19, 2025

Codecov Report

❌ Patch coverage is 37.90850% with 95 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.02%. Comparing base (7efe94a) to head (5e240df).

Files with missing lines Patch % Lines
src/relations/postgresql_provider.py 34.02% 75 Missing and 20 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           16/edge    #1238      +/-   ##
===========================================
- Coverage    70.73%   70.02%   -0.72%     
===========================================
  Files           16       16              
  Lines         4043     4134      +91     
  Branches       629      654      +25     
===========================================
+ Hits          2860     2895      +35     
- Misses         991     1032      +41     
- Partials       192      207      +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

if self.charm.unit.is_leader():
return json.loads(self.charm.app_peer_data.get("rel_databases", "{}"))

def _get_credentials(self, event: DatabaseRequestedEvent) -> tuple[str, str] | None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split off, to reduce McCabe.

return
return f"relation-{event.relation.id}", new_password()

def _collect_databases(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split off, to reduce McCabe.

self.add_database_to_prefix_mapping(database)
return database, databases

def _are_units_in_sync(self) -> bool:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split off, to reduce McCabe.

if event.prefix_matching and event.prefix_matching != "all":
logger.warning("Only all prefix matching is supported")
databases = sorted(self.charm.postgresql.list_databases(database[:-1]))
self.set_databases_prefix_mapping(event.relation.id, user, database[:-1], databases)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the initial prefixed DB cache.

else:
databases = [database]
# Add to cached field to be able to generate hba rules
self.add_database_to_prefix_mapping(database)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check and append to existing prefix caches.

Comment on lines +469 to +471
self.database_provides.delete_relation_data(
relation_id, ["uris", "read-only-uris"]
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On prefix DB that matches nothing, unset the URIs, since there's no possible valid value.

Comment on lines +46 to +48
username: str
prefix: str
databases: list[str]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cached prefix metadata. We need to know the username to update Postgresql roles and connect grant, we need to know the prefix, to match against when adding database.

Comment on lines +327 to +329
(dbs := self.get_rel_to_db_mapping())
and (database := dbs.get(str(event.relation.id)))
and database[-1] != "*"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use peer data cache of rel to DBs, since at this point the relation data (and the db name in it) should be gone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@dragomirp dragomirp marked this pull request as ready for review October 27, 2025 15:06
@dragomirp dragomirp requested review from a team and removed request for a team October 27, 2025 15:06
Copy link
Member

@marceloneppel marceloneppel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment on lines +327 to +329
(dbs := self.get_rel_to_db_mapping())
and (database := dbs.get(str(event.relation.id)))
and database[-1] != "*"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature, UI change, or workload upgrade Libraries: OK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants