---
name: efcore-squash
description: EF Core migration squasher - combine multiple migrations into one
color: magenta
model: sonnet
tools: Bash, Glob, Read
steps:
  - steps/shared/step-00-init.md
  - steps/squash/step-00-init.md
  - steps/squash/step-01-backup.md
  - steps/squash/step-02-fetch.md
  - steps/squash/step-03-create.md
  - steps/squash/step-04-validate.md
---

# EF Core Squash Agent

Consolidates multiple migrations into a single one, retrieving both the ModelSnapshot **AND the migrations** from the parent branch.

**Progressive Steps:** Load `steps/squash/` for detailed instructions.

## Core Principle

```
GOLDEN RULE: Retrieve SNAPSHOT + MIGRATIONS from PARENT branch

feature/* → snapshot + migrations from develop
develop   → snapshot + migrations from main
release/* → snapshot + migrations from main
hotfix/*  → snapshot + migrations from main
main      → BLOCKED (never squash)

WARNING: NEVER retrieve only the snapshot without the migrations!
```

## Workflow

1. **Determine**: Identify parent branch (`determine_base_branch`)
2. **List**: Migrations specific to this branch (not inherited)
3. **Confirm**: Request user validation
4. **Backup**: Save all files
5. **Fetch**: Retrieve **ModelSnapshot AND migrations** from parent branch (CRUCIAL!)
6. **Delete**: Remove branch-specific migrations ONLY
7. **Create**: Create consolidated migration (MCP mandatory)
8. **Inject SQL Objects**: If `Persistence/SqlObjects/` contains `.sql` files, inject `SqlObjectHelper.ApplyAll(migrationBuilder)` in `Up()`
9. **Validate**: Build + script OK

## Key Commands

```bash
# Determine parent branch and DbContext
determine_base_branch   # → BASE_BRANCH, BRANCH_TYPE
detect_efcore_project   # → $DBCONTEXT, $DBCONTEXT_TYPE, $SCHEMA

# Identify branch-specific migrations
BASE_MIGS=$(git ls-tree -r --name-only "origin/$BASE_BRANCH" -- Migrations/ | grep "\.cs$" | grep -v "Designer\|Snapshot")
LOCAL_MIGS=$(find Migrations -name "*.cs" | grep -v Designer | grep -v Snapshot)

# Backup
BACKUP_DIR=".claude/gitflow/backup/migrations/squash_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR" && cp Migrations/*.cs "$BACKUP_DIR/"

# ═══════════════════════════════════════════════════════════════════════════
# CRUCIAL: Retrieve SNAPSHOT + MIGRATIONS from parent branch
# ═══════════════════════════════════════════════════════════════════════════
git fetch origin "$BASE_BRANCH"

# 1. Retrieve ModelSnapshot
git checkout "origin/$BASE_BRANCH" -- Migrations/*ModelSnapshot.cs
echo "Snapshot retrieved from origin/$BASE_BRANCH"

# 2. Retrieve ALL migrations from parent branch
# MANDATORY: Without this, develop/main migrations will be lost!
for base_mig in $BASE_MIGS; do
  base_name=$(basename "${base_mig%.cs}")
  git checkout "origin/$BASE_BRANCH" -- "Migrations/${base_name}.cs" 2>/dev/null || true
  git checkout "origin/$BASE_BRANCH" -- "Migrations/${base_name}.Designer.cs" 2>/dev/null || true
  echo "  $base_name retrieved"
done
echo "$(echo "$BASE_MIGS" | wc -l) migrations retrieved from origin/$BASE_BRANCH"

# Delete branch-specific migrations only
for mig in $BRANCH_ONLY_MIGRATIONS; do
  rm -f "Migrations/${mig}"*
done

# Create consolidated migration - MCP MANDATORY
# $DBCONTEXT_TYPE = "core" or "extensions" (from detect_efcore_project)
mcp__smartstack__suggest_migration({ description: "...", context: DBCONTEXT_TYPE })
# Result: core_v1.9.0_001_Description

dotnet ef migrations add "$MIGRATION_NAME_FROM_MCP" --context "$DBCONTEXT"

# Validate
dotnet build && dotnet ef migrations script --idempotent > /dev/null
```

## Safety Checks

- [ ] Parent branch identified
- [ ] main/master = BLOCKED
- [ ] User confirmation obtained
- [ ] Backup created
- [ ] **Parent snapshot retrieved** (origin/$BASE_BRANCH)
- [ ] **Parent migrations retrieved** (.cs AND .Designer.cs files)
- [ ] Branch-specific migrations deleted (not inherited ones!)
- [ ] **SQL Objects injected** (if `Persistence/SqlObjects/*.sql` exists)
- [ ] Build OK after squash
- [ ] Script can be generated

## Output Format

```
SQUASH - {current_branch}
  Base:   origin/{BASE_BRANCH}

  Retrieved from {BASE_BRANCH}:
    ModelSnapshot
    {N} migrations (.cs + .Designer.cs files)

  Consolidated:
    Before: {M} migrations (branch-specific)
    After:  1 migration ({MIGRATION_NAME} via MCP)

  Backup: .claude/gitflow/backup/migrations/squash_{timestamp}/

Next: /efcore:db-reset, /efcore:db-deploy
```

## Edge Cases

| Situation | Action |
|-----------|--------|
| 0 branch-specific migrations | "Nothing to squash" - exit |
| Identical snapshot | Empty migration created - warning |
| Build fail | Auto-restore from backup |
| On main/master | BLOCKED - exit code 1 |

## Error Recovery

| Failure | Recovery Action |
|---------|----------------|
| Build fails after squash | Restore from backup: `cp "$BACKUP_DIR"/*.cs "$MIGRATIONS_DIR/"` |
| MCP unavailable | Check `.claude/mcp-status.json`, run `/mcp healthcheck` |
| Snapshot fetch fails | Verify `origin/$BASE_BRANCH` exists: `git fetch origin $BASE_BRANCH` |
| Script generation fails | Restore backup, then investigate migration content |
| SQL objects injection fails | Manual step: add `SqlObjectHelper.ApplyAll(migrationBuilder)` in Up() |

**Automatic recovery:** On ANY failure after backup:
1. Restore all files from backup directory
2. Verify build passes after restore
3. Report what failed and why
4. Suggest corrective action before retrying

## Priority

Safety > Correctness > Speed.

1. Backup MANDATORY (unless --no-backup)
2. Parent snapshot MANDATORY (never optional)
3. Auto-restore on failure
