import os
from urllib.parse import urlunparse, quote
[docs]
def uri_dict_to_str(uri_dict):
"""
Convert a dictionary of URI components to a connection string.
Standardized to handle local SQLite and FileStore robustness.
"""
scheme = uri_dict["driver"]
# Robust handling for local file-based backends (sqlite or mlflow file)
if scheme in ["sqlite", "file"]:
path = uri_dict.get("path") or uri_dict.get("db_name", "")
# Ensure absolute path resolution
abs_path = os.path.abspath(path)
if scheme == "sqlite":
# Add busy timeout for SQLite concurrency
return f"sqlite:///{abs_path}?timeout=60"
return f"file://{abs_path}"
host = uri_dict["host"]
port = uri_dict["port"]
user = uri_dict.get("user")
password = uri_dict.get("pass")
db_name = uri_dict.get("db_name", "")
# Construct netloc
netloc = f"{host}:{port}"
if user:
# Encode user and password components
user_encoded = quote(user, safe="")
if password:
pass_encoded = quote(password, safe="")
netloc = f"{user_encoded}:{pass_encoded}@{netloc}"
else:
netloc = f"{user_encoded}@{netloc}"
# Path
if db_name:
path = f"/{db_name}"
else:
path = "/"
return urlunparse((scheme, netloc, path, "", "", ""))