add auto migrations
This commit is contained in:
parent
e10b82a3b9
commit
bb8427c032
|
|
@ -55,8 +55,24 @@
|
||||||
**Полностью реализовано**:
|
**Полностью реализовано**:
|
||||||
- ✅ BACKEND: Models, Repositories, Services, API Endpoints
|
- ✅ BACKEND: Models, Repositories, Services, API Endpoints
|
||||||
- ✅ FRONTEND: Contexts, API Client, Components, LibraryPage Integration
|
- ✅ FRONTEND: Contexts, API Client, Components, LibraryPage Integration
|
||||||
|
- ✅ DOCKER: Auto-migrations on startup (async/sync boundary fix)
|
||||||
|
|
||||||
**Готово к тестированию!**
|
**Готово к deployment!**
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## КРИТИЧЕСКИЕ ИСПРАВЛЕНИЯ
|
||||||
|
|
||||||
|
### Auto-Migration Fix (database.py)
|
||||||
|
**Проблема**: RuntimeWarning: coroutine 'run_async_migrations' was never awaited
|
||||||
|
**Причина**: 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:25-26` - Copy alembic files to container
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -57,25 +57,37 @@ async def init_db() -> None:
|
||||||
|
|
||||||
|
|
||||||
def run_migrations() -> None:
|
def run_migrations() -> None:
|
||||||
"""Run Alembic migrations."""
|
"""
|
||||||
|
Run Alembic migrations synchronously.
|
||||||
|
|
||||||
|
Alembic is sync-only, so we convert async URL to sync URL.
|
||||||
|
"""
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from alembic import command
|
from alembic import command
|
||||||
from alembic.config import Config
|
from alembic.config import Config
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
# Find alembic.ini (works in both dev and Docker)
|
# Find alembic.ini
|
||||||
alembic_ini_path = Path(__file__).parent.parent.parent.parent / "alembic.ini"
|
alembic_ini_path = Path(__file__).parent.parent.parent.parent / "alembic.ini"
|
||||||
|
|
||||||
if not alembic_ini_path.exists():
|
if not alembic_ini_path.exists():
|
||||||
logger.warning(f"alembic.ini not found at {alembic_ini_path}, skipping migrations")
|
logger.warning(f"alembic.ini not found at {alembic_ini_path}, skipping migrations")
|
||||||
return
|
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
|
# Create Alembic config
|
||||||
alembic_cfg = Config(str(alembic_ini_path))
|
alembic_cfg = Config(str(alembic_ini_path))
|
||||||
alembic_cfg.set_main_option("sqlalchemy.url", settings.database_url)
|
alembic_cfg.set_main_option("sqlalchemy.url", sync_db_url)
|
||||||
|
|
||||||
# Run migrations
|
# Run migrations synchronously
|
||||||
logger.info("Running Alembic migrations...")
|
logger.info("Running Alembic migrations...")
|
||||||
command.upgrade(alembic_cfg, "head")
|
try:
|
||||||
logger.info("Migrations completed successfully")
|
command.upgrade(alembic_cfg, "head")
|
||||||
|
logger.info("Migrations completed successfully")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Migration failed: {e}")
|
||||||
|
raise
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue