All jobs

Add Database Migrations to a GORM App

Yes. An agent replaces AutoMigrate with goose SQL migrations, adopts your existing database, proves SQLite and Postgres apply cleanly, and opens a PR.

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 reads how the schema is created today before it writes a migration, because in a repository like agn-7/gin-crud the answer is two calls in models/setup.go. ConnectSQLite opens test.db with gorm.io/driver/sqlite and runs db.AutoMigrate(&Album{}); ConnectPostgres opens a hard-coded DSN (host=localhost user=ifsguid_usr dbname=ifsguid_db) with gorm.io/driver/postgres and migrates nothing, and main.go discards the error it returns, so the server starts and logs failed to connect when no Postgres is there. go.mod says go 1.21 and resolves gorm.io/gorm v1.25.5, gorm.io/driver/sqlite v1.4.3, gorm.io/driver/postgres v1.5.4, gin v1.9.0 and mattn/go-sqlite3, most of them marked // indirect although imported directly. A .gitognore file, misspelled, is why test.db is committed.

models/album.go holds three structs. Album is ID uint, Title, Artist and Price. Interaction and Message are meant for Postgres: uuid primary keys, indexed timestamps, Settings json.RawMessage, Interaction.Messages with foreignKey:InteractionID, and Message.Interaction tagged foreignKey:ID, which points the foreign key at the message's own primary key. The committed test.db shows what an older GORM made: "id" integer primary key autoincrement. Running the same AutoMigrate(&Album{}) with the pinned GORM on an empty file makes `id` integer ... PRIMARY KEY (`id`) with no AUTOINCREMENT, so the schema in git and the schema the code creates already disagree. On Postgres it is worse: AutoMigrate(&Interaction{}, &Message{}) on an empty database fails with ERROR: relation "interaction" does not exist, because GORM tries to create message first with a foreign key to a table it has not created, a consequence of the circular tags.

So the agent writes the schema down as SQL and lets a migration tool apply it. It adds pressly/goose/v3 and a migrations/sqlite/00001_create_albums.sql whose -- +goose Up block uses CREATE TABLE IF NOT EXISTS albums with the AUTOINCREMENT the committed file has, so running goose up against the existing test.db records version 1 and leaves the table as it is, which the agent checks with .schema before and after. For Postgres it writes migrations/postgres/00001_create_interaction_message.sql with interaction first, then message whose interaction_id references interaction(id) with the ON UPDATE CASCADE ON DELETE SET NULL the struct tag asks for, and the four indexes; goose up applies it against a Postgres in the sandbox and \d message shows the single, correct foreign key. The circular foreignKey:ID tag is reported, not silently fixed.

In code, ConnectSQLite and ConnectPostgres stop calling AutoMigrate and instead call goose.Up on the *sql.DB GORM exposes, with each dialect's migrations embedded through //go:embed, so a binary carries its own schema history. main.go stops discarding the Postgres error. The agent runs go vet ./..., builds, starts the server with both databases, exercises POST /api/v1/db/albums and GET /api/v1/db/albums, and runs goose status on each database to show the recorded versions.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Replace AutoMigrate with versioned migrations in this Gin and GORM app. Read go.mod, main.go, models/setup.go, models/album.go, controllers/albums.go and .gitognore first, inspect the committed test.db with sqlite3, and try AutoMigrate for every model against an empty SQLite file and an empty Postgres database in your sandbox to see what it creates and where it fails. Add pressly/goose/v3 with a migrations/sqlite and a migrations/postgres directory, embedded with go:embed. Write the SQLite migration so that goose up adopts the committed test.db without changing its albums table, and write the Postgres migration for interaction and message with the foreign key the struct tags intend, not the circular one. Replace the AutoMigrate calls in models/setup.go with goose.Up on each connection, stop main.go from discarding ConnectPostgres's error, and run go vet, the server and goose status against both databases. Do not change the model structs; report the circular foreignKey tag and the AUTOINCREMENT drift as findings. Open a pull request that pastes the 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 that adds migrations/sqlite/00001_create_albums.sql, migrations/postgres/00001_create_interaction_message.sql and an embed.go beside them, and changes models/setup.go, main.go, go.mod and go.sum; the model structs and the controllers are untouched. The description pastes goose up and goose status for both databases, the .schema albums output before and after adoption, and the go vet and server log lines.

