---
description: Scan all branches for migrations with risk analysis
agent: efcore-scan
model: sonnet
---

# EF Core Scan - Cross-Branch Migration Scanner

Scans all active branches to detect migrations and analyze conflict risks using **SmartStack MCP**.

**USAGE:** Before creating a migration or before a merge to check the status of other branches.

**SECURITY:** Read-only - no modifications.

**INTEGRATION:** Uses `mcp__smartstack__check_migrations` for each branch comparison.

---

## STEP 1: Detect Active Worktrees

```bash
# List all git worktrees
WORKTREES=$(git worktree list --porcelain | grep "^worktree" | cut -d' ' -f2)

echo "DETECTED WORKTREES"
echo "=================="
git worktree list
echo ""
```

---

## STEP 2: Invoke MCP for Each Branch

> **MCP INTEGRATION:** For each active branch, call the MCP to get structured analysis.

For each worktree/branch, call:

```json
{
  "tool": "mcp__smartstack__check_migrations",
  "parameters": {
    "branch": "<branch_name>",
    "compareBranch": "develop"
  }
}
```

**Claude instruction:** Loop through detected branches and invoke MCP:

```
FOR each branch in [feature/*, hotfix/*, release/*]:
  CALL mcp__smartstack__check_migrations WITH:
    - branch: {branch}
    - compareBranch: "develop"
  STORE result in BRANCH_RESULTS[branch]
```

---

## STEP 3: Aggregate Results

Parse MCP responses to build summary:

```typescript
interface ScanSummary {
  branches: BranchScan[];
  totalMigrations: number;
  conflictCount: number;
  recommendedMergeOrder: string[];
}

interface BranchScan {
  name: string;
  migrationCount: number;
  riskLevel: 'NONE' | 'LOW' | 'MEDIUM' | 'HIGH';
  conflicts: MigrationConflict[];
}
```

**Risk Level Calculation:**

| Condition | Risk Level |
|-----------|------------|
| Snapshot identical to develop | NONE |
| Different tables modified | LOW |
| FK to same tables | MEDIUM |
| Same tables/columns modified | HIGH |

---

## STEP 4: Calculate Merge Order

Based on conflict analysis, recommend merge order:

```
1. Branches with RISK=NONE (can merge immediately)
2. Branches with RISK=LOW (minor modifications, safe to merge)
3. Branches with RISK=MEDIUM (order matters, merge carefully)
4. Branches with RISK=HIGH (rebase required before merge)
```

---

## STEP 5: Resume

**Afficher le rapport de scan:**

```
===============================================================================
                         SCAN CROSS-BRANCH EF CORE
===============================================================================

WORKTREES DÉTECTÉS ({count})
-------------------------------------------------------------------------------
  main/                    [main]
  develop/                 [develop]         ← RÉFÉRENCE
  features/user-auth/      [feature/user-auth]
  features/add-products/   [feature/add-products]
  hotfixes/login-fix/      [hotfix/login-fix]

MIGRATIONS PAR BRANCHE
-------------------------------------------------------------------------------
  Branche                 | Migrations | Snapshot   | vs Develop
-------------------------------------------------------------------------------
  develop                 | 12         | a1b2c3d4   | RÉFÉRENCE
  feature/user-auth       | 13         | e5f6g7h8   | +1 migration
  feature/add-products    | 13         | i9j0k1l2   | +1 migration
  hotfix/login-fix        | 12         | a1b2c3d4   | identique
-------------------------------------------------------------------------------

ANALYSE DES RISQUES
-------------------------------------------------------------------------------
  Branche                 | Risque   | Raison
-------------------------------------------------------------------------------
  hotfix/login-fix        | AUCUN    | Snapshot = develop
  feature/user-auth       | FAIBLE   | Tables différentes
  feature/add-products    | MOYEN    | FK vers même table
-------------------------------------------------------------------------------

===============================================================================
                         ORDRE DE MERGE RECOMMANDÉ
===============================================================================

  #  | Branche               | Risque  | Action
-------------------------------------------------------------------------------
  1. | hotfix/login-fix      | AUCUN   | ✓ Merge direct OK
  2. | feature/user-auth     | FAIBLE  | ✓ Merge OK
  3. | feature/add-products  | MOYEN   | ⚠ Après user-auth, puis rebase
-------------------------------------------------------------------------------

===============================================================================
                         PROCHAINES ÉTAPES
===============================================================================

1. Pour merger une branche sans risque:
   /gitflow:9-merge {branch_name}

2. Pour rebaser avant un merge à risque:
   /efcore:rebase-snapshot

3. Pour analyser une branche spécifique:
   /efcore:conflicts --branch {branch_name}

===============================================================================
```

**Si des conflits critiques sont détectés:**

```
===============================================================================
                    SCAN CROSS-BRANCH - CONFLITS DÉTECTÉS
===============================================================================

WORKTREES DÉTECTÉS ({count})
-------------------------------------------------------------------------------
  {liste des worktrees}

CONFLITS CRITIQUES
-------------------------------------------------------------------------------
  ⛔ {count} branche(s) avec des conflits HIGH ou CRITICAL

DÉTAILS
-------------------------------------------------------------------------------
{for each branch with HIGH risk}
  [{branch_name}]
    Risque: ÉLEVÉ
    Raison: {conflict_reason}
    Action: Rebase OBLIGATOIRE avant merge
{end for}

===============================================================================
                         ACTIONS REQUISES
===============================================================================

  ⛔ Des conflits critiques empêchent le merge de certaines branches.

1. Pour chaque branche à risque ÉLEVÉ:
   git checkout {branch}
   /efcore:rebase-snapshot

2. Recréer les migrations après rebase:
   /efcore:migration

3. Re-scanner pour vérifier:
   /efcore:scan

===============================================================================
```

---

## Options

| Option | Description |
|--------|-------------|
| `--json` | Output raw JSON for CI/CD |
| `--branch <name>` | Scan specific branch only |
| `--verbose` | Show detailed MCP responses |
| `--no-recommend` | Skip merge order recommendations |

---

## CI/CD Usage

```yaml
# GitHub Actions
- name: Scan EF Core migrations
  run: |
    # Claude Code will use MCP internally
    OUTPUT=$(claude-code "/efcore:scan --json")
    HIGH_RISK=$(echo $OUTPUT | jq '[.branches[] | select(.riskLevel == "HIGH")] | length')
    if [ "$HIGH_RISK" -gt 0 ]; then
      echo "::warning::$HIGH_RISK branches have HIGH risk migration conflicts"
    fi
```

---

## MCP Tool Reference

**Tool:** `mcp__smartstack__check_migrations`

**Description:** Analyze EF Core migrations for conflicts, ordering issues, and ModelSnapshot discrepancies between branches

**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `projectPath` | string | EF Core project path (default: auto-detect) |
| `branch` | string | Git branch to check (default: current) |
| `compareBranch` | string | Branch to compare against |

**Response:** `MigrationCheckResult` with `hasConflicts`, `migrations[]`, `conflicts[]`, `suggestions[]`

---

## Why MCP Integration?

1. **Consistency:** Identical analysis logic for single branch (`/efcore:conflicts`) and multi-branch (`/efcore:scan`)
2. **Accuracy:** Structured parsing instead of regex on ModelSnapshot files
3. **Extensibility:** New conflict types added in MCP benefit all commands
4. **Caching:** MCP can cache results for faster subsequent scans
