---
description: Rebase ModelSnapshot on develop and regenerate migration
agent: efcore-rebase-snapshot
model: sonnet
---

# EF Core Rebase-Snapshot

> **Ref:** [_shared.md](_shared.md)
> **Usage:** Après `/efcore:conflicts` HIGH
> **WARNING:** Modifie fichiers migration. Backup automatique.

---

## STEP 1: Vérifier Prérequis

```bash
CURRENT_BRANCH=$(git branch --show-current)
[[ ! $CURRENT_BRANCH =~ ^(feature|release|hotfix)/ ]] && exit 1
[ -n "$(git status --porcelain)" ] && { echo "Working directory non clean"; exit 1; }
detect_efcore_project
```

---

## STEP 2: Backup

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

---

## STEP 3: Identifier Migrations de la Branche

```bash
# Migrations sur develop
DEVELOP_MIGRATIONS=$(git show develop:$MIGRATIONS_DIR | grep "\.cs$")

# Nouvelles migrations (cette branche seulement)
for migration in $LOCAL_MIGRATIONS; do
  echo "$DEVELOP_MIGRATIONS" | grep -q "$migration" || BRANCH_MIGRATIONS+=" $migration"
done
```

---

## STEP 4: Reset ModelSnapshot sur Develop

```bash
git fetch origin develop
git checkout origin/develop -- "$MIGRATIONS_DIR/*ModelSnapshot.cs"
```

---

## STEP 5: Supprimer Migrations de la Branche

```bash
for migration in $BRANCH_MIGRATIONS; do
  rm -f "$MIGRATIONS_DIR/${migration%.cs}.cs"
  rm -f "$MIGRATIONS_DIR/${migration%.cs}.Designer.cs"
done
```

---

## STEP 6: Régénérer Migration Consolidée

```bash
MIGRATION_NAME="${BRANCH_TYPE}_${VERSION}_${BRANCH_NAME}_Consolidated"
dotnet ef migrations add "$MIGRATION_NAME" --verbose
```

---

## STEP 7: Validation

```bash
dotnet build --no-restore
dotnet ef migrations script --no-build > /dev/null
```

---

## Résumé

```
REBASE-SNAPSHOT - {branch}
├── Backup: {backup_dir}
├── Avant: {old_migrations}
├── Après: {new_migration_name} (Snapshot = develop)
├── Validations: ✓ Build, ✓ Script
└── Prochains: /efcore:db-reset, /efcore:db-deploy, /gitflow:3-commit
```

**Si échec:** Backup restauré automatiquement.

---

## Options

| Option | Description |
|--------|-------------|
| `--no-backup` | Sans backup (dangereux) |
| `--name <name>` | Forcer nom migration |
| `--dry-run` | Afficher sans exécuter |

---

## Quand utiliser

| Situation | Action |
|-----------|--------|
| `/efcore:conflicts` HIGH | Utiliser rebase-snapshot |
| Conflit merge sur ModelSnapshot | Utiliser rebase-snapshot |
| Plusieurs migrations à consolider | Utiliser rebase-snapshot |
