Spaces:
Runtime error
Runtime error
| from sqlalchemy import create_engine | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy.orm import sessionmaker | |
| import os | |
| import urllib.parse | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Load env variables | |
| POSTGRES_USER = os.getenv("POSTGRES_USER", "postgres") | |
| POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD", "") | |
| POSTGRES_SERVER = os.getenv("POSTGRES_SERVER", "") | |
| POSTGRES_PORT = os.getenv("POSTGRES_PORT", "5432") | |
| POSTGRES_DB = os.getenv("POSTGRES_DB", "") | |
| # Encode sensitive values for URL safety | |
| encoded_user = urllib.parse.quote_plus(POSTGRES_USER) | |
| encoded_password = urllib.parse.quote_plus(POSTGRES_PASSWORD) | |
| # Build DATABASE URL (Supabase always uses sslmode=require) | |
| DATABASE_URL = ( | |
| f"postgresql://{encoded_user}:{encoded_password}@" | |
| f"{POSTGRES_SERVER}:{POSTGRES_PORT}/{POSTGRES_DB}?sslmode=require" | |
| ) | |
| print("📡 Connecting to:", DATABASE_URL) | |
| engine = create_engine( | |
| DATABASE_URL, | |
| connect_args={"client_encoding": "utf8"} | |
| ) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |