add auto migrations
This commit is contained in:
parent
bb8427c032
commit
e263fbc98a
|
|
@ -63,16 +63,12 @@
|
||||||
|
|
||||||
## КРИТИЧЕСКИЕ ИСПРАВЛЕНИЯ
|
## КРИТИЧЕСКИЕ ИСПРАВЛЕНИЯ
|
||||||
|
|
||||||
### Auto-Migration Fix (database.py)
|
### Migrations Strategy
|
||||||
**Проблема**: RuntimeWarning: coroutine 'run_async_migrations' was never awaited
|
**Решение**: Миграции запускаются в Dockerfile перед стартом сервера
|
||||||
**Причина**: Alembic - синхронная библиотека, но получала async database URL
|
|
||||||
**Решение**: Конвертация async URL в sync перед передачей в Alembic
|
|
||||||
- `sqlite+aiosqlite://` → `sqlite:///`
|
|
||||||
- `postgresql+asyncpg://` → `postgresql://`
|
|
||||||
|
|
||||||
**Файлы**:
|
**Файлы**:
|
||||||
- `backend/src/app/infra/database.py:78-80` - URL conversion
|
- `backend/Dockerfile:34` - `alembic upgrade head` перед uvicorn
|
||||||
- `backend/Dockerfile:25-26` - Copy alembic files to container
|
- `backend/Dockerfile:25-26` - Copy alembic files to container
|
||||||
|
- Удален `run_migrations()` из кода приложения (clean separation)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,4 +30,5 @@ RUN mkdir -p /app/data
|
||||||
|
|
||||||
ENV PYTHONPATH=/app/src
|
ENV PYTHONPATH=/app/src
|
||||||
|
|
||||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
# Run migrations and start server
|
||||||
|
CMD ["sh", "-c", "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000"]
|
||||||
|
|
|
||||||
|
|
@ -54,40 +54,3 @@ async def init_db() -> None:
|
||||||
"""Initialize database tables."""
|
"""Initialize database tables."""
|
||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
|
||||||
|
|
||||||
def run_migrations() -> None:
|
|
||||||
"""
|
|
||||||
Run Alembic migrations synchronously.
|
|
||||||
|
|
||||||
Alembic is sync-only, so we convert async URL to sync URL.
|
|
||||||
"""
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from alembic import command
|
|
||||||
from alembic.config import Config
|
|
||||||
from loguru import logger
|
|
||||||
|
|
||||||
# Find alembic.ini
|
|
||||||
alembic_ini_path = Path(__file__).parent.parent.parent.parent / "alembic.ini"
|
|
||||||
|
|
||||||
if not alembic_ini_path.exists():
|
|
||||||
logger.warning(f"alembic.ini not found at {alembic_ini_path}, skipping migrations")
|
|
||||||
return
|
|
||||||
|
|
||||||
# Convert async database URL to sync for Alembic
|
|
||||||
sync_db_url = settings.database_url.replace("sqlite+aiosqlite://", "sqlite:///")
|
|
||||||
sync_db_url = sync_db_url.replace("postgresql+asyncpg://", "postgresql://")
|
|
||||||
|
|
||||||
# Create Alembic config
|
|
||||||
alembic_cfg = Config(str(alembic_ini_path))
|
|
||||||
alembic_cfg.set_main_option("sqlalchemy.url", sync_db_url)
|
|
||||||
|
|
||||||
# Run migrations synchronously
|
|
||||||
logger.info("Running Alembic migrations...")
|
|
||||||
try:
|
|
||||||
command.upgrade(alembic_cfg, "head")
|
|
||||||
logger.info("Migrations completed successfully")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Migration failed: {e}")
|
|
||||||
raise
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
from app.api.v1 import assets, auth, batch, folders, shares, uploads
|
from app.api.v1 import assets, auth, batch, folders, shares, uploads
|
||||||
from app.infra.config import get_settings
|
from app.infra.config import get_settings
|
||||||
from app.infra.database import init_db, run_migrations
|
from app.infra.database import init_db
|
||||||
|
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
|
|
||||||
|
|
@ -15,8 +15,7 @@ settings = get_settings()
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
"""Application lifespan handler."""
|
"""Application lifespan handler."""
|
||||||
# Startup: Run migrations first
|
# Startup
|
||||||
run_migrations()
|
|
||||||
await init_db()
|
await init_db()
|
||||||
yield
|
yield
|
||||||
# Shutdown
|
# Shutdown
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue