[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
+5 -4
View File
@@ -11,15 +11,16 @@ config = context.config
# Interpret the config file for Python logging. # Interpret the config file for Python logging.
# This line sets up loggers basically. # This line sets up loggers basically.
if config.config_file_name is not None: #if config.config_file_name is not None:
fileConfig(config.config_file_name) # fileConfig(config.config_file_name, disable_existing_loggers=False)
# add your model's MetaData object here # add your model's MetaData object here
# for 'autogenerate' support # for 'autogenerate' support
# from myapp import mymodel # from myapp import mymodel
# target_metadata = mymodel.Base.metadata # target_metadata = mymodel.Base.metadata
from app.model.models import Base from app.model import models
target_metadata = Base.metadata
target_metadata = models.Base.metadata
# other values from the config, defined by the needs of env.py, # other values from the config, defined by the needs of env.py,
# can be acquired: # can be acquired:
+1 -1
View File
@@ -8,4 +8,4 @@ SECRET_KEY="8b14d0b447bff7efa24d5019cc59a999786e31f6f865173bbd642bf18de5ad85"
#Key for oauth #Key for oauth
#THESE ARE TESTING KEYS - DO NOT USE IN PROD #THESE ARE TESTING KEYS - DO NOT USE IN PROD
SQLALCHEMY_DATABASE_URL="sqlite:///./gatekeeper.db" SQLALCHEMY_DATABASE_PATH="./gatekeeper.db"
+4 -2
View File
@@ -5,19 +5,21 @@ from sqlmodel import Session, SQLModel, create_engine
logger = logging.getLogger(__name__) 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) engine = create_engine(SQLALCHEMY_DATABASE_URL)
def create_db_and_tables(): def create_db_and_tables():
if not path.exists(SQLALCHEMY_DATABASE_URL): if not path.exists(SQLALCHEMY_DATABASE_PATH):
SQLModel.metadata.create_all(engine) SQLModel.metadata.create_all(engine)
from alembic.config import Config from alembic.config import Config
from alembic import command from alembic import command
alembic_cfg = Config("./alembic.ini") alembic_cfg = Config("./alembic.ini")
alembic_cfg.attributes["sqlalchemy.url"] = SQLALCHEMY_DATABASE_URL
command.stamp(alembic_cfg, "head") command.stamp(alembic_cfg, "head")
logger.info("Database created and tables initialized.") logger.info("Database created and tables initialized.")
else: else:
+2 -2
View File
@@ -11,7 +11,7 @@ def verify_settings():
"MIFARE_ACL_WRITE_BASE_KEY", "MIFARE_ACL_WRITE_BASE_KEY",
] ]
important_envs = ["SECRET_KEY"] important_envs = ["SECRET_KEY"]
other_envs = ["SQLALCHEMY_DATABASE_URL"] other_envs = ["SQLALCHEMY_DATABASE_PATH"]
for setting in card_envs: for setting in card_envs:
if (setting not in os.environ or setting == "") and not os.getenv( if (setting not in os.environ or setting == "") and not os.getenv(
"DISABLE_CARDS" "DISABLE_CARDS"
@@ -26,4 +26,4 @@ def verify_settings():
) )
for setting in other_envs: for setting in other_envs:
if setting not in os.environ: if setting not in os.environ:
logger.critical(f"Env var {setting} not set. Continuing with defaults.") logger.warning(f"Env var {setting} not set. Continuing with defaults.")