Try different approach - => _

This commit is contained in:
Peter Stockings
2025-12-24 10:46:42 +11:00
parent 9898eb440d
commit de24d9f78a

15
app.py
View File

@@ -222,23 +222,16 @@ def run_sql_query(container_name: str, query: str) -> dict:
Infers the database name from the Dokku container name. Infers the database name from the Dokku container name.
""" """
try: try:
# Infer DB name: dokku.postgres.my-service -> my-service # Infer DB name: dokku.postgres.my-service -> my-service_db or my_service
# Dokku services follow the naming: dokku.<type>.<service-name> # Dokku typically replaces hyphens with underscores in database names.
parts = container_name.split(".", 2) parts = container_name.split(".", 2)
service_name = parts[2] if len(parts) >= 3 else None service_name = parts[2] if len(parts) >= 3 else None
# In some setups, the DB name might be the service name minus '-db' or '-database' # Most common Dokku pattern: replace - with _
# We'll use the service name but also handle the case where it might be a prefix db_name = service_name.replace("-", "_") if service_name else None
db_name = service_name
if service_name:
# Try to strip common suffixes if they are present
db_name = re.sub(r'-(db|database|postgres|mysql|mariadb|sql)$', '', service_name)
if "postgres" in container_name: if "postgres" in container_name:
# Postgres: use psql with CSV-like output # Postgres: use psql with CSV-like output
# Target the specific database using -d.
# If the user specifically said it's 'function' while service is 'function-db',
# our regex 're.sub' will turn 'function-db' into 'function'.
db_arg = ["-d", db_name] if db_name else [] db_arg = ["-d", db_name] if db_name else []
cmd = [DOCKER, "exec", container_name, "psql", "-U", "postgres"] + db_arg + ["-c", query, "-A", "-F", ","] cmd = [DOCKER, "exec", container_name, "psql", "-U", "postgres"] + db_arg + ["-c", query, "-A", "-F", ","]
elif "mysql" in container_name or "mariadb" in container_name: elif "mysql" in container_name or "mariadb" in container_name: