All jobs

Add Seed Data to a GORM App

Yes. An agent adds a seed command to a GORM app that fills the database with users, articles and tags through the models, runs it in its sandbox 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 database is opened and how rows are made before it writes any data. In an API laid out like realworld-apps/golang-gin-realworld-example-app, hello.go calls common.Init(), which opens SQLite at DB_PATH (default ./data/gorm.db, with TranslateError: true so unique violations become gorm.ErrDuplicatedKey), then a Migrate function in package main that runs AutoMigrate for users, follows, articles, tags, favourites, article users and comments. users.UserModel has a unique Email and a PasswordHash set only through the unexported setPassword, which uses bcrypt; articles.ArticleModel has a unique Slug made by makeUniqueSlug, tags through setTags and buildTagModels on a many2many table, and SaveOne helpers in both packages. The tests already build rows with userModelMocker(n) in users/unit_test.go against a second file named by TEST_DB_PATH. .gitignore ignores every *.db, .env.example lists PORT, GIN_MODE and DB_PATH, and ci.yml runs scripts/gofmt.sh, scripts/coverage.sh across three Go versions and golangci-lint. A fresh clone starts with no users, no articles and nothing to log in as.

The seed goes through the models, never through raw SQL, so it is subject to the same hashing, slugs and tag deduplication the API uses. The agent adds an exported SeedUsers to the users package, since only that package can call setPassword, and SeedArticles to articles, which creates articles for those users with SaveOne, tags with setTags and a few comments. A new cmd/seed/main.go opens the database with common.Init(), migrates, and calls the two in order. Migrate cannot be imported from package main, so the agent moves it into a small package that both binaries import; common cannot hold it without an import cycle, because users and articles import common.

The data is fixed, not random: named users with documented passwords so a frontend developer can log in through POST /api/users/login on a fresh clone, articles whose slugs and tags are known so a request for GET /api/articles?tag= returns something, and a comment or two. The seed is idempotent: each user is looked up by email with FindOneUser and skipped if present, each article by slug with FindOneArticle, so running the command twice leaves the counts unchanged, which is what a developer who forgot they already ran it expects.

In its sandbox the agent runs go run ./cmd/seed and then go run hello.go, logs in as a seeded user, lists articles and tags with curl, and reads the responses. It writes seed_test.go that seeds twice on TestDBInit and asserts the second pass adds nothing. It runs go test ./..., scripts/gofmt.sh and golangci-lint with the repository's .golangci.yml, since the CI will, and adds the seed command to the README's getting-started steps with the logins.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Add seed data to this Gin and GORM API. Read hello.go, common/database.go, users/models.go, articles/models.go, the unit tests in each package, .env.example, .gitignore, .golangci.yml, the scripts directory, .github/workflows/ci.yml and the README first. Add a cmd/seed command that opens the database the way the server does, migrates, and creates fixed users, articles, tags and comments through the existing model helpers so password hashing, slugs and tag handling match the API. Keep the password-setting path inside the users package by adding an exported seed function there. Make the seed idempotent by looking rows up before creating them. Move the migrate function out of package main so both binaries share it without an import cycle. In the sandbox, run the seed, start the server, log in as a seeded user and list articles and tags; add a test that seeds twice and asserts nothing is duplicated; run go test, the gofmt script and golangci-lint. Document the command and the seeded logins in the README. Open a pull request whose description quotes the login and article responses and the test 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 cmd/seed/main.go, a seed function in users and one in articles, a seed_test.go, and a package holding the moved Migrate; it changes hello.go to import it and the README to document the command and the logins. common/database.go, the routers and the serializers are untouched. The description quotes the login response for a seeded user, the article and tag listings from the running server, and the go test ./... output with the idempotence test named.

The run's record keeps the seed command's output, the server log and the curl responses, so a reviewer sees the rows as the API served them rather than as the seed describes them.

Before merging, a reviewer checks that no seeded password is one anybody would reuse, decides whether the seed should refuse to run when GIN_MODE is release, and confirms the moved Migrate is called from both binaries.

Run it on every repository without seed data

A team with several Go services sends this body once per repository from a script, with 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. GET /o/{org}/github lists what the Rush GitHub App reaches; the script keeps the ones with gorm.io/gorm in go.mod and no seed command.

Each run reads its own models. A service on Postgres seeds inside a transaction and rolls back on the first error; one whose models have no helper for a hashed field gets the exported seed function in that package; one that already has fixtures in its tests reuses their builders rather than inventing a second set. Give the key a webhook URL and Rush POSTs run.completed with each session_url as the runs finish.

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

Why run it on Rush?

Seed data written without running it tends to bypass the model layer, because the model layer is where the awkward parts are: an unexported password setter, a slug helper that appends a suffix, a tag table that must be deduplicated. In a Rush sandbox the agent runs the seed against the real database file, starts the real server and logs in with the rows it made, so the data it commits is data the API accepts.

The seed output and the server responses stay with the run, so the reviewer sees a login succeed rather than reading the code that should make it succeed. Several services run in parallel, each in its own sandbox with its own database file and its own pull request.

Frequently asked questions

Why go through the models instead of a SQL file?

Because the models own the rules: passwords are hashed with bcrypt in setPassword, slugs are made unique by makeUniqueSlug, and tags are deduplicated by buildTagModels. A SQL file that writes plain passwords or duplicate tags produces rows the API cannot use, and it breaks the next time a column changes.

Is it safe to run twice?

Yes. Each user is looked up by email and each article by slug before it is created, and the test the agent adds seeds twice and asserts the counts did not move. Running it on a database that already has real users adds only the seeded rows that are missing.

Why move the migrate function?

Migrate lives in package main in hello.go, which no other binary can import. The seed command needs the same tables, so the function moves into a small package both binaries import; common cannot host it because the model packages import common, which would be a cycle.

Does it change the tests' database?

No. The tests keep using TestDBInit and TEST_DB_PATH; the seed command writes to the file DB_PATH names, the same one the server opens. The idempotence test runs against the test file and removes it afterwards with TestDBFree.

What logins does the seed create?

Fixed ones, documented in the README beside the command, so a frontend developer on a fresh clone can log in without reading Go. The pull request lists them, and a reviewer can change the names before merging.

More GORM jobs

The same job on other stacks

Keep reading