# Add updatedAt Columns That Actually Update to a Drizzle Schema

Yes. An agent adds updatedAt to every Drizzle table your code updates, sets $onUpdate so each update stamps it, proves it on Postgres and opens a PR.

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

## What the agent does

The agent reads the schema against the queries, because an `updatedAt` column is only as good as the statements that touch its table. In an app laid out like vercel/chatbot, `lib/db/schema.ts` declares seven Postgres tables with `drizzle-orm` 0.45.2. `User` has `createdAt` and `updatedAt`, both `.notNull().defaultNow()` and nothing else, so `updatedAt` is set once at insert and never again. `Chat`, `Message_v2`, `Document`, `Suggestion` and `Stream` carry a `createdAt` the code fills with `new Date()` at insert time, and `Vote_v2` has no timestamp at all. `lib/db/queries.ts` then updates four of those tables: `updateMessage` sets `parts`, `voteMessage` flips `isUpvoted`, `updateDocumentContent` writes `content` on the latest version of a document, and `updateChatVisibilityById` and `updateChatTitleById` change a chat's `visibility` and `title`. None of them can record when that happened.

It adds the column where an update statement exists, not everywhere. `chat`, `message`, `document` and `vote` each get `updatedAt: timestamp("updatedAt").notNull().defaultNow().$onUpdate(() => new Date())`, and `user` keeps its column with the same `$onUpdate` added. The `$onUpdate` hook runs in Drizzle's query builder: every `db.update(table).set({ … })` gets `updatedAt` added to its `SET` clause unless the caller set it explicitly, which is why the agent does not touch the five update functions in `queries.ts` at all. `Document` is a special case it reads twice: its primary key is `(id, createdAt)` and each save in `saveDocument` is a new version row, while `updateDocumentContent` edits the newest version in place, so `updatedAt` on that table records an in-place edit of a version and not a new version. `components/chat/artifact.tsx` labels a document's `createdAt` as `Updated …`; the agent switches that label to the new column and leaves the version footer, which really is about versions, on `createdAt`.

The migration comes from the tools the repository already has. `drizzle.config.ts` points `drizzle-kit` 0.31.10 at `./lib/db/schema.ts` with `out: ./lib/db/migrations`, where `0000_initial.sql` and `meta/_journal.json` hold the one existing migration. `pnpm db:generate` writes `0001_<name>.sql` with five `ALTER TABLE … ADD COLUMN "updatedAt" timestamp DEFAULT now() NOT NULL` statements, which Postgres applies to existing rows by filling the default, so no backfill script is needed. The agent reads the generated SQL before committing it, checks that `pnpm db:check` passes, and does not run `db:push`, since `package.json` runs `tsx lib/db/migrate` before every `next build` and that is the path production takes.

Then it proves the hook fires. In its sandbox the agent installs Postgres from apt, starts it, creates a database and sets `POSTGRES_URL` in `.env.local`, runs `pnpm db:migrate`, and executes a short `tsx` script that inserts a chat with `saveChat`, calls `updateChatTitleById` a second later and reads the row back, asserting `updatedAt` is later than `createdAt`. It repeats the read for a raw `UPDATE` sent through `postgres` directly to show the hook is Drizzle-side only: a statement that bypasses Drizzle leaves `updatedAt` alone, which the pull request states plainly. It finishes with `pnpm check`, the repository's `ultracite` lint, and `pnpm exec tsc --noEmit`.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add updatedAt columns to the Drizzle schema for every table that an update statement in lib/db/queries.ts touches. Read lib/db/schema.ts, lib/db/queries.ts, drizzle.config.ts, lib/db/migrate.ts, lib/db/migrations and package.json first. Use timestamp(\"updatedAt\").notNull().defaultNow().$onUpdate(() => new Date()) and add the same $onUpdate to the existing User.updatedAt. Generate the migration with pnpm db:generate, read the SQL, and run pnpm db:check. Install Postgres in your sandbox, run the migrations against it, and prove with a tsx script that an update through Drizzle advances updatedAt and a raw UPDATE through postgres does not. Where the UI labels a createdAt as updated, switch it to the new column. Run pnpm check and tsc, and open a pull request that quotes the migration SQL and the proof script's output.",
  "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 with three kinds of change: the schema (`lib/db/schema.ts`, five columns gaining `$onUpdate` and four tables gaining the column), the generated migration under `lib/db/migrations/` with its journal entry, and one label in `components/chat/artifact.tsx`. The description quotes the migration SQL, the proof script with the two timestamps it read back, and the lint and type-check output.

