Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/provably/handoff/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _onboard_database(current_org: str, middleware_id: str, postgres_url: str) -
# padding step ran above to ensure provably_intercepts exists, and the backend's
# schema introspection will pick it up so discover_intercepts_table() can find it.
_log.info("database_already_exists_reusing")
database_id = resolve_existing_database_id(current_org, middleware_id, db_name)
database_id = resolve_existing_database_id(current_org, middleware_id, db_name, body["uri"])
Comment thread
aural-psynapse marked this conversation as resolved.
if not database_id:
log_failed_response(resp)
raise RuntimeError("Database exists but could not resolve existing database_id")
Expand Down
26 changes: 23 additions & 3 deletions src/provably/handoff/_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def discover_intercepts_table(org_id: str, middleware_id: str, database_id: str)
return _node_to_bundle(node)


def resolve_existing_database_id(org_id: str, middleware_id: str, db_name: str) -> str:
def resolve_existing_database_id(org_id: str, middleware_id: str, db_name: str, host: str = "") -> str:
candidates: list[str] = []
for path in (
f"/api/v1/organizations/{org_id}/middlewares/{middleware_id}/databases",
f"/api/v1/organizations/{org_id}/databases",
Expand All @@ -43,15 +44,34 @@ def resolve_existing_database_id(org_id: str, middleware_id: str, db_name: str)
db_id = extract_id(item, ["id", "database_id"])
except ValueError:
continue
if db_id:
return db_id
if db_id and db_id not in candidates:
candidates.append(db_id)
# Same name can map to several registrations on different hosts; pick the one whose
# stored host matches `host` so we don't bind to a different physical DB.
if host:
matched = [db_id for db_id in candidates if _database_host(org_id, middleware_id, db_id) == host]
if matched:
return matched[0]
if len(candidates) > 1:
return "" # ambiguous and none matched the host — refuse to guess
if candidates:
return candidates[0]
Comment thread
aural-psynapse marked this conversation as resolved.
try:
data = get_json(f"/api/v1/organizations/{org_id}/data")
return find_first_id(data, ("database_id",))
except Exception: # noqa: BLE001
return ""


def _database_host(org_id: str, middleware_id: str, database_id: str) -> str:
"""Stored connection host for a database (the list endpoint omits it; the detail one has it)."""
try:
detail = get_json(f"/api/v1/organizations/{org_id}/middlewares/{middleware_id}/databases/{database_id}")
except Exception: # noqa: BLE001
return ""
return str(detail.get("uri") or "").strip()


def resolve_existing_collection_id(org_id: str, middleware_id: str, database_id: str, table_id: str) -> str:
try:
payload = get_json(f"/api/v1/organizations/{org_id}/collections")
Expand Down
Loading