-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
28 lines (22 loc) · 877 Bytes
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os
# Load database URL from environment variable or directly provide it here
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres-secureshe_owner:tYLwHE28IpvO@ep-wild-smoke-a1xjdvuf.ap-southeast-1.aws.neon.tech/postgres-secureshe?sslmode=require")
# Create engine
engine = create_engine(DATABASE_URL)
# Create a configured "Session" class
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create a base class for our models
Base = declarative_base()
def get_session():
# Dependency that provides a SQLAlchemy session
db = SessionLocal()
try:
yield db
finally:
db.close()
# Function to create all tables
def init_db():
Base.metadata.create_all(bind=engine)