All jobs

Upgrade TypeORM from 0.2 to 0.3

Yes. The agent upgrades TypeORM with the packages pinned to it, rewrites every 0.2 find call, moves ormconfig.json into a DataSource and tests the queries.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent first works out what else has to move, because TypeORM 0.2 is rarely pinned alone. In a NestJS app like lujakob/nestjs-realworld-example-app, package.json has typeorm ^0.2.24, @nestjs/typeorm ^7.0.0, @nestjs/core ^7.0.5, rxjs ^6.5.5 and typescript ^3.8.3. On npm, @nestjs/typeorm 7 accepts only typeorm ^0.2.7; the first release whose peer range is ^0.3.0 is 8.1.0, and it requires Nest ^8.0.0 and rxjs ^7.2.0. So the smallest honest version of this upgrade is TypeORM 0.3 with Nest 8 and RxJS 7. It installs a MySQL-compatible server in the sandbox, since src/tag/tag.controller.spec.ts calls TypeOrmModule.forRoot() against a real database, and records the build and test results as the baseline.

Configuration moves next. src/app.module.ts calls TypeOrmModule.forRoot() with no arguments and relies on an ormconfig.json that .gitignore excludes; only ormconfig.json.example is committed. The 0.3 command line no longer reads that file: migration:run requires -d with the path to a file that exports a DataSource. Recent @nestjs/typeorm releases build their DataSource from the options they are handed and do not look for the file either. The agent writes one options module that reads the connection settings from environment variables and feeds both forRoot(options) and the CLI, and replaces the Connection injected into ApplicationModule with DataSource.

Then the queries, which is where 0.3 changes behaviour and not only names. The three service files hold 24 findOne calls, 23 of them in a 0.2 form: sixteen pass conditions directly, as in findOne({username}), five pass a bare id, and two pass a variable. On 0.3 both forms throw You must provide selection conditions in order to find a single row. The agent rewrites them to findOneBy({ ... }) or findOne({ where: ... }). The compiler finds the literal ones; it does not find ArticleService.findOne(where), whose parameter has no type in a project with noImplicitAny off, so the agent reads every call site and does not trust a green build. One call is worse than an exception. findFeed runs this.followsRepository.find({followerId: userId}), and 0.3 treats that object as options with no where: it returns every row in the table. The compiler flags that literal, but a cast or a transpile-only build lets it through, so it becomes findBy and gets a test.

Two more changes are about safety and the next upgrade. In 0.3, a where whose value is undefined is skipped, so findOneBy({ id: undefined }) returns the first row of the table. AuthMiddleware verifies a token and passes decoded.id to UserService.findById; rewritten naively, a token with no id claim signs in as the first user. The agent rejects a missing id in the middleware and sets invalidWhereValuesBehavior to throw on undefined, an option current 0.3 releases have, in the data source. It also replaces the three global getRepository(...) calls with the injected repositories. They still work in 0.3.31, where they are deprecated, and are gone from 1.x, the line npm now tags latest. New tests exercise each rewritten query against the database.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Upgrade TypeORM in this repository from 0.2 to the newest 0.3 release. Read package.json, the lockfile, every module that configures TypeORM, any ormconfig file or example, every entity, every service or repository that calls find, findOne, delete, getRepository, getConnection or createQueryBuilder, the auth code that loads a user by id, and the tests first. Check on npm which versions of the framework's TypeORM integration accept TypeORM 0.3 and what else they require, and state the full set of packages that must move together before changing anything. Install a MySQL-compatible server from system packages, create the database and the config the app expects, and record build and test results as the baseline; list anything that already fails. Upgrade the packages. Replace config-file loading with one DataSource options module read from environment variables and used by both the app and the TypeORM CLI. Rewrite every 0.2-style find and findOne call to findBy, findOneBy or an options object with where; do this by reading every call site, including ones with untyped arguments the compiler cannot check, and list them. Replace global getRepository, getConnection and getManager calls with injected ones. Make sure no lookup can run with a missing id: validate ids before querying and set invalidWhereValuesBehavior so that a where condition with no value throws. Add tests against the real database for each rewritten query, including that a missing id finds nothing. Do not turn synchronize on or off and do not go on to TypeORM 1.x; list what that would need as a follow-up. Open a pull request with the package set, the list of rewritten calls and the build and test output from before and after.",
  "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 whose description starts with the package set and why each member is in it, quoting the peer ranges that forced the framework and RxJS along. Below that is a table of every rewritten call: file, function, the 0.2 form and the 0.3 form, with the calls the compiler could not see marked, and the find that would have returned the whole table called out at the top.

It includes the new data source module and the environment variables it reads, the tests added for the rewritten queries, and build and test output from before and after against the sandbox's database. Bugs that predate the upgrade are listed and left alone. In this fixture findAll reads author.favorites from a user loaded without that relation, which fails with or without the upgrade.

The reviewer checks the two places a mistake is silent. One is any remaining call that passes conditions without where, since on 0.3 some of those throw and some quietly match everything. The other is the id path through authentication. The description ends with what a move to 1.x would involve: the removed globals are already gone from this code, and the remaining items are the Node version 1.x requires and a framework integration release that accepts it.

Run it on every Monday morning

An automation in the Project that holds the repository, with this prompt and a schedule trigger whose cron is 0 8 * * 1 in your timezone, starts one run a week. The first run does the migration described here. Later runs find the 0.3 line already in place, bump it to the newest patch if there is one, rerun the query tests and open a small pull request, or end with a short answer when nothing is newer.

The automation runs as the member who last changed it, and its own max runs per hour caps it. For a set of services on the same old version, a script can instead send this body to POST /o/{org}/sessions once per repository, each with a request_id that is a UUID v5 of the repository name and the target version, so a rerun of the script returns the runs that already exist.

The use case this job follows, with its automation and trigger: Upgrade dependencies weekly.

Why run it on Rush?

This upgrade fails at runtime, not at compile time, so it has to be run against a database to be believed. In its own sandbox the agent installs the server, builds the app on the new versions and executes every rewritten query, including the one that would have matched the whole table. The commands and their output are kept in the transcript at GET /o/{org}/sessions/{id}/messages, where a reviewer can see the output of each test run.

It is also a long chain of small forced moves: a peer range, a framework major, a config format, two dozen call sites. A run follows that chain to the end on a clean checkout, and what comes back is one pull request with the evidence attached.

Frequently asked questions

Why not go straight to TypeORM 1.x?

Because 0.3 is where the query API changed, and it still exports the deprecated 0.2 globals, so the app can be moved and tested in steps. The 1.x line removes those globals and requires a recent Node release, and the framework integration that accepts it requires a much newer Nest than this app has. The agent treats it as a second pull request once this one is merged.

Does the database schema change?

It should not, and the agent checks. It dumps the schema before the upgrade, starts the app on the new version and dumps it again. With synchronize: true, as the example config has, any difference in how 0.3 maps a column would be applied on startup, so a difference in the dumps is reported before anything is merged.

Will the compiler catch every call that needs rewriting?

No. It catches an object literal passed where options are expected. It cannot catch a call whose argument is typed any, comes from an untyped parameter, or is a bare variable, and those are the ones that fail in production. That is why the prompt asks for a list of call sites read by hand and a test for each.

More TypeORM jobs

More jobs like this: Upgrade dependencies weekly

Keep reading