---
name: step-00-init
description: Initialize migration - cross-branch check and context detection
next_step: step-01-check.md
---

# Step 0: Initialize Migration

## YOUR TASK:

Detect context, check for cross-branch conflicts, and prepare for migration creation.

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

---

## RULE:

```
1 MIGRATION PER FEATURE

- If migration exists for this branch → recreate it
- Never accumulate multiple migrations per feature
- Squash before merge if needed
```

---

## EXECUTION SEQUENCE:

### 1. Display Context

```bash
echo "Migration Context"
echo "================"
echo ""
echo "Branch:     $CURRENT_BRANCH"
echo "Type:       $BRANCH_TYPE"
echo "DbContext:  $DBCONTEXT ($DBCONTEXT_TYPE)"
echo "Schema:     $SCHEMA"
echo "Migrations: $MIGRATIONS_DIR"
```

### 2. Cross-Branch Validation (Optional)

```bash
if [ "$CROSS_BRANCH_ENABLED" = "true" ]; then
  echo ""
  echo "Checking cross-branch conflicts..."

  # Get snapshot from develop
  DEVELOP_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 "$DEVELOP_SNAPSHOT" ] && [ -n "$LOCAL_SNAPSHOT" ]; then
    DIFF_LINES=$(diff <(echo "$DEVELOP_SNAPSHOT") <(echo "$LOCAL_SNAPSHOT") 2>/dev/null | wc -l)

    if [ "$DIFF_LINES" -gt 100 ]; then
      echo ""
      echo "WARNING: Cross-branch conflict detected ($DIFF_LINES lines differ)"
      echo "Recommendation: /efcore rebase-snapshot"
      echo ""

      if [ "$BLOCK_ON_CONFLICT" = "true" ]; then
        echo "BLOCKED: Use --force to override or rebase first"
        exit 1
      fi
    else
      echo "  No significant conflicts detected"
    fi
  fi
fi
```

### 3. Skip If --force Flag

```bash
if [ "$FORCE_FLAG" = "true" ]; then
  echo ""
  echo "Force mode: skipping cross-branch check"
fi
```

---

## STATE OUTPUT:

| Variable | Description |
|----------|-------------|
| `{cross_branch_ok}` | true if no conflicts or force mode |
| `{diff_lines}` | Number of lines different from parent |

---

## SUCCESS CRITERIA:

- DbContext detected
- Cross-branch check passed (or forced)

## NEXT STEP:

→ `step-01-check.md`
