# Migrations & Seeding

## Introduction

Migrations are like version control for your database. In Laravel Bash, they're `.sql` files in `database/migrations`, run in the order their names sort.

```bash
./sail artisan migrate
./sail artisan migrate --seed
./sail artisan migrate:fresh --seed
```

## Writing Migrations

```sql
-- database/migrations/2026_12_04_000001_create_posts_table.sql
CREATE TABLE posts (
  id {{increments}},
  title VARCHAR(80) NOT NULL,
  body TEXT NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'draft',
  votes INTEGER NOT NULL DEFAULT 0,
  created_at {{timestamp}}
);
```

Most SQL is the same in SQLite and Postgres. The parts that aren't are written as placeholders, and the schema grammar fills them in with `sed`:

| Placeholder | SQLite | Postgres |
|---|---|---|
| `{{increments}}` | `INTEGER PRIMARY KEY AUTOINCREMENT` | `BIGSERIAL PRIMARY KEY` |
| `{{timestamp}}` | `TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP` | `TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP` |

```bash
DB::grammar() {
  case $DB_CONNECTION in
    sqlite) sed -e 's/{{increments}}/INTEGER PRIMARY KEY AUTOINCREMENT/g' ... ;;
    pgsql)  sed -e 's/{{increments}}/BIGSERIAL PRIMARY KEY/g' ... ;;
  esac
}
```

### Which migrations have run

Like Laravel, a `migrations` table records each one, and `migrate` skips those it finds there.

### Dropping everything

`migrate:fresh` deletes the SQLite file, or on Postgres drops and recreates the `public` schema, then migrates from the start.

## Seeding

Seeders live in `database/seeders`, one per file, and each one is a function called `<Name>::run`. `db:seed` and `--seed` run `DatabaseSeeder`, which runs the others with `call`, in order:

```bash
# database/seeders/DatabaseSeeder.sh
DatabaseSeeder::run() {
  call PostSeeder CommentSeeder
}
```

```bash
# database/seeders/PostSeeder.sh
PostSeeder::run() {
  local title status votes body
  while IFS='|' read -r title status votes body; do
    Post::create title="$title" status="$status" votes="$votes" body="$body" > /dev/null
  done <<'ROWS'
Introducing Laravel Bash|published|342|Laravel now runs on PHP, Rust, Go and the shell you already have.
Queues with mv|published|401|A rename is atomic, so four workers can share one folder.
ROWS
}
```

```text
   INFO  Seeding database.

  PostSeeder ............................................... RUNNING
  PostSeeder ............................................. 31ms DONE
  CommentSeeder ............................................ RUNNING
  CommentSeeder .......................................... 41ms DONE
```

To run one seeder on its own:

```bash
./sail artisan db:seed --class=PostSeeder
```

### Writing seeders

```bash
./sail artisan make:seeder UserSeeder
```

This creates `database/seeders/UserSeeder.sh`. Add it to `DatabaseSeeder::run` with `call UserSeeder`. There's no need to register it anywhere else, because `bootstrap/app.sh` sources every file in `database/seeders`.

> [!NOTE]
> Laravel resolves seeders from its service container, so they can ask for dependencies in their constructors. Laravel Bash has no classes and no container. It looks functions up by name when they're called, so `call` only needs the name, and `declare -F` checks that it exists first.

There are no factories. There is a heredoc, and a pipe character you hope nobody uses in a title.
