From b7a4e73d936d3f9237e38f5b6402ed91b6b15c0d Mon Sep 17 00:00:00 2001 From: saani Date: Mon, 24 Nov 2025 00:01:30 +0000 Subject: [PATCH] refactor(config): use dj-database-url for flexible database configuration - Add dj-database-url package for parsing DATABASE_URL - Replace hardcoded PostgreSQL config with DATABASE_URL environment variable - Add connection pooling with conn_max_age and health checks - Fallback to SQLite for local development when DATABASE_URL is not set - Remove obsolete production config comments - Update requirements.txt with new dependency This change improves deployment flexibility by supporting standard DATABASE_URL format commonly used in cloud platforms (Heroku, Railway, etc.) while maintaining backward compatibility with local development using SQLite. --- booking_system/settings.py | 33 +++++++++++++++------------------ requirements.txt | Bin 888 -> 986 bytes 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/booking_system/settings.py b/booking_system/settings.py index 58c66d8..ecb05dc 100644 --- a/booking_system/settings.py +++ b/booking_system/settings.py @@ -2,6 +2,7 @@ import os from pathlib import Path from datetime import timedelta from dotenv import load_dotenv +import dj_database_url load_dotenv() @@ -11,10 +12,8 @@ SECRET_KEY = os.getenv('JWT_SECRET', 'django-insecure-fallback-secret-key') DEBUG = os.getenv('DEBUG') -# Update ALLOWED_HOSTS for production ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '*').split(',') -# Update CORS for production CORS_ALLOWED_ORIGINS = os.getenv( 'CORS_ALLOWED_ORIGINS', 'http://localhost:3000,http://127.0.0.1:3000' @@ -77,23 +76,21 @@ if not DEBUG: SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") -# DATABASES = { -# 'default': { -# 'ENGINE': 'django.db.backends.sqlite3', -# 'NAME': BASE_DIR / 'db.sqlite3', -# } -# } - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': os.getenv('POSTGRES_DB'), - 'USER': os.getenv('POSTGRES_USER'), - 'PASSWORD': os.getenv('POSTGRES_PASSWORD'), - 'HOST': os.getenv('POSTGRES_HOST', 'postgres'), - 'PORT': os.getenv('POSTGRES_PORT', 5432), +if os.getenv('DATABASE_URL'): + DATABASES = { + 'default': dj_database_url.config( + default=os.getenv('DATABASE_URL'), + conn_max_age=600, + conn_health_checks=True, + ) + } +else: + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } } -} ENCRYPTION_KEY = os.getenv('ENCRYPTION_KEY') diff --git a/requirements.txt b/requirements.txt index 41195ed5c864da9e38f5a28db5ff846088a4c212..d056f49a0474905ac182fd7e067ec6d176b87908 100644 GIT binary patch delta 103 zcmeytc8h(&3GEbyECyYM6oy2G5+F_j;$ntWphzi05kn4xEf5+r=rI@ovEjzU`Hb4d r43!MY4EYQN4CxF;Ky^tBnGAVAT}41~kZL2KN=uMx2E)zTOumc&zb6#d delta 15 Xcmcb`{)278iH$$97&lL0a$^JlKVAmh