Dev-лаунчер: применять миграции перед стартом
Ветка задачи может принести миграцию, а run.ps1/run.sh сразу запускали uvicorn: приложение поднималось на старой схеме и падало 500 на первом обращении к новой таблице. Так вышло с 0012_match_finish_draft — создание партии отвечало «no such table: match_finish_drafts». В test/prod такого нет, там схему накатывает entrypoint.sh контейнера. Отдельно разобран случай «база новее ветки»: после возврата с ветки задачи alembic не находит ревизию, которой в этой ветке ещё нет. Останавливать запуск тут не за что — лишние таблицы старому коду не мешают, поэтому печатаем предупреждение и идём дальше. Настоящая ошибка миграции по-прежнему останавливает запуск. stderr alembic в run.ps1 уводится в файл: в Windows PowerShell 5.1 «2>&1» на нативном exe заворачивает каждую строку в ErrorRecord, а решение принимается по коду возврата. run.ps1 остался строго ASCII. #36 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BoiJK9ux8peeyjLb8TYjFf
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user