Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

Fix #108: honor DATABASE_URL in app and migrations

Omega (QA)ff9bdda2ccca
diff --git a/backend/db_handler.py b/backend/db_handler.py
index ea0ef4f..a85a8e1 100644
--- a/backend/db_handler.py
+++ b/backend/db_handler.py
@@ -10,12 +10,27 @@ from typing import List, Dict, Optional
logger = logging.getLogger(__name__)
# PostgreSQL connection details
+# DATABASE_URL (e.g. postgresql://user:pass@host:5432/dbname) takes precedence
+# over the individual DB_* variables when set (issue #108).
+DATABASE_URL = os.environ.get('DATABASE_URL')
DB_HOST = os.environ.get('DB_HOST', 'warrackerdb')
DB_PORT = os.environ.get('DB_PORT', '5432')
DB_NAME = os.environ.get('DB_NAME', 'warranty_db')
DB_USER = os.environ.get('DB_USER', 'warranty_user')
DB_PASSWORD = os.environ.get('DB_PASSWORD', 'warranty_password')
+def _connection_kwargs():
+ """psycopg2 connection kwargs: DATABASE_URL wins over DB_* parts."""
+ if DATABASE_URL:
+ return {'dsn': DATABASE_URL}
+ return {
+ 'host': DB_HOST,
+ 'port': DB_PORT,
+ 'database': DB_NAME,
+ 'user': DB_USER,
+ 'password': DB_PASSWORD,
+ }
+
connection_pool = None # Global connection pool for this module
# Track the PID that created the current pool to detect post-fork reuse
pool_pid: Optional[int] = None
@@ -59,11 +74,7 @@ def init_db_pool(max_retries=5, retry_delay=5):
# Optimized connection pool for memory efficiency
connection_pool = pool.SimpleConnectionPool(
1, 4, # Reduced from 1,10 to 1,4 for memory efficiency
- host=DB_HOST,
- port=DB_PORT,
- database=DB_NAME,
- user=DB_USER,
- password=DB_PASSWORD,
+ **_connection_kwargs(),
# Memory optimization settings
connect_timeout=10, # Connection timeout
application_name='warracker_optimized', # Identify connections
diff --git a/backend/migrations/apply_migrations.py b/backend/migrations/apply_migrations.py
index e87c698..dd863b2 100644
--- a/backend/migrations/apply_migrations.py
+++ b/backend/migrations/apply_migrations.py
@@ -27,11 +27,34 @@ logging.basicConfig(
logger = logging.getLogger(__name__)
# PostgreSQL connection details
-DB_HOST = os.environ.get('DB_HOST', 'localhost')
-DB_PORT = os.environ.get('DB_PORT', '5432')
-DB_NAME = os.environ.get('DB_NAME', 'warranty_db')
-DB_USER = os.environ.get('DB_USER', 'warranty_user')
-DB_PASSWORD = os.environ.get('DB_PASSWORD', 'warranty_password')
+# DATABASE_URL (e.g. postgresql://user:pass@host:5432/dbname) takes precedence
+# over the individual DB_* variables when set (issue #108).
+def _db_parts():
+ url = os.environ.get('DATABASE_URL')
+ if url:
+ from urllib.parse import urlparse, unquote
+ p = urlparse(url)
+ return {
+ 'host': p.hostname or 'localhost',
+ 'port': str(p.port) if p.port else '5432',
+ 'name': (p.path or '').lstrip('/') or 'warranty_db',
+ 'user': unquote(p.username) if p.username else 'warranty_user',
+ 'password': unquote(p.password) if p.password else 'warranty_password',
+ }
+ return {
+ 'host': os.environ.get('DB_HOST', 'localhost'),
+ 'port': os.environ.get('DB_PORT', '5432'),
+ 'name': os.environ.get('DB_NAME', 'warranty_db'),
+ 'user': os.environ.get('DB_USER', 'warranty_user'),
+ 'password': os.environ.get('DB_PASSWORD', 'warranty_password'),
+ }
+
+_DB = _db_parts()
+DB_HOST = _DB['host']
+DB_PORT = _DB['port']
+DB_NAME = _DB['name']
+DB_USER = _DB['user']
+DB_PASSWORD = _DB['password']
DB_ADMIN_USER = os.environ.get('DB_ADMIN_USER', 'warracker_admin')
DB_ADMIN_PASSWORD = os.environ.get('DB_ADMIN_PASSWORD', 'change_this_password_in_production')