---
name: step-02-fetch
description: Reset snapshot to parent and delete branch migrations
next_step: step-03-create.md
---

# Step 2: Fetch Parent Snapshot

## YOUR TASK:

Reset the ModelSnapshot to the parent branch state and remove branch-specific migrations.

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

---

## EXECUTION SEQUENCE:

### 1. Fetch Latest Parent

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

### 2. Reset ModelSnapshot

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

SNAPSHOT_FILE="$MIGRATIONS_DIR/${DBCONTEXT}ModelSnapshot.cs"

git checkout "origin/$BASE_BRANCH" -- "$SNAPSHOT_FILE" 2>/dev/null

if [ $? -eq 0 ]; then
  echo "  Snapshot reset to origin/$BASE_BRANCH"
else
  echo "  WARNING: Could not fetch snapshot from origin/$BASE_BRANCH"
  echo "  Using local snapshot (may cause issues)"
fi
```

### 3. Delete Branch Migrations

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

for mig in "${BRANCH_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_MIGRATIONS[@]} migration(s)"
```

### 4. Verify State

```bash
echo ""
echo "Current migrations after reset:"

REMAINING=$(find "$MIGRATIONS_DIR" -name "*.cs" 2>/dev/null | grep -v "Designer\|Snapshot" | wc -l)

echo "  Remaining: $REMAINING (from $BASE_BRANCH)"
```

---

## EXPECTED STATE:

```
After step-02-fetch:
├── {parent_migration_1}.cs        ← From parent (unchanged)
├── {parent_migration_1}.Designer.cs
├── ...
└── {DbContext}ModelSnapshot.cs    ← Reset to parent state

Branch migrations: DELETED
```

---

## STATE OUTPUT:

| Variable | Description |
|----------|-------------|
| `{snapshot_reset}` | true if reset successful |
| `{remaining_migrations}` | Count after deletion |

---

## ERROR HANDLING:

If snapshot fetch fails:
- Continue with local snapshot
- Migration may contain more changes than expected
- Warn user

---

## SUCCESS CRITERIA:

- Snapshot reset to parent
- Branch migrations removed

## NEXT STEP:

→ `step-03-create.md`
