# Add Migrations to a Sequelize Express App

Yes. An agent replaces sequelize.sync in your Express app with sequelize-cli migrations and seeders that build the same schema, diffs the two, and opens a PR.

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

## What the agent does

The agent reads how the schema is created today before it writes a migration, because the migrations must produce exactly the tables the models expect. In an app like the `express-main-example` in sequelize/express-example, `sequelize/index.js` builds one `Sequelize` instance with `dialect: 'sqlite'` and `storage: 'sqlite-example-database/example-db.sqlite'`, defines three models from `sequelize/models/` and applies `extra-setup.js`, where `orchestra.hasMany(instrument)` and `instrument.belongsTo(orchestra)` add an `orchestraId` foreign key. The models are `user` with a unique `username` validated by `/^\w{3,}$/`, `instrument` with `type` and a `purchaseDate` of type `DATE`, and `orchestra` with `name`, each with an explicit auto-increment integer `id`. The only schema step is `sqlite-example-database/setup.js`, run by `npm run setup-example-db`: `sequelize.sync({ force: true })` drops and recreates everything, then `bulkCreate` inserts four users and three orchestras and `createInstrument` adds ten random instruments per orchestra. `package.json` has `express` `^4.17.1`, `sequelize` `^6.3.3` and `sqlite3` `^5.0.0`, `engines` `node >= 10`, and `.npmrc` sets `package-lock=false`.

The agent adds `sequelize-cli` as a dev dependency and a `.sequelizerc` that points `config` at a new `sequelize/config.js`, `migrations-path` at `sequelize/migrations` and `seeders-path` at `sequelize/seeders`, so the CLI and the app read the same storage path. Three migrations create `users`, `orchestras` and `instruments`, the pluralised table names Sequelize derives from the model names, with the columns above plus the `createdAt` and `updatedAt` timestamps the models carry by default, a unique index on `username`, and `orchestraId` as a foreign key to `orchestras.id` with `onUpdate: 'CASCADE'` and `onDelete: 'SET NULL'`, which is what the association generated. Each `down` drops its table in reverse order.

A seeder replaces the dummy data in `setup.js` with `queryInterface.bulkInsert` calls for the same users, orchestras and instruments. Reading the seed exposed one thing the agent writes down rather than fixes: the usernames `jack-sparrow` and `white-beard` contain a hyphen, which the model's `^\w{3,}$` validation rejects on `create`; they only load today because `bulkCreate` skips validation by default, and `bulkInsert` skips it too. The pull request flags that so you can decide whether the regex or the data is wrong. `setup.js` and the `setup-example-db` script are replaced by `db:migrate`, `db:seed:all` and `db:migrate:undo:all` scripts, and `index.js` keeps `sequelize.authenticate()` on startup; `sync` is no longer called anywhere.

The proof is a schema comparison, not a claim. In its sandbox the agent runs the old `setup.js` against one SQLite file and the new `db:migrate` against another, dumps both with the `sqlite3` command line's `.schema`, and diffs them, allowing for the `SequelizeMeta` table the CLI adds. It then runs `db:seed:all`, starts the server and requests `/api/orchestras?includeInstruments` and `/api/users/1` to confirm the routes in `express/routes/` read the migrated tables, runs `db:migrate:undo:all` to prove the `down` path, and includes every command and its output in the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Replace sequelize.sync with sequelize-cli migrations in this Express application. Read the Sequelize setup, the models, the associations in extra-setup.js, the database setup script and package.json first. Add sequelize-cli with a .sequelizerc and a config file that uses the same SQLite storage path as the app. Write one migration per model producing exactly the tables, columns, timestamps, unique indexes and foreign keys that sync creates today, with working down methods, and a seeder that inserts the same example data. Replace the setup script with db:migrate, db:seed:all and db:migrate:undo:all scripts and remove every sync call. Prove the schemas match by running the old setup against one database file and the migrations against another and diffing the sqlite3 .schema output, then seed, start the server and request the REST routes, then undo all migrations. Report anything in the seed data that the model validations would reject instead of changing it. Include the commands and output. Open a pull request.",
  "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

