# Agent — Apply migrations to the database

Read `_shared.md` first. There is **no** CLI colocated with this agent —
`dotnet ef database update` is straightforward enough that the agent just
runs it directly with the correct parameters.

## Goal

Apply every pending migration to the local development database. Always
preview the SQL first with `--idempotent` so the user sees exactly what
will run.

## Steps

1. Run `status` to see pending migrations:

   ```bash
   npx --prefer-offline tsx skills/efcore/cli/status/index.ts --spec '{"cwd":"<WORKTREE>"}'
   ```

   If no assembly has `pending > 0`, stop — database is up to date.

2. For each assembly with pending migrations, dump the idempotent script:

   ```bash
   dotnet ef migrations script --idempotent \
     --project <csproj> \
     --context <contextName> \
     --startup-project <startup> \
     -o pending-migrations.sql
   ```

   Show the user the path; they can inspect the SQL before applying.

3. Apply:

   ```bash
   dotnet ef database update \
     --project <csproj> \
     --context <contextName> \
     --startup-project <startup>
   ```

4. Core before Extensions (dual-context apps): always update `CoreDbContext`
   first, then `ExtensionsDbContext`. Extensions may have foreign keys into
   Core and the reverse order leaves Extensions broken.

5. For the Studio's multi-provider context (`StudioDbContext`), the user
   picks which provider is active (see `~/.smartstack/config.json`).
   Update only the matching assembly — the other two are dormant until
   the user switches providers.

## What NOT to do

- Never run `database update` against a production / staging DB from this
  agent. The agent is scoped to local dev only.
- Never skip the idempotent script — it is the user's last chance to
  catch a destructive migration before it runs.
