v1.4 - Login fixes, etc
This commit is contained in:
+36
-6
@@ -1,7 +1,9 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
from threading import Lock
|
||||
|
||||
if __package__ in {None, ""}:
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
@@ -19,16 +21,41 @@ from app.api.raw_materials import router as raw_materials_router
|
||||
from app.api.scenarios import router as scenarios_router
|
||||
from app.core.config import settings
|
||||
from app.db.session import Base, engine
|
||||
from app.db.migrations import ensure_tenant_columns, sync_tenant_ids
|
||||
from app.db.migrations import MigrationReport, bootstrap_schema, sync_tenant_ids
|
||||
from app.seed import seed_if_empty
|
||||
|
||||
logger = logging.getLogger("data_entry_app.startup")
|
||||
_database_ready = False
|
||||
_database_ready_lock = Lock()
|
||||
|
||||
|
||||
def ensure_database_ready() -> MigrationReport:
|
||||
global _database_ready
|
||||
|
||||
if _database_ready:
|
||||
return MigrationReport()
|
||||
|
||||
with _database_ready_lock:
|
||||
if _database_ready:
|
||||
return MigrationReport()
|
||||
|
||||
schema_report = bootstrap_schema(engine, Base.metadata)
|
||||
seed_if_empty()
|
||||
tenant_sync_report = sync_tenant_ids(engine)
|
||||
|
||||
report = MigrationReport(
|
||||
created_tables=schema_report.created_tables,
|
||||
added_columns=schema_report.added_columns,
|
||||
synced_tenant_rows=tenant_sync_report,
|
||||
)
|
||||
logger.info("Database startup checks complete: %s", report.summary())
|
||||
_database_ready = True
|
||||
return report
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
ensure_tenant_columns(engine)
|
||||
seed_if_empty()
|
||||
sync_tenant_ids(engine)
|
||||
ensure_database_ready()
|
||||
yield
|
||||
|
||||
|
||||
@@ -36,7 +63,8 @@ app = FastAPI(title=settings.app_name, lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["http://localhost:5173", "http://localhost:5174"],
|
||||
allow_origins=list(settings.cors_allow_origins),
|
||||
allow_origin_regex=settings.cors_allow_origin_regex,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
@@ -81,6 +109,8 @@ def healthcheck():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
report = ensure_database_ready()
|
||||
print(f"Database startup checks complete: {report.summary()}")
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=os.getenv("HOST", "0.0.0.0"),
|
||||
|
||||
Reference in New Issue
Block a user