# Make a Database Schema Change in a Rails App as a Team

An agent proposes an additive user_emails table, waits for approval, then adds dual-write, a rerunnable backfill and tests on disposable Postgres.

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

## What the agent does

In its first turn the agent read `app/models/user.rb`, `EmailConfirmable`, `db/schema.rb` and `doc/maintenance-tasks.md`. It found that `users.email` has no unique index, only the non-unique `index_users_on_lower_email`; uniqueness is a case-insensitive validation, so case-variant duplicates may already exist. It wrote only `db/migrate/20261102000000_create_user_emails.rb`, with a unique index on `lower(email)` for primary or verified addresses, hand-derived its SQL because the sandbox had no Ruby or Postgres yet, and asked for approval.

A follow-up approved it as proposed. The second turn added `UserEmail.sync!`, called from an `after_save` on `User` in the same transaction, so the existing `with_lock` in `confirm_email_with_token` covers the new rows. `User.find_by_email` checks the primary row first, then falls back to `users.email`, and `Maintenance::BackfillUserEmailsTask` reuses `sync!`.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Split users.email storage into a separate verified-emails table through a staged Rails change. First inspect User, EmailConfirmable, authentication lookups, schema/indexes, strong_migrations and maintenance-task guidance. In the first turn write only the proposed additive migration, show its SQL and rollback limits, and explain primary-address, case-insensitive uniqueness, unverified/pending-address and partial-backfill behavior. Retain old columns, do not apply to production, and end the turn waiting for a follow-up approving the design. Only after approval update models, authentication queries, confirmation flows and tests; use the existing MaintenanceTasks mechanism for a rerunnable backfill that preserves newer writes. Verify on disposable Postgres, including concurrent token consumption and partial backfill. Run focused tests and the supported suite, then open a PR with deployment order and evidence. Do not remove old columns or promote unverified addresses.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

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

Tried on rubygems/rubygems.org at 96a4bd1f7: 8 files changed, 275 insertions(+), 4 deletions(-). `bin/rails test` went from `5627 runs, 17216 assertions, 1 failures, 0 errors, 214 skips` to `5632 runs, 17261 assertions, 1 failures, 0 errors, 214 skips`, exit 1 both times: the one failure, `StoreVersionContentsJobTest` record file mode, fails on master too in this sandbox. Rubocop ended `1072 files inspected, no offenses detected`. The sandbox needed Postgres 18.6, OpenSearch 2.13.0, Ruby 4.0.9 and a built `tailwind.css`.

For the reviewer: a user whose login address differs from another's only by case is skipped and logged, so count those in production before switching reads. Backfilled `verified_at` uses `users.updated_at`, because the real verification time isn't stored. The backfill was not timed at production scale.

## Run it on every schema change a teammate starts

A teammate starts this prompt from the console in the app's Project. The first turn ends on the proposed migration and its SQL; the starter or an org admin replies with a follow-up approving it or choosing another offered option, such as moving confirmation tokens to `user_emails` now.

## Why run it on Rush?

The partial backfill was tested, not described. On 3,000 seeded users (6 soft-deleted, 300 with a pending address, 1 case-variant duplicate), stopping after 1,500 left 1,646 rows and 0 login lookup mismatches; the full run made 3,287 rows and a rerun added none. The concurrency test passed 5 of 5 runs, `EXPLAIN` shows login uses `index_user_emails_on_lower_email_claimed`, and the full suite caught a real regression: a user with a blank email broke `sync!`.

## Frequently asked questions

### Can a pending address be used to sign in?

No. A pending address is a row with `primary_address = false` and no `verified_at`, and only confirmation sets `verified_at`. The backfill sets it only where `email_confirmed` is true.

### What is the deployment order?

Deploy, run the backfill, confirm `Maintenance::BackfillUserEmailsTask.new.collection` is empty, then later pull requests switch reads and drop the old columns. Rolling back is `DROP TABLE user_emails`, lossless while `users` keeps every value.

## More Rails jobs

- [Plan a Linear Issue Against a Ruby on Rails Codebase](https://getrush.ai/run/plan-linear-issues/rails/)
- [Reproduce a Bug from a Linear Issue in a Rails App](https://getrush.ai/run/reproduce-linear-bug/rails/)
- [Review Database Migrations in Rails Pull Requests](https://getrush.ai/run/review-database-migrations/rails/)

## The same job on other stacks

- [Make a Database Schema Change in a Django App as a Team](https://getrush.ai/run/pair-on-schema-change/django/)

## More jobs like this: Steer a run as a team

- [QA a Next.js App in a Real Browser](https://getrush.ai/run/qa-in-browser/nextjs/)
- [QA a Nuxt App in a Real Browser](https://getrush.ai/run/qa-in-browser/nuxt/)

## 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/)
