# Add Database Migrations to a CodeIgniter 4 App

Yes. An agent writes your CodeIgniter 4 app's first migration, seeder and model with spark, runs migrate, rollback and seed, adds a SQLite test, and opens a PR.

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

## What the agent does

The agent reads the configuration before it generates anything, because CodeIgniter 4 already has a migration runner and the job is to use it the way the app is set up. In an app laid out like codeigniter4/appstarter, `app/Database/Migrations` and `app/Database/Seeds` are empty directories, `app/Models` is empty, and `app/Config/Database.php` has a `default` group on `MySQLi` with blank credentials and a `tests` group on `SQLite3` with `database` set to `:memory:` and `DBPrefix` set to `db_`, selected automatically when `ENVIRONMENT` is `testing`. `app/Config/Migrations.php` has `$enabled = true`, `$table = 'migrations'`, `$timestampFormat = 'Y-m-d-His_'` and `$lock = false`. The `env` file keeps the `database.default.*` lines commented out, `composer.json` excludes `**/Database/Migrations/**` from the classmap, and `tests/_support` already holds an `ExampleMigration` with `protected $DBGroup = 'tests'` that creates a `factories` table, an `ExampleSeeder`, and `tests/database/ExampleDatabaseTest.php` using `DatabaseTestTrait` with `$seed`.

Then it generates the files with the framework's own commands, so names and timestamps follow `Config\Migrations`: `php spark make:migration CreateUsersTable` writes `app/Database/Migrations/<timestamp>_CreateUsersTable.php`, and the agent fills `up()` with `$this->forge->addField()` for an auto-increment `id`, `email` and `name` as `varchar`, `created_at` and `updated_at` as `datetime`, a unique key on `email`, and `createTable('users')`, with `down()` dropping it. `php spark make:seeder UserSeeder` and `php spark make:model UserModel` follow, the model with `$table`, `$allowedFields`, `$useTimestamps` and `$validationRules` matching the columns. The prompt names the table and columns; nothing is invented.

Next it runs the lifecycle against a real database in the sandbox. It copies `env` to `.env`, sets `CI_ENVIRONMENT = development` and the `database.default.*` lines for a local MySQL it installs, then runs `php spark migrate`, `php spark migrate:status`, `php spark migrate:rollback`, `php spark migrate` again and `php spark db:seed UserSeeder`, pasting each command's output. `migrate:status` lists the new file beside the `Tests\Support` one, which is why the test needs a namespace.

The test is `tests/database/UserModelTest.php`: `CIUnitTestCase` with `DatabaseTestTrait`, `$migrate = true`, `$namespace = 'App'` so only `app/Database/Migrations` runs against the in-memory SQLite and the `factories` example stays out, `$seed = UserSeeder::class`, and assertions with `seeInDatabase` and `UserModel::find`. `vendor/bin/phpunit --coverage-text` runs it the way `.github/workflows/phpunit.yml` does on PHP 8.2 and 8.5 with the `sqlite3` extension, and the agent notes that `phpunit.dist.xml` already points the `tests` group at SQLite, so CI needs no database service.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add database migrations to this CodeIgniter 4 app. Read app/Config/Database.php, app/Config/Migrations.php, env, composer.json, phpunit.dist.xml, .github/workflows/phpunit.yml, tests/README.md and everything under tests/_support and tests/database first. Use php spark make:migration to create a users table with an auto-increment id, a unique email, a name and created_at and updated_at datetimes, with a down method that drops it. Use php spark make:seeder for a UserSeeder with two rows and php spark make:model for a UserModel with allowedFields, timestamps and validation rules. Configure .env for a local database, run php spark migrate, migrate:status, migrate:rollback, migrate and db:seed, and paste the output. Add tests/database/UserModelTest.php using DatabaseTestTrait with migrate true, namespace App and the seeder, and run vendor/bin/phpunit. Open a pull request with the files and the output. Do not commit .env.",
  "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

