Configure multi-database PostgreSQL routing with custom DatabaseRouter
Some checks failed
Deploy Beta (NATIVE) / deploy (push) Failing after 25s

This commit is contained in:
vickytechkey 2026-08-12 17:31:57 +05:30
parent bc7dab7a81
commit 200c747c4c
3 changed files with 70 additions and 40 deletions

33
config/db_router.py Normal file
View file

@ -0,0 +1,33 @@
class DatabaseRouter:
"""
A database router to route model operations to different PostgreSQL databases.
"""
def db_for_read(self, model, **hints):
if model is not None:
model_name = model._meta.model_name
else:
model_name = hints.get('model_name')
if model_name in ['user', 'supplierprofile', 'otprecord']:
return 'sellerprofile'
elif model_name in ['product', 'bulkuploadlog']:
return 'productprofile'
elif model_name in ['order', 'returnrequest', 'wallet', 'wallettransaction']:
return 'customerprofile'
return 'default'
def db_for_write(self, model, **hints):
return self.db_for_read(model, **hints)
def allow_relation(self, obj1, obj2, **hints):
# We removed cross-database foreign key constraints, so we can allow logical relations.
return True
def allow_migrate(self, db, app_label, model_name=None, **hints):
if model_name:
target_db = self.db_for_read(None, **{'model_name': model_name})
if db == target_db:
return True
return False
return None

View file

@ -115,49 +115,45 @@ def _fetch_db_secret(secret_arn: str) -> dict:
if _DB_SECRET_ARN:
# --- Mode 1: Production — credentials from AWS Secrets Manager ---
_secret = _fetch_db_secret(_DB_SECRET_ARN)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': _secret['dbname'],
'USER': _secret['username'],
'PASSWORD': _secret['password'],
'HOST': _secret['host'],
'PORT': str(_secret.get('port', 3306)),
'OPTIONS': {
'charset': 'utf8mb4',
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'connect_timeout': 10,
},
}
db_credentials = {
'ENGINE': 'django.db.backends.postgresql',
'USER': _secret['username'],
'PASSWORD': _secret['password'],
'HOST': _secret['host'],
'PORT': str(_secret.get('port', 5432)),
}
elif _DB_ENGINE == 'django.db.backends.mysql':
# --- Mode 2: Local MySQL via explicit env vars (docker-compose) ---
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ.get('DB_NAME', 'seller_central'),
'USER': os.environ.get('DB_USER', 'root'),
'PASSWORD': os.environ.get('DB_PASSWORD', ''),
'HOST': os.environ.get('DB_HOST', '127.0.0.1'),
'PORT': os.environ.get('DB_PORT', '3306'),
'OPTIONS': {
'charset': 'utf8mb4',
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'connect_timeout': 10,
},
}
}
else:
# --- Mode 3: Local dev — SQLite (no config needed) ---
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
# --- Mode 2: Local dev / fallback with default credentials ---
db_credentials = {
'ENGINE': 'django.db.backends.postgresql',
'USER': os.environ.get('DB_USER', 'vignesh'),
'PASSWORD': os.environ.get('DB_PASSWORD', 'vtechnosoft@123A'),
'HOST': os.environ.get('DB_HOST', '127.0.0.1'),
'PORT': os.environ.get('DB_PORT', '5432'),
}
DATABASES = {
'default': {
'NAME': 'sellerprofile',
**db_credentials
},
'sellerprofile': {
'NAME': 'sellerprofile',
**db_credentials
},
'customerprofile': {
'NAME': 'customerprofile',
**db_credentials
},
'productprofile': {
'NAME': 'productprofile',
**db_credentials
}
}
DATABASE_ROUTERS = ['config.db_router.DatabaseRouter']
# Password validation
# https://docs.djangoproject.com/en/6.1/ref/settings/#auth-password-validators

View file

@ -8,6 +8,7 @@ django-cors-headers
pytest
pytest-django
pillow
mysqlclient
psycopg2-binary
boto3
djangorestframework-simplejwt