- Dockerfile builds image with app source in /app_src - docker-entrypoint.sh syncs code from image on each start, inits DB - docker-compose.yml mounts ./appdata:/app for easy backup - backend/init_db.py for safe DB init (no drop_all) - backend/database.py supports DATABASE_URL env var - .dockerignore excludes venv, __pycache__, *.db, .git Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
19 lines
566 B
Python
19 lines
566 B
Python
# backend/database.py
|
|
import os
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker, scoped_session
|
|
|
|
SQLALCHEMY_DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./app.db")
|
|
|
|
connect_args = {"check_same_thread": False} if SQLALCHEMY_DATABASE_URL.startswith("sqlite") else {}
|
|
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args=connect_args)
|
|
SessionLocal = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|