The pull request adds one file under `app/Database/Migrations`, one under `app/Database/Seeds`, `app/Models/UserModel.php` and `tests/database/UserModelTest.php`. Its description pastes the `migrate`, `migrate:status`, `migrate:rollback` and `db:seed` output and the PHPUnit result, and says that `.env` was created in the sandbox and not committed.

`PHPUnit` runs on the pull request as it does for every change, on both PHP versions, with the new test running on the in-memory SQLite `tests` group.

Before merging, a reviewer checks the column types against the database they deploy on, since `varchar` lengths and `datetime` behave differently on MySQL, Postgres and SQLite, decides whether `Config\Migrations::$lock` should be turned on for deployments that run `spark migrate` from more than one host, and whether the seeder is for development only.

## Run it on every new app from the starter

A team that starts apps from the starter runs this once per repository from its own server: one `POST /o/{org}/sessions` each, with that repository in `resources`, the table and columns in `input`, 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 reads its own `Database.php`, so an app already on Postgres gets types the `Postgre` driver accepts, and one with migrations present gets the next file in sequence and no seeder.

Afterwards, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` and the prompt "if this pull request adds a migration, check that down() reverses up(), that the file name follows Config\Migrations::$timestampFormat, and that a model's allowedFields match the columns; comment with file and line on anything that does not" reviews every later schema change. The trigger puts the pull request after the prompt; only pushes by an owner, member or collaborator fire it, never a fork's.

## Why run it on Rush?

A migration is only known to work once it has run forward, run backward and run forward again on a real database, and the starter ships with its credentials blank. The agent installs a database in its own sandbox, fills `.env` there, runs the whole cycle through `spark`, and keeps every command and its output with the run, so the pull request shows the migration applied rather than asserting it would be.

The database test it adds runs on the in-memory SQLite group that `phpunit.dist.xml` already selects, so CI proves the schema on every later change without a service container. For a team with many CodeIgniter apps, each gets its own run and its own pull request, fitted to its own driver and configuration.

## Frequently asked questions

### Why does the test set $namespace to App?

Because `DatabaseTestTrait` with `$migrate = true` and `$namespace = null` runs migrations from every namespace, including `Tests\Support`, whose `ExampleMigration` creates the `factories` table for the example test. Setting `App` runs only the migrations under `app/Database/Migrations`, so the test exercises exactly the file the pull request adds.

### Does the run need MySQL?

For the `spark migrate` and `rollback` output, yes: the `default` group is `MySQLi` and the agent installs a MySQL server in the sandbox and fills `.env` with its credentials. PHPUnit needs nothing extra, because the `tests` group is SQLite in memory and the `phpunit.yml` workflow installs the `sqlite3` extension.

### Will the migration work on Postgres or SQLite in production?

The `forge` calls are driver-neutral, and the test proves them on SQLite. The reviewer still checks the column types against their production driver; the pull request lists each column so that read takes a minute.

### Why not write the files by hand instead of running spark?

Because `make:migration` applies `Config\Migrations::$timestampFormat`, which the runner uses to find and order files; a hand-named file with the wrong format is silently skipped. Using the commands also gives the model and seeder the framework's current skeletons.

### Should the migration lock be turned on?

`$lock` is `false` in the starter. It prevents two deployments from running migrations at once and is worth turning on where `spark migrate` runs from more than one host. The agent leaves it as it is and names it in the pull request, since that is a deployment decision.

## More CodeIgniter jobs

- [Write a Dockerfile for a CodeIgniter 4 App](https://getrush.ai/run/add-dockerfile/codeigniter/)
- [Add File Uploads to a CodeIgniter 4 App](https://getrush.ai/run/add-file-uploads/codeigniter/)
- [Set Up GitHub Actions CI for a CodeIgniter 4 App](https://getrush.ai/run/add-github-actions-ci/codeigniter/)

## The same job on other stacks

- [Add Database Migrations to a Drizzle App](https://getrush.ai/run/add-database-migrations/drizzle/)
- [Add Versioned Database Migrations to an Echo App](https://getrush.ai/run/add-database-migrations/echo/)
- [Add Versioned Database Migrations to a Gin App](https://getrush.ai/run/add-database-migrations/gin/)

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