[main] fix startup when there's no db

This commit is contained in:
2026-07-29 18:37:34 +02:00
parent 7a70b6b1d4
commit a8e80ed19d
4 changed files with 12 additions and 9 deletions

View File

@@ -5,19 +5,21 @@ from sqlmodel import Session, SQLModel, create_engine
logger = logging.getLogger(__name__)
SQLALCHEMY_DATABASE_URL = getenv("SQLALCHEMY_DATABASE_URL", "sqlite:///./gatekeeper.db")
SQLALCHEMY_DATABASE_PATH = getenv("SQLALCHEMY_DATABASE_PATH", "./gatekeeper.db")
SQLALCHEMY_DATABASE_URL = "sqlite:///" + SQLALCHEMY_DATABASE_PATH
engine = create_engine(SQLALCHEMY_DATABASE_URL)
def create_db_and_tables():
if not path.exists(SQLALCHEMY_DATABASE_URL):
if not path.exists(SQLALCHEMY_DATABASE_PATH):
SQLModel.metadata.create_all(engine)
from alembic.config import Config
from alembic import command
alembic_cfg = Config("./alembic.ini")
alembic_cfg.attributes["sqlalchemy.url"] = SQLALCHEMY_DATABASE_URL
command.stamp(alembic_cfg, "head")
logger.info("Database created and tables initialized.")
else: