---
name: efcore-rebase-snapshot
description: EF Core snapshot rebaser - resync ModelSnapshot with develop
color: yellow
model: sonnet
tools: Bash, Glob, Read, Edit
steps:
  - steps/shared/step-00-init.md
  - steps/rebase-snapshot/step-00-init.md
  - steps/rebase-snapshot/step-01-backup.md
  - steps/rebase-snapshot/step-02-fetch.md
  - steps/rebase-snapshot/step-03-create.md
  - steps/rebase-snapshot/step-04-validate.md
---

# EF Core Rebase-Snapshot Agent

Rebases ModelSnapshot on develop to resolve conflicts.

**Progressive Steps:** Load `steps/rebase-snapshot/` for detailed instructions.

## Workflow

1. **Backup** all migrations
2. **Reset** ModelSnapshot to develop
3. **Delete** branch migrations
4. **Regenerate** consolidated migration
5. **Validate** build OK

## Key Commands

```bash
# Detect project and DbContext
detect_efcore_project   # → $DBCONTEXT, $DBCONTEXT_TYPE, $SCHEMA
determine_base_branch   # → $BASE_BRANCH, $BRANCH_TYPE

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

# Reset snapshot to parent branch (develop or main depending on context)
git fetch origin "$BASE_BRANCH"
git checkout "origin/$BASE_BRANCH" -- Migrations/*ModelSnapshot.cs

# Retrieve parent branch migrations
BASE_MIGS=$(git ls-tree -r --name-only "origin/$BASE_BRANCH" -- Migrations/ | grep "\.cs$" | grep -v "Designer\|Snapshot")
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
done

# Delete branch migrations only
rm -f Migrations/*${BRANCH_TYPE}_*.cs
rm -f Migrations/*${BRANCH_TYPE}_*.Designer.cs

# Regenerate with MCP naming - MANDATORY
mcp__smartstack__suggest_migration({ description: "...", context: DBCONTEXT_TYPE })
# Result: core_v1.7.0_001_UserAuthConsolidated

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

# Validate
dotnet build
```

## Migration Naming (via MCP)

**MANDATORY: Use MCP for naming**

```
{context}_v{version}_{sequence}_{Description}

core_v1.7.0_001_UserAuthConsolidated
core_v1.6.2_001_LoginFixFix
core_v1.7.0_001_ReleaseInitial
```

> **FORBIDDEN:** Legacy pattern `Feature_1_7_0_UserAuth_Consolidated`

## Safety Checks

- [ ] Clean working directory
- [ ] Backup created
- [ ] Build OK after rebase
- [ ] SQL script can be generated
- [ ] SQL objects injected (if `Persistence/SqlObjects/*.sql` exists)

## Error Recovery

| Failure | Recovery Action |
|---------|----------------|
| Build fails after rebase | 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` |
| Migration creation fails | Restore backup, check `dotnet build` output |
| 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. Backup mandatory.
