Strip '-db' from database names

This commit is contained in:
Peter Stockings
2025-12-24 10:41:41 +11:00
parent f6547afcad
commit 9898eb440d

14
app.py
View File

@@ -225,16 +225,24 @@ def run_sql_query(container_name: str, query: str) -> dict:
# Infer DB name: dokku.postgres.my-service -> my-service # Infer DB name: dokku.postgres.my-service -> my-service
# Dokku services follow the naming: dokku.<type>.<service-name> # Dokku services follow the naming: dokku.<type>.<service-name>
parts = container_name.split(".", 2) 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: 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 # 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:
# MySQL: use mysql with tab-separated output # MySQL: use mysql with tab-separated output
# Target the specific database by passing its name
db_arg = [db_name] if db_name else [] db_arg = [db_name] if db_name else []
cmd = [DOCKER, "exec", container_name, "mysql", "-u", "root", "-e", query, "-B"] + db_arg cmd = [DOCKER, "exec", container_name, "mysql", "-u", "root", "-e", query, "-B"] + db_arg
else: else: