# Agent — Squash branch migrations

Read `_shared.md` first.

## Goal

Collapse all branch-only migrations into a single consolidated migration
right before merging the feature to develop. The CLI handles the
mechanical parts (backup / delete / reset / regenerate) but you still own
the decision to run it.

## Preconditions

- Working tree is clean (no unstaged changes other than the migrations
  being squashed). Ask the user to stash or commit first.
- Current branch is NOT `main` / `master` — the CLI refuses otherwise.
- `base` ref is fetchable (usually `origin/develop`). If the user is
  offline or has no `origin/develop`, pass `baseBranch` explicitly.

## Steps

1. Run `status` first to see how many branch-only migrations exist:

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

   If fewer than 2, there is nothing to squash — stop.

2. Dry-run the squash:

   ```bash
   npx tsx skills/efcore/cli/squash/index.ts --spec '{
     "cwd": "<WORKTREE>",
     "contextName": "CoreDbContext",
     "description": "FeatureXYZConsolidated",
     "dryRun": true
   }'
   ```

3. Review the plan: which files will be deleted, which snapshot will be
   reset, the consolidated migration name. Confirm with the user.

4. Run the real squash (same spec, remove `dryRun`). The CLI:
   - Writes a backup under `.efcore-squash-backup/<timestamp>/<Assembly>/`.
   - Deletes branch-only migration files and their `.Designer.cs`.
   - Resets the snapshot with `git checkout <baseRef> -- <snapshot>`.
   - Runs `dotnet ef migrations add <consolidated_name>` to regenerate
     one migration that reflects every cumulative change.

5. Run `dotnet build` to verify the solution compiles. If it fails,
   restore from the backup and investigate.

6. Commit the single new migration. The commit message should reference
   the feature and note that the previous N commits' migrations have
   been consolidated.

## Recovery

If something went wrong:

```bash
cp -r .efcore-squash-backup/<timestamp>/<Assembly>/* .
git checkout HEAD -- path/to/ModelSnapshot.cs
```

then remove the partially-generated consolidated migration:

```bash
dotnet ef migrations remove --context <context> --project <csproj> --startup-project <startup>
```

## What NOT to do

- Never squash on `main` or `master`.
- Never skip the backup by editing the CLI to disable it.
- Never delete the `.efcore-squash-backup/` directory before validating
  the new migration in a real database (at least in dev).
