-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
32 lines (24 loc) · 948 Bytes
/
Copy pathdatabase.py
File metadata and controls
32 lines (24 loc) · 948 Bytes
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
29
30
31
32
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from app.core.config import settings
# DATABASE_URL (e.g. postgresql://user:pass@host/db) takes over from the local
# SQLite file when set -- lets the same models/routes run against a managed
# Postgres in production without any other code change. `check_same_thread`
# is SQLite-only, so it's only passed for that dialect.
_database_url = os.getenv("DATABASE_URL")
if _database_url:
engine = create_engine(_database_url)
else:
engine = create_engine(f"sqlite:///{settings.db_path}", connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
def init_db():
from app.models import db_models # noqa
Base.metadata.create_all(bind=engine)