All jobs

Add Database Seeders to a CodeIgniter 4 App

Yes. An agent writes CodeIgniter 4 seeders that match your migrations, with Faker data, one entry point, a production guard and PHPUnit coverage, 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 what the database already looks like. In an app laid out like codeigniter4/appstarter, app/Database/Migrations and app/Database/Seeds hold only a .gitkeep, app/Models is empty, and app/Config/Database.php has a default group on MySQLi plus a tests group on SQLite3 with ':memory:', a db_ prefix and a constructor that switches defaultGroup to tests whenever ENVIRONMENT is testing. composer.json requires php ^8.2 and codeigniter4/framework ^4.7, and keeps fakerphp/faker ^1.9 and phpunit/phpunit ^10.5.16 in require-dev. The only working example is under tests/_support: Database/Migrations/2020-02-22-222222_example_migration.php creates a factories table with $DBGroup = 'tests', Database/Seeds/ExampleSeeder.php inserts three rows through $this->db->table('factories'), Models/ExampleModel.php reads them, and tests/database/ExampleDatabaseTest.php uses DatabaseTestTrait with protected $seed = ExampleSeeder::class and asserts findAll() returns three.

A seeder is a class under app/Database/Seeds extending CodeIgniter\Database\Seeder with a run() method, created with php spark make:seeder <Name> --suffix and run with php spark db:seed <Name>; inside it $this->db is the connection, $this->forge the forge, and $this->call('OtherSeeder') runs a child. For each table the app's own migrations create, the agent reads the createTable call and its addField columns to learn names, types, lengths and nullability, then writes one seeder per table and a DatabaseSeeder whose run() calls them in dependency order, parents before foreign keys. Each row set uses insertBatch rather than a loop of insert, and each seeder is re-runnable: it checks countAllResults() on its table and returns when rows exist, so db:seed DatabaseSeeder on a seeded database adds nothing.

Faker is a dev dependency, and the agent treats it that way. The framework's Seeder::faker() helper is marked deprecated and returns null when the package is missing, so the seeders call \Faker\Factory::create() directly, and DatabaseSeeder refuses to run when ENVIRONMENT is production, printing why with CLI::error, because a composer install --no-dev deploy has no Faker and a production database should not receive invented rows anyway. A seeded faker instance ($faker->seed(...)) keeps the generated data the same across runs, which is what makes a seeded development database comparable between two machines.

The tests reuse the fixture mechanism the starter ships. DatabaseTestTrait runs migrations from tests/_support/Database/Migrations by default and seeds from tests/_support/Database/Seeds, so for the app's own seeders the test sets $namespace to null (every namespace) and $basePath to app/Database, with $migrate = true, $refresh = true and $seed = DatabaseSeeder::class. It asserts seeNumRecords per table, seeInDatabase for a known row, and that a second $this->seed(DatabaseSeeder::class) leaves the counts unchanged. phpunit.dist.xml already points the tests group at the in-memory SQLite, and .github/workflows/phpunit.yml installs the sqlite3 extension and runs vendor/bin/phpunit --coverage-text, so the suite needs no server. The agent runs composer install, vendor/bin/phpunit and php spark db:seed DatabaseSeeder against a SQLite file it points the default group at in .env for the demonstration.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Add database seeders to this CodeIgniter 4 app. Read composer.json, app/Config/Database.php, app/Config/Migrations.php, env, phpunit.dist.xml, every file under app/Database/Migrations and app/Models, tests/_support/Database/Migrations, tests/_support/Database/Seeds/ExampleSeeder.php, tests/database/ExampleDatabaseTest.php and .github/workflows/phpunit.yml first. For each table the app's migrations create, write a seeder under app/Database/Seeds that builds rows with Faker matching the migration's column types and lengths, inserts them with insertBatch, and returns without inserting when the table already has rows. Add a DatabaseSeeder that calls them parents first and refuses to run when ENVIRONMENT is production. Write a PHPUnit test with DatabaseTestTrait that migrates all namespaces, seeds DatabaseSeeder, asserts the row counts and a known row, and checks that seeding twice does not duplicate. Document php spark db:seed DatabaseSeeder in the README. Run vendor/bin/phpunit and the seed command against SQLite, then open a pull request that quotes 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

The pull request adds one seeder per table plus app/Database/Seeds/DatabaseSeeder.php, a test under tests/database, and a README section with the db:seed command and the environment rule. Its description quotes the phpunit --coverage-text output and the Seeded: lines php spark db:seed DatabaseSeeder prints for each class, followed by the same command run again with no new rows.

A reviewer checks the dependency order in DatabaseSeeder against the foreign keys in the migrations, whether any column the seeders fill has a uniqueness rule Faker could violate at the chosen row count (an email column with a unique key wants unique()->safeEmail()), and whether the idempotence rule, skip when rows exist, is the behaviour they want or whether a --refresh style truncate is better for their development flow.

The migrations are untouched, and so are the test-support fixtures. A database that has never been seeded gets rows; one that has is left alone; production is refused.

Run it on every new migration

A table without a seeder is the usual way a development database drifts from the schema. An automation on the Project with a github trigger on pull_request.opened and pull_request.synchronize runs read-only with a prompt that lists the createTable calls in app/Database/Migrations and the classes in app/Database/Seeds, and comments on the pull request when a migration adds a table that no seeder fills, or drops a column a seeder still writes. The event the trigger adds names the pull request; the prompt has no placeholder to fill.

A handler on your own server reading that comment sends this body to POST /o/{org}/sessions with the table's name appended to the prompt and the pull request's branch in resources as https://github.com/your-org/your-app/tree/<branch>, with a request_id that is a UUID v5 of the head SHA so a redelivered event returns the run it already started. That run pushes the seeder to the branch, and the key that started it receives run.completed at its webhook_url.

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

Why run it on Rush?

Seeders are only right when they run against the real migrations, in order, on a fresh database, which is exactly the step people skip on a laptop with a database that has been around for months. The agent migrates from zero in its own sandbox, seeds, seeds again and keeps every command and its output in the run's record, so the pull request shows the seeders working rather than describing them.

A team with several CodeIgniter services has the same gap in each, with different tables. One run per repository reads that app's migrations and writes seeders to match, with the same entry point, the same production guard and the same idempotence rule across all of them.

Frequently asked questions

Why not use Seeder::faker()?

The framework marks it deprecated, and it returns null rather than failing when fakerphp/faker is not installed, which turns a missing dev dependency into a confusing error inside run(). Calling Faker\Factory::create() directly fails early with a clear message, and the production guard keeps it from being reached on a --no-dev install.

How does the test find the app's seeders?

DatabaseTestTrait defaults $basePath to tests/_support/Database/Seeds and $namespace to the test-support migrations. The test sets $basePath to app/Database and $namespace to null so it migrates every namespace, then names DatabaseSeeder in $seed.

Is it safe to run twice?

Yes. Each seeder returns when its table already has rows, and the test seeds twice and asserts the counts did not change. To rebuild from scratch, roll back and migrate first, or truncate the tables; the README section says which.

What stops it running in production?

DatabaseSeeder::run() checks the ENVIRONMENT constant and refuses with a CLI::error message when it is production. The child seeders are still callable one by one for a deliberate exception, and the message says so.

Does it need MySQL to test?

No. The tests group in app/Config/Database.php is SQLite in memory, Database.php switches to it whenever the framework runs under the testing environment, and the GitHub workflow installs the sqlite3 extension, so the seeder test runs anywhere PHPUnit does.

More CodeIgniter jobs

The same job on other stacks

Keep reading