# Review Pull Requests in a SQLAlchemy App

Yes. For each pull request the agent runs the tests on a real database, checks migrations against the models, reads each query and session use, and posts a review.

[Run this on Rush](https://cloud.getrush.ai/run/review-pull-requests/sqlalchemy/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent sets up the database the tests need, because in an app like teamhide/fastapi-boilerplate they do not run without one. `tests/conftest.py` applies the Alembic migrations to a real MySQL before each test that takes the `session` fixture and truncates every table after it, and `core/config.py` points the test configuration at a `fastapi_test` database on `localhost:3306` through the `aiomysql` driver. The repository starts MySQL and Redis with `docker/docker-compose.yml`. The sandbox has no Docker, so the agent installs MySQL and Redis servers from system packages, creates the `fastapi` user and the two databases that the compose file and `docker/init.sql` set up, and runs `make test`, which is `ENV=test pytest tests -s`, on the base branch and on the pull request's head.

Then it checks the schema story the diff tells. A pull request that changes a mapped class needs a revision under `migrations/versions`, and the two must agree. The agent applies the migrations to an empty database and compares the result with the models. That is worth doing by hand here, because `migrations/env.py` imports `Base` from `core.db` and nothing that defines a model, so what `alembic revision --autogenerate` sees depends on which modules happened to be imported first. The existing revision and model already differ in small ways: the migration creates `id` as `BigInteger` and `is_admin` as nullable, while the model declares `Mapped[int]` and `Mapped[bool]`. The review separates drift the pull request adds from drift that was already there.

It reads session code against the rules this codebase sets for itself. `core/db/session.py` routes by statement: `RoutingSession.get_bind` sends flushes and `Insert`, `Update` and `Delete` to the writer engine and everything else to the reader. Writes go through the request-scoped `session` and are committed by the `@Transactional()` decorator, and the README says not to call `commit()` yourself. Reads in `UserSQLAlchemyRepo` open their own session with `session_factory()`. So the agent flags an explicit `commit()`, a write on a `session_factory()` session, a service method that writes without `@Transactional()`, and a read placed right after a write it must see, since that read may be routed to a replica.

Last come the mistakes that type-check and pass tests. SQLAlchemy builds SQL from Python expressions, so a comparison between two plain Python values is evaluated by Python and never reaches the database. A filter such as `and_(Order.id == order_id, status == status)` shows the shape: the second term compares an argument with itself, so it is always true, and a test that tries only a matching value still passes. The agent looks for that pattern in every filter the diff touches. It also looks for a `select` with `limit` and no `order_by`, as `get_users` has, for lazy loads in async code, and for SQL assembled from strings.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Review this pull request to our SQLAlchemy app. Read pyproject.toml, core/config.py, core/db/session.py, core/db/transactional.py, the SQLAlchemy middleware, migrations/env.py, every file under migrations/versions, tests/conftest.py, the test database helper, docker/docker-compose.yml, docker/init.sql, the Makefile and the README first. Docker is not available: install the database and Redis servers the compose file names from system packages, create the user and databases the compose file and the init script create, and install dependencies with Poetry on the Python version pyproject.toml pins. Run make test on the base branch and on the head and report only what the pull request changes. Apply the migrations to an empty database, then compare the resulting schema with the mapped classes column by column (type, nullability, defaults, unique constraints); if the diff adds a revision, also run its downgrade and upgrade again. Separate drift the pull request introduces from drift already on the base. Then read the diff. Flag: a filter that compares two Python values instead of a column and a value; a query with limit or offset and no order_by; an explicit commit where the Transactional decorator should be used; a write on a session from session_factory; a read that must see a preceding write but may be routed to the reader engine; a lazy relationship load in async code; SQL built from strings; and a model change with no revision. Where a finding can be shown with a short test against the database, write it, run it and include it in the review without committing it. Post one review with the command results first and findings by file and line. Push nothing.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app/pull/42"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

One review on the pull request. It opens with the test result on base and head and the migration check: whether `alembic upgrade head` applies cleanly to an empty database, whether the new revision's downgrade reverses it, and the differences between the migrated schema and the models, split into those the pull request introduces and those already present.

Findings follow by file and line, each with one sentence on what would happen in production: this filter never compares the column, so it matches rows it should leave out; this page of results can repeat or skip rows because nothing orders them; this read may reach the reader engine before the write has replicated. Where the agent wrote a short test to demonstrate a finding, the test and its output are in the review. The full output of every command is in the session's transcript at `GET /o/{org}/sessions/{id}/messages`.

The review comments and pushes nothing. It also says what it could not judge. Replication lag does not exist in a sandbox where reader and writer are one server, so read-after-write findings come from reading the code. How long a migration holds a lock on a table with production row counts was not measured, and the review says so next to any revision that alters an existing table.

## Run it on every pull request

Create an automation in the Project that holds the repository with this prompt, read-only switched on, and a `github` trigger on `pull_request.opened` and `pull_request.synchronize`. Each pull request then gets a review when it opens and after each push, with the pull request placed after the prompt and nothing filled in. Read-only lets the run read the repository and comment, and stops it pushing. The trigger fires for owners, members and collaborators, never for forks or bots.

Starting a database for a pull request that only edits a README is wasted work. A trigger filters on base branch, label and author, not on changed paths, so a team that wants this review only for data-layer changes uses `pull_request.labeled` with a label such as `database` in `labels` and adds the label when a pull request touches models, repositories or `migrations`. The automation's max runs per hour caps how many reviews start in a burst; events past it wait.

## Why run it on Rush?

A SQLAlchemy review that only reads the diff misses what the database would say. Whether a migration applies, whether it reverses, and whether the models and the schema still agree are questions for a running server, and a reviewer with the pull request open in a browser does not have one. The agent starts one in its sandbox for each pull request, with the user and databases the repository's own compose file creates, and the commands and their output stay with the run.

The other half is patience with expressions. `status == status` looks right at a glance, type-checks and passes a test that tries only a matching value. Finding it means reading each filter and asking which side is a column. The agent does that for every query in the diff, on every pull request, and several pull requests are reviewed at once, each in a separate sandbox with its own database, so none of them waits for another's test run.

## Frequently asked questions

### Does it need access to our real database?

No. It creates an empty database in its sandbox and builds the schema from your migrations, the way `tests/conftest.py` already does for every test. No connection string or production data is involved, and the request gives it none.

### We use PostgreSQL. Does the review still work?

Yes. The agent reads the driver from your configuration and the image from your compose file and installs that server instead. The checks are the same; the dialect matters for a few findings, such as how a reserved table name is quoted or what an identity column looks like.

### Will it catch N+1 queries?

It flags a relationship read inside a loop with no `selectinload` or `joinedload` on the query, from the code. In async code a lazy load raises instead of querying, so the test run shows those. An app with a single table and no relationships, like this example, gives that check nothing to find.

### Why compare the schema by hand instead of running autogenerate?

Autogenerate is only as good as the metadata it is given. If the Alembic environment does not import every model module, it compares the database with an incomplete picture and can propose dropping tables. The agent builds the schema from the migrations and reads the mapped classes directly, then reports what autogenerate would say as a second opinion.

### Can it fix what it finds?

Not in this job, which is read-only so that a review never changes a contributor's branch. A finding can become its own run: send the finding's text as the prompt with the repository in `resources`, and the agent opens a pull request with the fix and a test that fails without it.

## More SQLAlchemy jobs

- [Upgrade a FastAPI App from SQLAlchemy 1.4 to 2.0](https://getrush.ai/run/upgrade-sqlalchemy/sqlalchemy/)
- [Add Connection Pool Settings to a Flask and SQLAlchemy App](https://getrush.ai/run/add-connection-pooling/sqlalchemy/)
- [Add Cursor Pagination to a SQLAlchemy API](https://getrush.ai/run/add-cursor-pagination/sqlalchemy/)

## The same job on other stacks

- [Review Every Pull Request to a Streamlit App](https://getrush.ai/run/review-pull-requests/streamlit/)
- [Review Pull Requests Automatically in a SvelteKit App](https://getrush.ai/run/review-pull-requests/sveltekit/)
- [Review Pull Requests Automatically in a Symfony App](https://getrush.ai/run/review-pull-requests/symfony/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
