---
name: step-02-fetch
description: Fetch ModelSnapshot AND migrations from parent branch (CRITICAL)
next_step: step-03-create.md
---

# Step 2: Fetch Parent State (CRITICAL)

## YOUR TASK:

Recover BOTH the ModelSnapshot AND all migrations from the parent branch.

**THIS IS THE MOST CRITICAL STEP.** Recovering only the snapshot loses parent migrations!

**Previous step:** `step-01-backup.md`

---

## PRINCIPLE:

```
GOLDEN RULE: Fetch 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

NEVER fetch only the snapshot without migrations!
```

---

## EXECUTION SEQUENCE:

### 1. Fetch Parent Branch

```bash
echo "Fetching origin/$BASE_BRANCH..."
git fetch origin "$BASE_BRANCH" --quiet
```

### 2. Recover ModelSnapshot

```bash
echo ""
echo "Recovering ModelSnapshot from origin/$BASE_BRANCH..."

SNAPSHOT_FILE=$(git ls-tree -r --name-only "origin/$BASE_BRANCH" -- "$MIGRATIONS_DIR" | grep "ModelSnapshot.cs$" | head -1)

if [ -n "$SNAPSHOT_FILE" ]; then
  git checkout "origin/$BASE_BRANCH" -- "$SNAPSHOT_FILE"
  echo "  Snapshot: $(basename "$SNAPSHOT_FILE")"
else
  echo "  WARNING: No snapshot found on origin/$BASE_BRANCH"
fi
```

### 3. Recover ALL Parent Migrations (CRUCIAL!)

```bash
echo ""
echo "Recovering migrations from origin/$BASE_BRANCH..."

RECOVERED_COUNT=0

for base_mig in $BASE_MIGRATIONS; do
  base_name=$(basename "${base_mig%.cs}")

  # Recover migration file
  git checkout "origin/$BASE_BRANCH" -- "$MIGRATIONS_DIR/${base_name}.cs" 2>/dev/null && {
    echo "  ${base_name}.cs"
    RECOVERED_COUNT=$((RECOVERED_COUNT + 1))
  }

  # Recover Designer file
  git checkout "origin/$BASE_BRANCH" -- "$MIGRATIONS_DIR/${base_name}.Designer.cs" 2>/dev/null && {
    echo "  ${base_name}.Designer.cs"
  }
done

echo ""
echo "Recovered: $RECOVERED_COUNT migrations from origin/$BASE_BRANCH"
```

### 4. Delete Branch-Only Migrations

```bash
echo ""
echo "Removing branch-specific migrations..."

for mig in "${BRANCH_ONLY_MIGRATIONS[@]}"; do
  mig_name="${mig%.cs}"

  rm -f "$MIGRATIONS_DIR/${mig_name}.cs"
  rm -f "$MIGRATIONS_DIR/${mig_name}.Designer.cs"

  echo "  Removed: $mig_name"
done

echo ""
echo "Removed: ${#BRANCH_ONLY_MIGRATIONS[@]} branch-specific migrations"
```

### 5. Verify State

```bash
echo ""
echo "Current state after fetch:"
echo ""

ls -la "$MIGRATIONS_DIR"/*.cs 2>/dev/null | while read line; do
  echo "  $line"
done

CURRENT_MIGS=$(find "$MIGRATIONS_DIR" -name "*.cs" 2>/dev/null | grep -v "Designer\|Snapshot" | wc -l)
echo ""
echo "Migrations remaining: $CURRENT_MIGS (all from $BASE_BRANCH)"
```

---

## EXPECTED RESULT:

```
After step-02-fetch:
├── {base_migration_1}.cs              ← From parent (preserved)
├── {base_migration_1}.Designer.cs
├── {base_migration_N}.cs              ← From parent (preserved)
├── {base_migration_N}.Designer.cs
└── {DbContext}ModelSnapshot.cs        ← From parent (recovered)

Branch-specific migrations: DELETED (will be recreated consolidated)
```

---

## STATE OUTPUT:

| Variable | Description |
|----------|-------------|
| `{recovered_count}` | Number of parent migrations recovered |
| `{snapshot_recovered}` | true if snapshot was recovered |

---

## FAILURE HANDLING:

If this step fails:

```bash
# Restore from backup
cp "{BACKUP_DIR}"/*.cs "{MIGRATIONS_DIR}/"
echo "Restored from backup"
```

---

## SUCCESS CRITERIA:

- ModelSnapshot recovered from parent
- ALL parent migrations recovered
- Branch-specific migrations removed
- No migration loss from parent

## NEXT STEP:

→ `step-03-create.md`
