---
name: step-01-check
description: Check for existing migration and decide action
next_step: step-02-create.md
---

# Step 1: Check Existing Migration

## YOUR TASK:

Search for existing migrations for this branch and decide whether to recreate or skip.

**Previous step:** `step-00-init.md`

---

## EXECUTION SEQUENCE:

### 1. Build Search Pattern

```bash
# Extract branch name for pattern
case "$BRANCH_TYPE" in
  feature)
    BRANCH_SHORT=$(echo "$CURRENT_BRANCH" | sed 's|feature/||' | tr '-' '_')
    ;;
  release)
    BRANCH_SHORT=$(echo "$CURRENT_BRANCH" | sed 's|release/||' | tr '.' '_')
    ;;
  hotfix)
    BRANCH_SHORT=$(echo "$CURRENT_BRANCH" | sed 's|hotfix/||' | tr '.' '_')
    ;;
  *)
    BRANCH_SHORT=""
    ;;
esac

echo "Searching for existing migrations..."
echo "  Pattern: *${BRANCH_SHORT}*"
```

### 2. Find Existing Migrations

```bash
# Search for migrations that match this branch
EXISTING_MIGRATIONS=$(find "$MIGRATIONS_DIR" -name "*.cs" 2>/dev/null | \
  grep -v "Designer\|Snapshot" | \
  xargs -I{} basename {} | \
  grep -i "$BRANCH_SHORT" || echo "")

EXISTING_COUNT=$(echo "$EXISTING_MIGRATIONS" | grep -c "." 2>/dev/null || echo "0")

echo ""
if [ "$EXISTING_COUNT" -gt 0 ]; then
  echo "Found $EXISTING_COUNT existing migration(s):"
  echo "$EXISTING_MIGRATIONS" | while read mig; do
    echo "  - $mig"
  done
else
  echo "No existing migration for this branch."
fi
```

### 3. Decide Action

**If migrations exist and NOT force mode:**

```yaml
AskUserQuestion:
  header: "Migration"
  question: "Found {EXISTING_COUNT} existing migration(s). What do you want to do?"
  options:
    - label: "Recreate (Recommended)"
      description: "Delete existing and create new"
    - label: "Keep and add new"
      description: "Add another migration (not recommended)"
    - label: "Cancel"
      description: "Do nothing"
  multiSelect: false
```

**Handle response:**

```javascript
if (answer === "Recreate (Recommended)" || force_mode) {
  // Delete existing migrations
  ACTION = "recreate";
} else if (answer === "Keep and add new") {
  ACTION = "add";
  // Warning: violates 1-migration-per-feature rule
} else {
  // Cancel
  exit(0);
}
```

### 4. Delete Existing (If Recreate)

```bash
if [ "$ACTION" = "recreate" ]; then
  echo ""
  echo "Removing existing migrations..."

  for mig in $EXISTING_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 "Existing migrations removed."
fi
```

---

## STATE OUTPUT:

| Variable | Description |
|----------|-------------|
| `{existing_migrations}` | List of existing migrations |
| `{existing_count}` | Number found |
| `{action}` | "recreate", "add", or "cancel" |

---

## SUCCESS CRITERIA:

- Existing migrations identified
- User decision captured
- Old migrations removed (if recreate)

## NEXT STEP:

→ `step-02-create.md`