The run's record keeps the Postgres install, the migration run and the script's output, so a reviewer can see the `UPDATE … SET "title" = $1, "updatedAt" = $2` Drizzle generated rather than take the hook on faith.

Before merging, a reviewer decides two things the pull request names: whether `Suggestion` and `Stream`, which nothing updates today, should get the column now so the next feature does not need a migration, and whether the sidebar in `components/chat/sidebar-history.tsx` should keep grouping chats by `createdAt` or move to `updatedAt`, which changes the order a user sees and is a product call rather than a schema one.

## Run it on every Linear issue labelled schema

An automation in the Project with a `linear` trigger runs this prompt when an issue gets the `schema` label, with the issue's text added after the prompt, so a ticket that says which tables need the column drives one run and one pull request. The automation runs as the member who last changed it and caps itself with `max_runs_per_hour`.

A team with several Drizzle services starts one run per repository from a script, `POST /o/{org}/sessions` with this `input` and that repository in `resources`, and a `request_id` that is a UUID v5 of the repository's full name so a retried call returns the run it already started. The key's `webhook_url` gets a `run.completed` event per run with the session's address, from which the script reads the pull request.

## Why run it on Rush?

The change is small and the proof is not: it needs a Postgres, the migration applied, and a script that updates a row and reads it back, which is exactly what people skip when the diff looks obvious. The agent does all of it in its own sandbox and keeps the output, so the pull request shows the generated `SET` clause and the two timestamps rather than asserting the hook works.

Across several services the runs go in parallel, one sandbox each, and each reads its own schema and queries. A service whose updates go through raw SQL, or whose tables already have the column, gets a pull request that says so instead of a blind copy of this one.

## Frequently asked questions

### Why not a database trigger instead of $onUpdate?

A trigger catches every `UPDATE`, including raw SQL, and `$onUpdate` catches only statements Drizzle builds. The agent picks `$onUpdate` because it lives in the schema file next to the column, needs no custom SQL in the migration, and matches how the app writes today: every update in `lib/db/queries.ts` goes through `db.update`. The pull request names the trigger option for a reviewer who wants the stronger guarantee.

### Does the migration lock the tables?

`ADD COLUMN … DEFAULT now() NOT NULL` on Postgres 11 and later stores the default in the catalog and does not rewrite the table, so it takes a brief lock and no long scan. The agent states the Postgres version it tested on in the pull request so a reviewer can check their own.

### Will existing rows have a meaningful updatedAt?

No. They get the time the migration ran, because that is the default. The pull request says so, and if the team wants `updatedAt` to equal `createdAt` for old rows, that is one `UPDATE … SET "updatedAt" = "createdAt"` the agent can add to the migration on request.

### What about updateChatTitleById's empty catch?

That function swallows every error as a best-effort title update, so a failed stamp would be invisible there too. The agent leaves the behaviour as it is, since changing error handling is outside this change, and notes it in the pull request as a follow-up.

## More Drizzle ORM jobs

- [Migrate a Drizzle Schema from SQLite to PostgreSQL](https://getrush.ai/run/migrate-sqlite-to-postgres/drizzle/)
- [Review Drizzle Migrations in Every Pull Request](https://getrush.ai/run/review-database-migrations/drizzle/)
- [Upgrade Drizzle ORM and Drizzle Kit](https://getrush.ai/run/upgrade-drizzle-orm/drizzle/)

## The same job on other stacks

- [Fix updatedAt Columns That Never Update in a Prisma Schema](https://getrush.ai/run/add-updated-at-timestamps/prisma/)
- [Add updated_at Timestamps to SQLModel Tables](https://getrush.ai/run/add-updated-at-timestamps/sqlmodel/)

## More jobs like this: Turn a Linear label into a PR

- [Add Muting Users to a Rails App from a Linear Issue](https://getrush.ai/run/add-user-mute/rails/)

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