Skip to content

Conversation

@vprashrex
Copy link
Collaborator

@vprashrex vprashrex commented Feb 7, 2026

Summary

Target issue is #237
Explain the motivation for making this change. What existing problem does the pull request solve?
Added a startup script (start.sh) that runs alembic upgrade head before starting uvicorn, and updated the Dockerfile CMD to use it. so that there is no need to manually run migration.

Checklist

Before submitting a pull request, please ensure that you mark these task.

  • Ran fastapi run --reload app/main.py or docker compose up in the repository root and test.
  • If you've fixed a bug or added code that is tested and has test cases.

Notes

Please add here if any other information is required for the reviewer.
Tested locally using docker run to simulate ECS behavior, migrations ran successfully (001→044) before app startup

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 001, Initialize models
INFO  [alembic.runtime.migration] Running upgrade 001 -> 002, Add max length for string(varchar) fields in User and Items models
....
INFO  [alembic.runtime.migration] Running upgrade 043 -> 044, Add score_trace_url to evaluation_run
Bytecode compiled 12493 files in 238ms
INFO:     Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)
INFO:     Started parent process [104]
/app/app/core/config.py:144: UserWarning: The value of FIRST_SUPERUSER_PASSWORD is "changethis", for security, please change it, at least for deployments.

Summary by CodeRabbit

  • Chores
    • Updated backend startup flow to run database migrations automatically on initialization and then start the service, ensuring the app boots with the latest schema.

@vprashrex vprashrex requested a review from kartpop February 7, 2026 17:39
@vprashrex vprashrex self-assigned this Feb 7, 2026
@coderabbitai
Copy link

coderabbitai bot commented Feb 7, 2026

📝 Walkthrough

Walkthrough

The backend container CMD was changed to run a startup script; the new script runs alembic migrations (upgrade head) then execs uvicorn to start the app on 0.0.0.0:80 with 4 workers.

Changes

Cohort / File(s) Summary
Container startup
backend/Dockerfile
Replaced direct uvicorn CMD with CMD ["bash", "scripts/start.sh"] to delegate startup to a script.
Startup automation
backend/scripts/start.sh
Added startup script (shebang + set -e) that runs uv run alembic upgrade head then exec uv run uvicorn app.main:app --host 0.0.0.0 --port 80 --workers 4.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Container
  participant StartScript as "start.sh"
  participant Alembic
  participant Database
  participant Uvicorn as "uvicorn (app.main)"
  Container->>StartScript: CMD runs bash scripts/start.sh
  StartScript->>Alembic: run "uv run alembic upgrade head"
  Alembic->>Database: apply migrations
  Database-->>Alembic: migration result
  StartScript->>Uvicorn: exec "uv run uvicorn app.main:app --host 0.0.0.0 --port 80 --workers 4"
  Uvicorn-->>Container: serve requests
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐇 I hop in at dawn, scripts snug and bright,
I nudge the DB to grow just right,
Then call the workers, four by four,
The app springs up — I dance and soar! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: adding automatic migration execution during deployment via a startup script.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch enhancement/auto-run-migrations-on-deploy

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@vprashrex vprashrex added the enhancement New feature or request label Feb 7, 2026
@vprashrex vprashrex linked an issue Feb 7, 2026 that may be closed by this pull request
@vprashrex vprashrex moved this to In Review in Kaapi-dev Feb 7, 2026
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@backend/scripts/start.sh`:
- Around line 1-6: The script missing a trailing newline at EOF is breaking the
end-of-file pre-commit hook; open the start.sh script (look for the shebang and
commands like "uv run alembic upgrade head" and "exec uv run uvicorn
app.main:app --host 0.0.0.0 --port 80 --workers 4") and add a single newline
character at the end of the file so the file ends with a newline.
🧹 Nitpick comments (1)
backend/scripts/start.sh (1)

4-4: Migration race condition when running multiple container replicas.

If the service is scaled beyond a single task/replica (now or in the future), every container will attempt alembic upgrade head concurrently on startup. Alembic does not acquire an advisory lock by default, so concurrent migrations can fail or corrupt the alembic_version table.

This is fine for a single-instance staging deployment today, but consider one of these guards before scaling or promoting to production:

  • Use a PostgreSQL advisory lock in env.py (SELECT pg_advisory_lock(...) before running migrations).
  • Run migrations as a separate one-off ECS task / init container rather than on every replica startup.

@codecov
Copy link

codecov bot commented Feb 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Prajna1999 Prajna1999 self-requested a review February 9, 2026 05:05
Copy link
Collaborator

@Prajna1999 Prajna1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vprashrex vprashrex linked an issue Feb 9, 2026 that may be closed by this pull request
@vprashrex vprashrex changed the title CD: Auto-run migrations on staging deployment Migration: Auto-run migrations on staging deployment Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request ready-for-review

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

Migration: Autorun

2 participants