Three findings lead the description. AutoMigrate with the pinned GORM would create albums without AUTOINCREMENT, unlike the committed file, so ids could be reused after a delete on a fresh database. AutoMigrate(&Interaction{}, &Message{}) cannot run at all on an empty Postgres, failing on relation "interaction" does not exist, because Message.Interaction is tagged foreignKey:ID. And ConnectPostgres embeds credentials in source and its failure was ignored, which the pull request now surfaces as a startup error.

Before merging, a reviewer decides whether the Postgres migration should match the structs as tagged or the structs should be fixed to match the migration, since the two cannot both be true, and runs goose up once against any existing Postgres database so the version table exists before the new binary starts.

Run it on every pull request that changes a model

An automation in the Project with a github trigger on pull_request.opened and pull_request.synchronize runs a shorter prompt against a pull request that touches models/: write the goose create migration for the struct change, apply it to a fresh database of each dialect, run goose status, and report on the pull request. The trigger adds the pull request to the prompt, and pull requests from forks never start a run.

A team with several Go services still on AutoMigrate sends one POST /o/{org}/sessions per repository from a script, with this prompt and the 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 already started. Each run opens at most one pull request.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

AutoMigrate hides what it does until it runs against a database that matters. Here it would create a different albums table than the one in git, and it cannot create the Postgres tables at all; neither shows up until someone points the app at an empty database. The agent runs both cases in its own sandbox, against a SQLite file and a Postgres it installs, reads the DDL and the error, and writes migrations that match the database the project actually has, before any of it reaches a review.

Every command and its output stays on the run, so a reviewer reads the failing AutoMigrate, the .schema diff and the passing goose up without installing Postgres or a C compiler for go-sqlite3. Several GORM services can be moved at once, each in its own sandbox, and a model whose tags contradict themselves is reported as a finding instead of being quietly rewritten.

Frequently asked questions

Why goose rather than GORM's own migrator?

AutoMigrate is not versioned: it diffs structs against the live database each start, never drops a column, and here produces different DDL than the committed file and fails on the Postgres models. goose keeps numbered SQL files, a goose_db_version table, up and down blocks and a status command, and runs as a library on the *sql.DB GORM already holds, so no second connection is needed.

Will the first goose up break my existing SQLite database?

No. The SQLite migration uses CREATE TABLE IF NOT EXISTS albums with the same column types and AUTOINCREMENT as the committed test.db, so against that file goose records version 1 and the table's DDL is unchanged; the agent checks the .schema output before and after. A fresh file gets the same table from the same statement.

What is wrong with the Postgres models?

Message.Interaction is tagged foreignKey:ID, so GORM reads the message's own primary key as the foreign key column and emits a constraint from interaction.id to message.id beside the intended one from message.interaction_id to interaction.id. On an empty database the create order fails with relation "interaction" does not exist. The migration writes only the intended key; fixing the tag is a one-line follow-up the pull request names.

Does the server still start without Postgres?

Today it does, because main.go ignores the error from ConnectPostgres and the /api/v1/db/interactions route would then dereference a nil connection. The pull request makes a failed Postgres connection a startup error so it is seen; if the team wants Postgres optional, the reviewer can gate the route instead, and the description says where.

Why keep the migrations embedded in the binary?

So the deployed program and its schema history are one artifact: goose.Up reads the embed.FS, no migrations/ directory needs to exist at runtime, and a container built from the Dockerfile has nothing extra to copy. The goose command line still works against the same files in development.

More GORM jobs

The same job on other stacks

Keep reading