# Move a Gin API From SQLite to Postgres

Yes. An agent switches your Gin and GORM API from the SQLite driver to Postgres, keeps SQLite for local runs, moves the tests to a real database and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-sqlite-to-postgres/gin/). 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 database is opened today. In a Gin API like `gothinkster/golang-gin-realworld-example-app`, `go.mod` is at `go 1.21` with `gin-gonic/gin v1.10.0`, `gorm.io/gorm v1.25.12` and `gorm.io/driver/sqlite v1.5.7`, which pulls in `mattn/go-sqlite3` and so needs cgo. `common/database.go` has `GetDBPath` reading `DB_PATH` with a default of `./data/gorm.db`, `Init` calling `gorm.Open(sqlite.Open(dbPath), &gorm.Config{})` and `SetMaxIdleConns(10)`, and a test pair: `TestDBInit` opens `TEST_DB_PATH` or `./data/gorm_test.db` with `logger.Info`, and `TestDBFree` closes the handle and `os.Remove`s the file. `hello.go` calls `common.Init()`, then `Migrate`, which runs `users.AutoMigrate()` and `db.AutoMigrate` for `ArticleModel`, `TagModel`, `FavoriteModel`, `ArticleUserModel` and `CommentModel`, before mounting the `/api` groups. `.env.example` lists `PORT`, `GIN_MODE` and `DB_PATH`.

The models decide what Postgres has to accept. `articles/models.go` gives `ArticleModel` a `Slug` with `uniqueIndex`, `Description` and `Body` at `size:2048`, a `many2many:article_tags` relation to `TagModel`, whose `Tag` is also `uniqueIndex`, and `Comments` through `ForeignKey:ArticleID`; every model embeds `gorm.Model`, so each table gets `deleted_at` and soft deletes. The agent checks each of those against the Postgres driver: `size:2048` becomes `varchar(2048)`, the unique indexes and the join table are created by `AutoMigrate` as before, and `gorm.Model`'s `uint` ids become `bigserial`. It also reads `users/unit_test.go`, which calls `common.TestDBInit()`, `AutoMigrate()` and `common.TestDBFree(test_db)` around each group of cases and re-creates the file between them.

Then it makes the change. `go get gorm.io/driver/postgres` joins `go.mod`; `common/database.go` gains a `DATABASE_URL` read, opens `postgres.Open(url)` when it is set and keeps `sqlite.Open(dbPath)` when it is not, so a developer with no Postgres still runs `go run hello.go` as the README describes. `TestDBInit` reads `TEST_DATABASE_URL` the same way, and `TestDBFree` stops removing a file and instead drops the tables the tests created in reverse dependency order, so the suite can run twice against one database. Any place the tests assume SQLite's lenient typing, such as a string compared to an integer id, is fixed. A `docker-compose.yml` with a `postgres` service and the two new variables in `.env.example` give the local setup, and the README's database section is updated.

Proof comes from the repository's own checks. `.github/workflows/ci.yml` runs `go mod download`, `go mod verify`, `bash ./scripts/gofmt.sh`, then `bash ./scripts/coverage.sh`, which walks every package directory and runs `go test -coverprofile` in each, on Go 1.21, 1.22 and 1.23, and a `lint` job runs `golangci-lint` v1.61 with the `staticcheck`, `unused` and `misspell` linters from `.golangci.yml`. The agent adds a `postgres` service to the `test` job with `TEST_DATABASE_URL` pointing at it, starts Postgres in its sandbox, and runs the suite once against Postgres and once with the variable unset to show the SQLite path still works. `go vet` and `gofmt -l` run last.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Move this Gin API from the SQLite GORM driver to Postgres. Read go.mod, hello.go, common/database.go, common/test_helpers.go, users/models.go, users/unit_test.go, articles/models.go, articles/unit_test.go, .env.example, scripts/coverage.sh, .golangci.yml and .github/workflows/ci.yml first. Add gorm.io/driver/postgres. Make Init open Postgres when DATABASE_URL is set and keep the SQLite file otherwise, and make TestDBInit do the same with TEST_DATABASE_URL. Replace the file removal in TestDBFree with dropping the tables the tests created, so the suite is repeatable against one database. Check every model tag, index and relation against Postgres and fix anything that relied on SQLite's typing. Add a postgres service to the CI test job, a docker-compose.yml for local use, the two variables to .env.example and a README note. Start Postgres in the sandbox, run go build, go vet, gofmt -l, bash ./scripts/coverage.sh against Postgres and again with the variables unset, and open a pull request that lists what changed and the test output for both paths.",
  "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 changes `go.mod` and `go.sum`, `common/database.go`, the test files that set up and tear down the database, `.env.example`, `.github/workflows/ci.yml` and the README, and adds `docker-compose.yml`. The description lists the model tags it checked against Postgres, the tear-down change, and the summaries of the coverage run against Postgres and the run against SQLite.

