From 8f67d326c9a06978dec554e09eb202f70d4d2c88 Mon Sep 17 00:00:00 2001 From: Celes Renata Date: Mon, 20 Apr 2026 01:33:14 +0000 Subject: [PATCH] feat: derive POSTGRES_DB and Redis prefix from DEPLOY_STAGE for pipeline isolation --- services/shared/config.py | 8 +++++++- services/shared/redis_keys.py | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/services/shared/config.py b/services/shared/config.py index ec6b6c3..747f471 100644 --- a/services/shared/config.py +++ b/services/shared/config.py @@ -105,6 +105,12 @@ def _bucket(name: str) -> str: prefix = os.getenv("DEPLOY_STAGE", "") return f"{prefix}-{name}" if prefix else name + +def _stage_db() -> str: + """Derive database name from DEPLOY_STAGE. Empty stage = 'stonks'.""" + stage = os.getenv("DEPLOY_STAGE", "") + return f"stonks_{stage}" if stage else "stonks" + BUCKET_RETENTION_FIELDS: dict[str, str] = { _bucket("stonks-raw-market"): "raw_market_days", _bucket("stonks-raw-news"): "raw_news_days", @@ -229,7 +235,7 @@ def load_config() -> AppConfig: postgres=PostgresConfig( host=os.getenv("POSTGRES_HOST", "localhost"), port=int(os.getenv("POSTGRES_PORT", "5432")), - database=os.getenv("POSTGRES_DB", "stonks"), + database=os.getenv("POSTGRES_DB", "") or _stage_db(), user=os.getenv("POSTGRES_USER", "stonks"), password=os.getenv("POSTGRES_PASSWORD", "stonks_dev"), ), diff --git a/services/shared/redis_keys.py b/services/shared/redis_keys.py index 0aad931..16f6c87 100644 --- a/services/shared/redis_keys.py +++ b/services/shared/redis_keys.py @@ -1,7 +1,10 @@ """Redis key conventions and queue abstractions.""" +import os + # --- Key prefixes --- -PREFIX = "stonks" +_STAGE = os.getenv("DEPLOY_STAGE", "") +PREFIX = f"stonks:{_STAGE}" if _STAGE else "stonks" # Distributed locks LOCK_PREFIX = f"{PREFIX}:lock"