diff --git a/app/const.py b/app/const.py index 8db0918..0aacda3 100644 --- a/app/const.py +++ b/app/const.py @@ -1,3 +1,4 @@ +ALEMBIC_CONFIG = '/app/alembic.ini' DB_URL = 'sqlite:////app/storage/was.db' DIR_ASSET = '/app/storage/asset' DIR_OTA = '/app/storage/ota' diff --git a/app/db/main.py b/app/db/main.py index 17644d0..4b24a62 100644 --- a/app/db/main.py +++ b/app/db/main.py @@ -14,10 +14,6 @@ engine = create_engine(DB_URL, echo=True, connect_args=connect_args) -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - def get_config_db(config_type): config = WillowConfig() diff --git a/app/main.py b/app/main.py index 71cc08f..6859218 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,9 @@ import asyncio import json import os + +import alembic +import alembic.config from fastapi import ( FastAPI, Header, @@ -18,11 +21,12 @@ from fastapi.middleware.cors import CORSMiddleware from app.const import ( + ALEMBIC_CONFIG, DIR_OTA, STORAGE_USER_CONFIG, ) -from app.db.main import create_db_and_tables, migrate_user_config +from app.db.main import migrate_user_config from app.internal.command_endpoints import ( CommandEndpointResponse, CommandEndpointResult, @@ -59,9 +63,16 @@ pass +def db_migrations(): + cfg = alembic.config.Config(ALEMBIC_CONFIG) + cfg.attributes['logger'] = log + alembic.command.upgrade(cfg, "head") + + @asynccontextmanager async def lifespan(app: FastAPI): - create_db_and_tables() + # database schema migrations + db_migrations() migrate_user_files() get_tz_config(refresh=True) diff --git a/migrations/env.py b/migrations/env.py index 84b2e36..a6418dc 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -14,7 +14,8 @@ # Interpret the config file for Python logging. # This line sets up loggers basically. -if config.config_file_name is not None: +# We must skip this when config has a logger attribute to prevent the WAS logger config from being overwritten +if config.config_file_name is not None and config.attributes.get('logger', None) is None: fileConfig(config.config_file_name) # add your model's MetaData object here