Add alembic migrations
This commit is contained in:
+2
-1
@@ -1,7 +1,7 @@
|
||||
from fastapi import FastAPI, Depends
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from .providers.db_provider import create_db_and_tables
|
||||
from .providers.db_provider import create_db_and_tables, startup_migrations
|
||||
from .routers.houses import router as houses_router
|
||||
from .routers.owners import router as owners_router
|
||||
from .middleware.authenticate import authenticate
|
||||
@@ -10,6 +10,7 @@ from contextlib import asynccontextmanager
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI):
|
||||
startup_migrations()
|
||||
create_db_and_tables()
|
||||
yield
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ from sqlalchemy.ext.asyncio import (
|
||||
)
|
||||
from sqlmodel import create_engine, SQLModel
|
||||
from sqlalchemy.orm import Session
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
from ..models.user import User
|
||||
|
||||
@@ -80,3 +82,13 @@ def create_db_and_tables():
|
||||
engine = _get_engine()
|
||||
SQLModel.metadata.create_all(engine)
|
||||
_seed_db()
|
||||
|
||||
def startup_migrations():
|
||||
print("Running Alembic migrations...")
|
||||
|
||||
backend_path = os.path.dirname(os.path.abspath(__file__)) + "/../.."
|
||||
try:
|
||||
subprocess.run(["alembic", "upgrade", "head"], check=True, cwd=backend_path)
|
||||
print("Migrations applied successfully!")
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error applying migrations: {e}")
|
||||
|
||||
Reference in New Issue
Block a user