What comes back is a pull request that adds `.sequelizerc`, `sequelize/config.js`, three files under `sequelize/migrations/`, one under `sequelize/seeders/`, and changes `package.json` (`sequelize-cli`, the `db:*` scripts, `setup-example-db` removed) and the README's setup section. `sqlite-example-database/setup.js` is deleted; `sequelize/index.js`, the models and the Express routes are unchanged.

The description quotes the `.schema` diff between the synced and migrated databases, the `db:migrate`, `db:seed:all` and `db:migrate:undo:all` output, and the JSON returned by `/api/orchestras?includeInstruments` after seeding, and flags the hyphenated seed usernames against the model validation. The commands and full output are in the transcript at `GET /o/{org}/sessions/{id}/messages`.

Before merging, a reviewer checks the foreign key actions match what the production database should do on delete, decides whether to fix the regex or the seed names, and plans when `db:migrate` runs in deployment now that startup no longer creates tables. A follow-up message to the same session can add a Postgres entry to `config.js` for production.

## Run it on every Sequelize service that still calls sync on startup

A team with several Node services on Sequelize starts one run per repository with one `POST /o/{org}/sessions` each, the repository in `resources` and this prompt as `input`. Each run reads its own models and associations, so a service with many-to-many tables gets junction-table migrations and one with `paranoid` models gets `deletedAt` columns, each proved by the same schema diff.

Send a `request_id`, a UUID v5 of the repository's full name, so a retried call returns the run it already started. The key's `webhook_url` reports when each run the key starts finishes, with the run's final answer in `result`.

## Why run it on Rush?

Writing migrations by hand means reproducing what `sync` inferred from models and associations, and the mistakes are silent: a missing timestamp column or a different foreign key action only shows up in production. The agent runs both paths in a sandbox of its own and diffs the real SQLite schema, so the pull request shows the two match rather than asserting it.

The seed data, the routes and the undo path are exercised in the same run with every command's output kept. Several services can be converted at once, each in its own sandbox with its own pull request and its own schema diff.

## Frequently asked questions

### Will the migrations match what sync created?

That is what the schema diff proves: the agent runs the old `setup.js` and the new migrations against separate SQLite files and compares the `.schema` output, including `createdAt` and `updatedAt`, the unique index on `username` and the `orchestraId` foreign key with its `CASCADE` and `SET NULL` actions. The only expected difference is the CLI's `SequelizeMeta` table.

### What happens to the example data?

It moves into a seeder that inserts the same users, orchestras and instruments with `bulkInsert`. The agent notes that two seed usernames contain hyphens the model's `\w` validation would reject on `create`, which `bulkCreate` and `bulkInsert` both skip, and leaves the choice to you.

### Does this work with Postgres or MySQL?

The migrations use `queryInterface` and Sequelize data types, so they run on any supported dialect; `config.js` gets one entry per environment and the app reads the same file. The schema diff in the sandbox uses SQLite because that is what this example runs on; say so in the prompt if you want the comparison on another database the agent can install.

### Why not keep sync for development?

Two schema sources drift. The migrations become the only definition, `db:migrate` replaces the setup script in the README, and `sequelize.authenticate()` on startup still fails fast when the database is missing. If you want a convenience script for a fresh local database, it chains `db:migrate` and `db:seed:all` rather than calling `sync`.

## More Sequelize jobs

- [Add Optimistic Locking to a Sequelize App](https://getrush.ai/run/add-optimistic-locking/sequelize/)
- [Add Pagination to a Sequelize Express API](https://getrush.ai/run/add-pagination/sequelize/)
- [Add Prometheus Metrics to an Express and Sequelize API](https://getrush.ai/run/add-prometheus-metrics/sequelize/)

## More jobs like this: Hand off a long task

- [Add a Modal to a Slack Bolt App](https://getrush.ai/run/add-modals/slack-bolt/)
- [Add Model Groups and Access Levels to a dbt Project](https://getrush.ai/run/add-model-groups/dbt/)
- [Add a Minimum Supported Rust Version Check to a Rust CLI](https://getrush.ai/run/add-msrv-check/rust-cli/)

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