# Add Seed Data to a Vapor App

Yes. An agent adds a Fluent seed migration that runs only in development, keeps the testing environment empty, proves it with swift test and opens a pull request.

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

## What the agent does

The agent clones the repository into its own cloud sandbox and reads how the database is set up before it writes a row. In a project generated from vapor/template with Fluent, `configure.swift` picks the driver from environment variables with defaults such as `vapor_username` and `vapor_database`, or `db.sqlite` for SQLite, calls `app.migrations.add(CreateTodo())` and then `routes(app)`. `CreateTodo` is an `AsyncMigration` that creates the `todos` table with an id and a required `title` string, `Todo.swift` is the model with `toDTO()`, and `TodoController` serves `GET` and `POST /todos` plus `DELETE /todos/:todoID`. `entrypoint.swift` detects the environment with `Environment.detect()` before `configure` runs, which is what makes an environment-gated migration possible.

`Tests/AppTests/AppTests.swift` matters as much as the app. Its `withApp` helper calls `Application.make(.testing)`, `configure(app)`, `app.autoMigrate()`, runs the test and then `app.autoRevert()`, and `getAllTodos` inserts two todos and expects exactly those two back. Any seed that ran in the testing environment would break that test, so the agent treats the empty testing database as a requirement, not an accident.

It adds `Migrations/SeedTodos.swift`, an `AsyncMigration` whose `prepare` creates a few `Todo` rows with fixed titles and whose `revert` deletes exactly those titles, so `migrate --revert` leaves the table as `CreateTodo` made it. In `configure.swift` it registers the migration only when `app.environment == .development`, after `CreateTodo`, with a comment saying why production and testing are excluded. `docker-compose.yml` already has a `migrate` service that runs the app's `migrate` command, so the agent updates its comment to say that `docker compose run migrate` in development seeds too.

Then it proves both sides: a new test asserts that `GET /todos` is empty under `.testing` after `autoMigrate`, and a second test that builds an `Application` with `.development`, runs `autoMigrate`, reads the seeded rows, runs `autoRevert` and reads none. It runs `swift build` and `swift test` against SQLite in the sandbox, and against Postgres when the project uses it, and opens a pull request with the migration and the test output.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add seed data to this Vapor app. Write a Fluent AsyncMigration that inserts a small, fixed set of rows for the existing model and removes exactly those rows on revert. Register it in configure.swift only when the environment is development, after the schema migrations, and leave testing and production without seeds. Add Swift Testing tests that show the testing database stays empty and that the development migration seeds and reverts cleanly, run swift build and swift test, and open a pull request that explains how to run the seed with the migrate command.",
  "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 on its own branch. The diff adds `Sources/App/Migrations/SeedTodos.swift`, a guarded `app.migrations.add` in `configure.swift`, the two tests in `AppTests.swift` and a note in the README and `docker-compose.yml` on running `migrate` in development. The description lists the seeded rows so a reviewer can see what a fresh development database contains.

The run's record keeps `swift build`, the `swift test` output with the existing todo tests and the two new ones, and the `migrate` and `migrate --revert` runs the agent made to watch the rows appear and disappear. Nothing in `.testing` or `.production` changes behaviour.

Before merging, a reviewer checks that the seed titles do not collide with real data a developer might create, that the environment check reads the same environment `entrypoint.swift` detects, and that CI, which runs tests in `.testing`, still sees an empty database.

## Run it on every new Fluent model

In the console, an automation with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` starts one run per pull request that adds a model or a schema migration, with the pull request as its context, in the Project that has the repository. The run proposes seed rows for the new model as a review comment or a commit on the branch, as the prompt says.

From your own server, the same work is one `POST /o/{org}/sessions` per repository, with the repository in `resources`. A webhook on the API key posts `run.completed` or `run.failed` when the turn finishes, with the `session_url` to read the pull request link and the test output.

## Why run it on Rush?

A seed migration is only right when it runs in one environment and not the others, and the way to know is to build the app in each and read the table. The sandbox has the Swift toolchain, SQLite and Postgres, so the agent runs `swift test` and the `migrate` command in both environments and keeps the output, instead of a reviewer reasoning about `Environment.detect()` from the diff.

The job touches a migration, the configuration and the tests together. One run produces one pull request with all three and the proof, and the same prompt runs against several Vapor services at once, each in its own sandbox.

## Frequently asked questions

### Why a migration rather than a script?

Fluent already tracks which migrations ran, so a seed migration runs once per database and can be reverted with `migrate --revert`. A script would need its own bookkeeping. The environment guard keeps it out of production.

### What if I want seeds in staging too?

The guard is one line in `configure.swift`. It can check for a custom environment name or an environment variable instead of `.development`; the pull request shows the check so the reviewer can widen it.

### Does it work with Postgres, MySQL and SQLite?

Yes. The migration uses the model's `create(on:)` and `delete(on:)`, not raw SQL, so it runs on every driver the template offers. The agent tests on SQLite in the sandbox and on the project's own driver when it is Postgres or MySQL.

### Will the existing tests change?

No. They run in `.testing`, where the seed is not registered, so `getAllTodos` still sees only the rows it inserted. The new tests are added beside them.

## More Vapor jobs

- [Add Soft Delete to a Vapor App](https://getrush.ai/run/add-soft-delete/vapor/)
- [Add Structured JSON Logging to a Vapor App](https://getrush.ai/run/add-structured-logging/vapor/)
- [Add Pagination to a Vapor API from a Linear Issue](https://getrush.ai/run/add-todo-pagination/vapor/)

## The same job on other stacks

- [Add Database Seeders to an AdonisJS API](https://getrush.ai/run/add-seed-data/adonisjs/)
- [Add Database Seeders to a CodeIgniter 4 App](https://getrush.ai/run/add-seed-data/codeigniter/)
- [Add a Seed Script to a Drizzle ORM Project](https://getrush.ai/run/add-seed-data/drizzle/)

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