From 9898eb440dab00aa5e7a09644311b18ce7db9581 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Wed, 24 Dec 2025 10:41:41 +1100 Subject: [PATCH] Strip '-db' from database names --- app.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 7ccc617..70b9cf4 100644 --- a/app.py +++ b/app.py @@ -225,16 +225,24 @@ def run_sql_query(container_name: str, query: str) -> dict: # Infer DB name: dokku.postgres.my-service -> my-service # Dokku services follow the naming: dokku.. parts = container_name.split(".", 2) - db_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' + # We'll use the service name but also handle the case where it might be a prefix + 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: # Postgres: use psql with CSV-like output - # Target the specific database using -d + # 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 [] cmd = [DOCKER, "exec", container_name, "psql", "-U", "postgres"] + db_arg + ["-c", query, "-A", "-F", ","] elif "mysql" in container_name or "mariadb" in container_name: # MySQL: use mysql with tab-separated output - # Target the specific database by passing its name db_arg = [db_name] if db_name else [] cmd = [DOCKER, "exec", container_name, "mysql", "-u", "root", "-e", query, "-B"] + db_arg else: