---
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; }

# Détecte projet ET DbContext (core vs extensions)
detect_efcore_project   # Appelle detect_dbcontext() automatiquement
determine_base_branch   # Détermine la branche parente

echo "DbContext: $DBCONTEXT ($DBCONTEXT_TYPE)"
echo "Branche parente: $BASE_BRANCH"
```

**Si DbContext non détecté automatiquement :**

```javascript
AskUserQuestion({
  questions: [{
    question: "Quel DbContext utiliser ?",
    header: "DbContext",
    options: [
      { label: "CoreDbContext", description: "Entités SmartStack" },
      { label: "ExtensionsDbContext", description: "Entités client" }
    ],
    multiSelect: false
  }]
})
```

---

## 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

**OBLIGATOIRE : Utiliser le MCP pour le nommage conforme**

```bash
# Construire la description
BRANCH_NAME_CLEAN=$(echo "$CURRENT_BRANCH" | sed 's|.*/||' | sed 's/-/ /g')
DESCRIPTION="${BRANCH_NAME_CLEAN} Consolidated"
```

**Appel MCP OBLIGATOIRE :**

```javascript
// Obtenir le nom conforme via MCP
mcp__smartstack__suggest_migration({
  description: DESCRIPTION,           // Ex: "user auth Consolidated"
  context: DBCONTEXT_TYPE             // "core" ou "extensions" (de detect_dbcontext)
})

// Résultat: core_v1.7.0_001_UserAuthConsolidated
```

**Puis créer la migration :**

```bash
# $MIGRATION_NAME = résultat du MCP
# $DBCONTEXT = CoreDbContext ou ExtensionsDbContext

dotnet ef migrations add "$MIGRATION_NAME" \
  --context "$DBCONTEXT" \
  --project "$INFRA_PROJECT" \
  --startup-project "$STARTUP_PROJECT" \
  -o Persistence/Migrations \
  --verbose
```

> **Convention :** `{context}_v{version}_{sequence}_{Description}` - Jamais de nom hardcodé

---

## 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 |
