Spaces:
Sleeping
Sleeping
File size: 681 Bytes
20d4fcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | from sqlmodel import SQLModel, create_engine, Session
from typing import Generator
import os
from dotenv import load_dotenv
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
raise ValueError("DATABASE_URL environment variable is not set")
# Neon/Postgres requires postgresql:// but some libs output postgres://
if DATABASE_URL.startswith("postgres://"):
DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1)
engine = create_engine(DATABASE_URL, echo=True)
def get_session() -> Generator[Session, None, None]:
with Session(engine) as session:
yield session
def init_db():
SQLModel.metadata.create_all(engine) |