---
name: step-00-init
description: Initialize rebase-snapshot - verify prerequisites
next_step: step-01-backup.md
---

# Step 0: Initialize Rebase-Snapshot

## YOUR TASK:

Verify prerequisites for rebase-snapshot operation.

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

---

## WHEN TO USE:

| Situation | Action |
|-----------|--------|
| `/efcore conflicts` shows HIGH | Use rebase-snapshot |
| ModelSnapshot merge conflict | Use rebase-snapshot |
| Multiple migrations to consolidate | Use rebase-snapshot |
| Snapshot diverged from parent | Use rebase-snapshot |

---

## EXECUTION SEQUENCE:

### 1. Verify Branch Type

```bash
case "$BRANCH_TYPE" in
  feature|release|hotfix)
    echo "Branch type: $BRANCH_TYPE (OK)"
    ;;
  develop|main|master)
    echo "ERROR: Cannot rebase-snapshot on $BRANCH_TYPE branch"
    exit 1
    ;;
  *)
    echo "WARNING: Unknown branch type: $BRANCH_TYPE"
    ;;
esac
```

### 2. Verify Clean Working Directory

```bash
if [ -n "$(git status --porcelain)" ]; then
  echo "ERROR: Working directory not clean"
  git status --short
  exit 1
fi
```

### 3. Display Context

```bash
echo ""
echo "Rebase-Snapshot Context"
echo "======================="
echo ""
echo "Branch:     $CURRENT_BRANCH"
echo "Parent:     $BASE_BRANCH"
echo "DbContext:  $DBCONTEXT ($DBCONTEXT_TYPE)"
echo "Schema:     $SCHEMA"
echo "Migrations: $MIGRATIONS_DIR"
```

### 4. Identify Branch Migrations

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

# Get migrations from parent
PARENT_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 branch-only migrations
BRANCH_MIGRATIONS=()
for mig in $LOCAL_MIGRATIONS; do
  if ! echo "$PARENT_MIGRATIONS" | grep -q "$(basename "$mig")"; then
    BRANCH_MIGRATIONS+=("$mig")
  fi
done

BRANCH_MIG_COUNT=${#BRANCH_MIGRATIONS[@]}

echo ""
echo "Branch migrations: $BRANCH_MIG_COUNT"
for mig in "${BRANCH_MIGRATIONS[@]}"; do
  echo "  - $mig"
done
```

### 5. Check Snapshot Divergence

```bash
echo ""
echo "Checking snapshot divergence..."

PARENT_SNAPSHOT=$(git show "origin/$BASE_BRANCH:$MIGRATIONS_DIR/${DBCONTEXT}ModelSnapshot.cs" 2>/dev/null || echo "")
LOCAL_SNAPSHOT=$(cat "$MIGRATIONS_DIR/${DBCONTEXT}ModelSnapshot.cs" 2>/dev/null || echo "")

if [ -n "$PARENT_SNAPSHOT" ] && [ -n "$LOCAL_SNAPSHOT" ]; then
  DIFF_LINES=$(diff <(echo "$PARENT_SNAPSHOT") <(echo "$LOCAL_SNAPSHOT") 2>/dev/null | wc -l)
  echo "  Snapshot difference: $DIFF_LINES lines"

  if [ "$DIFF_LINES" -eq 0 ]; then
    echo ""
    echo "Snapshot is already synchronized with $BASE_BRANCH."
    echo "Nothing to do."

    if [ "$FORCE_FLAG" != "true" ]; then
      exit 0
    fi
  fi
else
  echo "  WARNING: Could not compare snapshots"
  DIFF_LINES="unknown"
fi
```

### 6. Confirmation

**If NOT auto mode:**

```yaml
AskUserQuestion:
  header: "Rebase"
  question: "Rebase snapshot from {BASE_BRANCH}?

This will:
1. Backup all migration files
2. Reset ModelSnapshot to {BASE_BRANCH}
3. Delete {BRANCH_MIG_COUNT} branch migrations
4. Create ONE consolidated migration

Snapshot difference: {DIFF_LINES} lines"
  options:
    - label: "Yes, rebase"
      description: "Proceed with rebase-snapshot"
    - label: "Cancel"
      description: "Do nothing"
  multiSelect: false
```

---

## STATE OUTPUT:

| Variable | Description |
|----------|-------------|
| `{branch_migrations}` | Array of migrations to remove |
| `{branch_mig_count}` | Number of branch migrations |
| `{diff_lines}` | Snapshot divergence (lines) |

---

## SUCCESS CRITERIA:

- Branch type is feature/release/hotfix
- Working directory clean
- Branch migrations identified
- User confirmed

## NEXT STEP:

→ `step-01-backup.md`
