diff --git a/run.ps1 b/run.ps1 index 9bf026c..f3158ac 100644 --- a/run.ps1 +++ b/run.ps1 @@ -82,6 +82,30 @@ switch ($appEnv) { Write-Host "node_modules not found. First: cd frontend; npm install" -ForegroundColor Red exit 1 } + # Schema first: a branch may add a migration, and uvicorn would otherwise start + # on the old schema and fail with 500 on the first hit of a new table. + # stderr goes to a file: alembic logs there, and in PowerShell 5.1 "2>&1" on a + # native exe wraps every line into an ErrorRecord. + Write-Host "Applying migrations (alembic upgrade head)..." -ForegroundColor Green + $migLog = [System.IO.Path]::GetTempFileName() + Push-Location $backend + & $py -m alembic upgrade head 2> $migLog + $migCode = $LASTEXITCODE + Pop-Location + $migText = (Get-Content $migLog -Raw) + Remove-Item $migLog -Force -ErrorAction SilentlyContinue + if ($migCode -ne 0) { + Write-Host $migText + if ($migText -match "locate revision") { + # The DB carries a migration this branch does not have (switched back from + # a feature branch). Extra tables do not bother the older code - go on. + Write-Host "DB is newer than this branch - migrations skipped." -ForegroundColor Yellow + } else { + Write-Host "Migrations failed - not starting. Fix the error above and retry." -ForegroundColor Red + exit 1 + } + } + Write-Host "Starting backend (uvicorn --reload) and frontend (vite) in separate windows..." -ForegroundColor Green Start-Process powershell -ArgumentList @( "-NoExit", "-Command", diff --git a/run.sh b/run.sh index ad90724..047d691 100644 --- a/run.sh +++ b/run.sh @@ -50,6 +50,23 @@ case "$app_env" in [ -x "$py" ] || py="$root/backend/.venv/Scripts/python.exe" # Git Bash на Windows [ -x "$py" ] || { echo "Нет venv. Сначала: cd backend && python -m venv .venv && pip install -e '.[dev]'"; exit 1; } [ -d "$root/frontend/node_modules" ] || { echo "Нет node_modules. Сначала: cd frontend && npm install"; exit 1; } + # Сначала схема: ветка могла принести миграцию, и без неё бэк поднимется на + # старой схеме и упадёт 500 на первом же обращении к новой таблице. + echo "Применяю миграции (alembic upgrade head)…" + mig_log="$( cd "$root/backend" && "$py" -m alembic upgrade head 2>&1 )" && mig_code=0 || mig_code=$? + if [ "$mig_code" -ne 0 ]; then + echo "$mig_log" + case "$mig_log" in + *"locate revision"*) + # В базе миграция, которой нет в этой ветке (вернулись с ветки задачи). + # Лишние таблицы старому коду не мешают — продолжаем запуск. + echo "База новее ветки — миграции пропущены." ;; + *) + echo "Миграции не применились — не запускаю. Исправьте ошибку выше и повторите." + exit 1 ;; + esac + fi + echo "Запуск бэка (uvicorn --reload) и фронта (vite)… Ctrl+C — остановить всё." ( cd "$root/backend" && exec "$py" -m uvicorn app.main:app --reload ) & back=$!