---
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:

### 0. Validate Required Variables

```bash
# Validate required variables from step-00-init
for VAR_NAME in DBCONTEXT DBCONTEXT_TYPE MIGRATIONS_DIR INFRA_PROJECT STARTUP_PROJECT CURRENT_BRANCH BRANCH_TYPE; do
  eval VAR_VALUE=\$$VAR_NAME
  if [ -z "$VAR_VALUE" ]; then
    echo "ERROR: Required variable $VAR_NAME is not set"
    echo "Run step-00-init first"
    exit 1
  fi
done
```

### 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
# Sanitize branch name: only allow alphanumeric, underscore (prevent regex/shell injection)
BRANCH_SHORT_SAFE=$(echo "$BRANCH_SHORT" | sed 's/[^a-zA-Z0-9_]//g')
if [ -z "$BRANCH_SHORT_SAFE" ]; then
  echo "ERROR: Invalid branch name (no alphanumeric characters after sanitization)"
  exit 1
fi

# Search for migrations matching this branch (-F = fixed-string, prevents regex injection)
EXISTING_MIGRATIONS=$(find "$MIGRATIONS_DIR" -name "*.cs" ! -name "*Designer.cs" ! -name "*Snapshot.cs" -type f 2>/dev/null | \
  xargs -I{} basename {} | \
  grep -iF "$BRANCH_SHORT_SAFE" || 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"

    # Verify deletion succeeded
    if [ -f "$MIGRATIONS_DIR/${mig_name}.cs" ]; then
      echo "  ERROR: Failed to delete ${mig_name}.cs"
      exit 1
    fi

    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`
