[settings] fuck it, rework the settings again

this time using pydantic-settings as a base
- removed all os.getenv calls
- removed the secret_key default option
- reworked database loading, creating tables
- prob. something else also but its 4:30 and i have to sleep
This commit is contained in:
2026-07-31 04:33:18 +02:00
parent bad4e8dd1b
commit 158b430235
11 changed files with 100 additions and 89 deletions

View File

@@ -1,38 +1,48 @@
import logging
from os import getenv, path
from functools import lru_cache
from sqlalchemy import inspect
from sqlmodel import Session, SQLModel, create_engine
from app.services.settings import settings
logger = logging.getLogger(__name__)
SQLALCHEMY_DATABASE_PATH = getenv("SQLALCHEMY_DATABASE_PATH", "./gatekeeper.db")
SQLALCHEMY_DATABASE_URL = "sqlite:///" + SQLALCHEMY_DATABASE_PATH
engine = create_engine(SQLALCHEMY_DATABASE_URL)
@lru_cache
def get_engine():
return create_engine(
settings.sqlalchemy_database_url, connect_args={"check_same_thread": False}
)
def create_db_and_tables():
if not path.exists(SQLALCHEMY_DATABASE_PATH):
SQLModel.metadata.create_all(engine)
inspector = inspect(get_engine())
existing_tables = inspector.get_table_names()
if not existing_tables:
SQLModel.metadata.create_all(get_engine())
from alembic.config import Config
from alembic import command
alembic_cfg = Config("./alembic.ini")
alembic_cfg.attributes["sqlalchemy.url"] = SQLALCHEMY_DATABASE_URL
alembic_cfg = Config(settings.alembic_config)
alembic_cfg.set_main_option("sqlalchemy.url", str(get_engine().url))
alembic_cfg.attributes["sqlalchemy.url"] = settings.sqlalchemy_database_url
command.stamp(alembic_cfg, "head")
logger.info("Database created and tables initialized.")
else:
logger.info("Database already exists")
logger.info(
"Database already initialized (%d tables found).", len(existing_tables)
)
def get_session():
with Session(engine) as db:
with Session(get_engine()) as db:
yield db
def get_db_session():
return Session(engine)
return Session(get_engine())
def add_and_refresh(db: Session, obj):