# Replace TypeORM synchronize with Migrations

Yes. The agent turns synchronize off, generates a baseline migration that reproduces today's schema exactly, wires the CLI scripts and tests a fresh install.

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

## What the agent does

The agent starts from how the schema is made today. In a project like lujakob/nestjs-realworld-example-app (`typeorm` `^0.2.24`, MySQL), the committed `ormconfig.json.example` sets `"synchronize": true` and the README says tables for all entities are created on application start. That is convenient until an entity changes: on the next start TypeORM alters the live tables to match, and a renamed property is a dropped column and a new empty one. The real `ormconfig.json` is ignored by git, so the agent works from the example. It also notes how the app runs, `node index.js` with `ts-node/register`, because the TypeORM command line must load the same TypeScript entities and so runs as `ts-node ./node_modules/typeorm/cli.js`.

It installs a database server in the sandbox, creates two databases and lets the app synchronize the first one. That is the reference: six tables from five entities, the sixth being the `user_favorites_article` join table. Against the second, empty database it sets `synchronize` to `false`, adds `migrations` and `cli.migrationsDir` to the config, and runs `migration:generate -n InitialSchema`. The generated class has ten statements in `up`: six `CREATE TABLE` and four foreign keys. `migration:run` applies it inside a transaction and records it in a `migrations` table. Then comes the check that makes a baseline trustworthy: a schema-only dump of the migrated database is compared with a dump of the synchronized one, and the two must be identical apart from that bookkeeping table.

Existing databases need the opposite treatment. Their tables are already there, so running the baseline would fail on the first `CREATE TABLE`. They have to be told the baseline is applied without applying it. The 0.2.24 command line in this repository has no flag for that, so the agent writes the two statements into the pull request: create the `migrations` table and insert one row with the baseline's timestamp and name. It adds `typeorm`, `migration:generate`, `migration:run` and `migration:revert` scripts to `package.json` and updates `ormconfig.json.example` and the README's database section.

It also tests the generator against itself, and here the result depends on the server. On MariaDB 10.11, the server Debian's packages install in place of MySQL when this page was written, `migration:generate` against a database that TypeORM 0.2.24 synchronized a moment earlier produces a migration anyway: eight statements that drop and re-add two foreign keys and change four columns to the definitions they already have. The schema is in sync and the tool still reports a difference. A CI step that fails when `migration:generate` writes a file would fail on every run there. So the agent runs this check on the server the team uses in production, names the server and version in its report, and proposes the drift check only where the generator comes back empty.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Replace TypeORM's synchronize option with migrations in this repository, staying on the installed TypeORM version. Read package.json, the lockfile, the TypeORM config or its committed example, the module that configures TypeORM, every entity, how the app is started in development and production, the README's database section and the tests first. Install a database server of the kind the config names from system packages and say exactly which server and version you installed. Create two databases. Start the app once with synchronize on against the first and keep a schema-only dump as the reference. Then set synchronize to false, add the migrations settings and package.json scripts for generate, run and revert that work with the way this app loads TypeScript, and generate one baseline migration against the second, empty database. Run it, dump that schema and compare it with the reference; they must match apart from the migrations table, and any difference must be fixed in the migration, not in the entities. Write down how an environment whose tables already exist records the baseline as applied without running it, with the exact statements or flag for the installed version. Run generate again against the migrated database and report whether it comes back empty; if it does not, show the statements and do not add a drift check. Make the existing tests run against a migrated database. Do not change any entity, do not upgrade TypeORM and do not enable migrationsRun unless the README already promises tables on start; say which you chose. Open a pull request with the migration, the schema comparison and the output of each command.",
  "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

A pull request with one migration file under `src/migration`, the config example with `synchronize` off and the migration settings added, four new scripts and a rewritten README section. The description shows the schema comparison first: the command used for each dump and the empty diff between them. A reviewer who trusts nothing else can repeat those two commands.

Next is a section headed for existing environments, with the statements that record the baseline on a database that already has the tables, and a warning in plain words that running the baseline there will fail and that nobody should work around it by dropping tables. The report on the generator follows: whether a second `migration:generate` came back empty, on which server and version, and the statements it produced if not.

What the reviewer decides is rollout order. The config change and the baseline row have to reach each environment together: with `synchronize` off and no baseline recorded, the next deploy tries to create tables that exist; with the row recorded and `synchronize` still on, nothing is gained. The description gives the order for a staging and a production database and says how to confirm it worked, by listing applied migrations with `migration:show`.

## Run it on every service still using synchronize

This is handed off once per service, usually the day somebody notices `synchronize: true` in a production config. A developer sends this body to `POST /o/{org}/sessions` from a terminal or an editor's MCP client and returns to their own work; the pull request is on the session when the run completes. A `request_id`, a UUID made once for the job, makes a retried call return the same run.

For several services, a script sends the body once per repository with a `request_id` that is a UUID v5 of the repository name. The runs are independent, and a key starts as many at once as its `max_concurrent` allows while the rest wait as queued runs. Each pull request is reviewed and rolled out without reference to the others, since each service owns its schema.

## Why run it on Rush?

A baseline migration is only safe if it reproduces the current schema exactly, and the only way to know is to build both and compare them. In its own sandbox the agent installs a server, creates one database each way, dumps both and diffs them, and the commands with their output are kept in the transcript at `GET /o/{org}/sessions/{id}/messages`. The generator's false difference on MariaDB is the kind of fact that turns up only when the tool is run against a real server.

The job also needs two disposable databases and a clean checkout, which is awkward on a laptop that already has a development database with months of drift in it. The run starts from nothing each time, so what it generates describes the entities and not one developer's history.

## Frequently asked questions

### Why not keep synchronize in development and use migrations in production?

Because then the two schemas are built by different code and nothing checks that they agree. A developer changes an entity, it works locally, and production has no migration for it. With `synchronize` off everywhere, the change does not work locally either until its migration exists, which is the point.

### How should migrations run in production?

As a deploy step, `npm run migration:run`, before the new version takes traffic. TypeORM can also run pending migrations on startup through `migrationsRun`, which is simpler and means every instance that starts tries to apply them. The agent sets up the explicit step unless the prompt says otherwise and documents both.

### Will later migrations be generated correctly?

Often, and they still need reading. `migration:generate` compares entities with the database and writes the difference, and it cannot tell a rename from a drop and an add, so a renamed column arrives as data loss unless someone edits the migration. The README section the agent writes says to read every generated file before committing it.

### Does this differ on TypeORM 0.3?

The idea is the same and the commands are not. In 0.3 the CLI takes a data source file with `-d` and the migration path as an argument in place of `-n`, and `migration:run` gained a `--fake` option that records a migration without executing it, which replaces the manual insert for existing databases. The agent reads the installed version and uses its syntax.

## More TypeORM jobs

- [Add a Migration Check to CI for a TypeORM Project](https://getrush.ai/run/add-migration-check-to-ci/typeorm/)
- [Add Optimistic Locking to TypeORM Entities](https://getrush.ai/run/add-optimistic-locking/typeorm/)
- [Add Query Timeouts to a TypeORM + NestJS App](https://getrush.ai/run/add-query-timeouts/typeorm/)

## The same job on other stacks

- [Add Database Migrations to an Actix Web App](https://getrush.ai/run/add-database-migrations/actix/)
- [Add Alembic Migrations to an aiohttp App](https://getrush.ai/run/add-database-migrations/aiohttp/)
- [Add Database Migrations to a CodeIgniter 4 App](https://getrush.ai/run/add-database-migrations/codeigniter/)

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