---
name: step-00-init
description: Initialize squash - verify prerequisites and get confirmation
next_step: step-01-backup.md
---

# Step 0: Initialize Squash

## YOUR TASK:

Verify prerequisites for squash operation and get user confirmation.

**Requires:** `steps/shared/step-00-init.md` completed (provides {dbcontext}, {base_branch}, etc.)

---

## EXECUTION SEQUENCE:

### 1. Verify Clean Working Directory

```bash
[ -n "$(git status --porcelain)" ] && {
  echo "ERROR: Working directory not clean"
  git status --short
  exit 1
}
```

### 2. Identify Branch-Only Migrations

```bash
# Fetch parent branch
git fetch origin "$BASE_BRANCH" --quiet

# Get migrations from parent branch
BASE_MIGRATIONS=$(git ls-tree -r --name-only "origin/$BASE_BRANCH" -- "$MIGRATIONS_DIR" 2>/dev/null | grep "\.cs$" | grep -v "Designer\|Snapshot" || echo "")

# Get local migrations
LOCAL_MIGRATIONS=$(find "$MIGRATIONS_DIR" -name "*.cs" 2>/dev/null | grep -v "Designer\|Snapshot" | xargs -I{} basename {} 2>/dev/null || echo "")

# Find migrations unique to this branch
BRANCH_ONLY_MIGRATIONS=()
for mig in $LOCAL_MIGRATIONS; do
  if ! echo "$BASE_MIGRATIONS" | grep -q "$(basename "$mig")"; then
    BRANCH_ONLY_MIGRATIONS+=("$mig")
  fi
done

MIGRATION_COUNT=${#BRANCH_ONLY_MIGRATIONS[@]}
BASE_MIGRATION_COUNT=$(echo "$BASE_MIGRATIONS" | grep -c "." || echo "0")

echo "Branch: $CURRENT_BRANCH"
echo "Parent: $BASE_BRANCH"
echo "Migrations to squash: $MIGRATION_COUNT (branch-only)"
echo "Migrations from parent: $BASE_MIGRATION_COUNT (preserved)"
```

### 3. Check If Squash Needed

```bash
if [ "$MIGRATION_COUNT" -eq 0 ]; then
  echo "No branch-specific migrations to squash."
  echo "Nothing to do."
  exit 0
fi

if [ "$MIGRATION_COUNT" -eq 1 ]; then
  echo "Only 1 migration on branch."
  # Ask if user wants to continue anyway
fi
```

### 4. Display Summary

```
SQUASH PREVIEW
==============
Branch:     {CURRENT_BRANCH}
Parent:     {BASE_BRANCH}
DbContext:  {DBCONTEXT} ({DBCONTEXT_TYPE})

Migrations to squash ({MIGRATION_COUNT}):
{for mig in BRANCH_ONLY_MIGRATIONS}
  - {mig}
{endfor}

Migrations from parent ({BASE_MIGRATION_COUNT}):
  (will be preserved)

Result: {BASE_MIGRATION_COUNT + 1} migrations total
```

### 5. User Confirmation

**If NOT auto_mode:**

```yaml
AskUserQuestion:
  header: "Squash"
  question: "Squash {MIGRATION_COUNT} migrations from {CURRENT_BRANCH}?

The ModelSnapshot and migrations from {BASE_BRANCH} will be recovered.
Your {MIGRATION_COUNT} migrations will be consolidated into ONE.

This operation is DESTRUCTIVE but a backup will be created."
  options:
    - label: "Yes, squash"
      description: "Consolidate on base of {BASE_BRANCH}"
    - label: "View details"
      description: "Show migrations to be squashed"
    - label: "Cancel"
      description: "Do nothing"
  multiSelect: false
```

**Handle response:**
- "Yes, squash" → Continue to next step
- "View details" → Show full migration list, ask again
- "Cancel" → Exit

---

## STATE OUTPUT:

| Variable | Description |
|----------|-------------|
| `{branch_only_migrations}` | Array of migrations to squash |
| `{migration_count}` | Number of migrations to squash |
| `{base_migration_count}` | Number of parent migrations |

---

## SUCCESS CRITERIA:

- Working directory clean
- At least 1 branch-specific migration
- User confirmed (or auto_mode)

## NEXT STEP:

→ `step-01-backup.md`