The session keeps the whole record: the Postgres start, each `go test` run with its coverage line per package, `go vet`, `gofmt -l` and the two `coverage.sh` runs, so a reviewer can see the suite passed on both databases. `GET /o/{org}/sessions/{id}/messages` returns it as events, and `GET /o/{org}/p/{project}/sessions/{id}/changes` returns the diff.

Before merging, a reviewer checks that `DATABASE_URL` wins over `DB_PATH` when both are set, that `TestDBFree` drops the `article_tags` join table before `tags` and `articles`, that the CI service's health check runs before the tests start, and that no test still assumes file-based isolation. A follow-up message to the same session adds a migration tool in place of `AutoMigrate`, and the sandbox still has Postgres running.

## Run it on every service on its last SQLite file

A backend team moving several small Go services off SQLite starts one run per repository: `POST /o/{org}/sessions` with that repository in `resources` and this prompt, with a `request_id` per repository so a retried request returns the run already started. Each run starts its own Postgres, and no two share a port.

Start the first one from your terminal, close the laptop, and read the pull request from the console or from `GET /o/{org}/sessions/{id}` when it is done. A `webhook_url` on the key posts `run.completed` for each run with a `session_url` for its pull request.

## Why run it on Rush?

A driver change is only believable with the suite run against the real database, which on a laptop means installing Postgres, creating a database, exporting two variables and remembering to run the suite twice. In a Rush sandbox the agent starts Postgres itself, runs `coverage.sh` against it and again against the file, and keeps both runs and every `go test` line in the record.

The pull request carries the proof instead of a promise: a reviewer reads the Postgres run in the transcript and sees the CI service that will repeat it. Several services can move in parallel, each in its own sandbox with its own database, each ending in its own pull request.

## Frequently asked questions

### Will I still be able to run it locally without Postgres?

Yes. `Init` keeps opening the SQLite file at `DB_PATH` when `DATABASE_URL` is not set, so `go run hello.go` works as the README describes. The `docker-compose.yml` is there for the day you want the local run on Postgres too.

### Does the schema change?

The models do not. `AutoMigrate` creates the same tables, unique indexes and the `article_tags` join table on Postgres; `size:2048` becomes `varchar(2048)` and `gorm.Model`'s ids become `bigserial`. The agent reads each tag and reports anything the Postgres driver treats differently.

### How do the tests stay isolated without a fresh file each time?

`TestDBFree` drops the tables the tests created, join table first, instead of deleting a file, and `TestDBInit` migrates them again. The suite can run repeatedly against one database and against the SQLite file unchanged.

### Can it drop the SQLite path entirely instead?

Yes. Say so in the prompt and the agent removes `gorm.io/driver/sqlite`, `DB_PATH` and the cgo dependency, and makes `DATABASE_URL` required. The default here keeps SQLite so the change is safe to merge before every environment has Postgres.

## More Gin jobs

- [Review Pull Requests on a Gin API](https://getrush.ai/run/review-pull-requests/gin/)
- [Security Audit of a Gin API](https://getrush.ai/run/security-audit/gin/)
- [Find Test Coverage Gaps in a Gin Service](https://getrush.ai/run/test-coverage-gaps/gin/)

## The same job on other stacks

- [Migrate a Sequelize App From SQLite to Postgres](https://getrush.ai/run/migrate-sqlite-to-postgres/sequelize/)
- [Move a Shopify App's Session Storage From SQLite to Postgres](https://getrush.ai/run/migrate-sqlite-to-postgres/shopify-app/)
- [Move a Socket.IO Chat From SQLite to PostgreSQL](https://getrush.ai/run/migrate-sqlite-to-postgres/socket-io/)

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