# Migration File Validation

## Detect EF Core Migrations

```bash
# Find migration files in changes
MIGRATIONS=$(echo "$STAGED $MODIFIED $UNTRACKED" | tr ' ' '\n' | grep -E "Migrations/.*\.cs$")
HAS_MIGRATIONS=$([ -n "$MIGRATIONS" ] && echo "true" || echo "false")

if [ "$HAS_MIGRATIONS" = "true" ]; then
  # Check for the 3 required files
  MIGRATION_NAME=$(echo "$MIGRATIONS" | grep -v "Designer\|Snapshot" | head -1 | sed 's/.*\///' | sed 's/\.cs$//')

  HAS_MAIN=$(echo "$MIGRATIONS" | grep -c "${MIGRATION_NAME}.cs")
  HAS_DESIGNER=$(echo "$MIGRATIONS" | grep -c "${MIGRATION_NAME}.Designer.cs")
  HAS_SNAPSHOT=$(echo "$MIGRATIONS" | grep -c "ModelSnapshot.cs")

  [ "$HAS_MAIN" -eq 0 ] || [ "$HAS_DESIGNER" -eq 0 ] || [ "$HAS_SNAPSHOT" -eq 0 ] && {
    echo "❌ INCOMPLETE MIGRATION"
    echo "Required: Migration.cs + Designer.cs + ModelSnapshot.cs"
    echo "Found: Main=$HAS_MAIN, Designer=$HAS_DESIGNER, Snapshot=$HAS_SNAPSHOT"
    STOP
  }
fi
```

## Check Destructive Operations (BLOCKING)

```bash
if [ "$HAS_MIGRATIONS" = "true" ]; then
  DESTRUCTIVE=$(grep -l "DropTable\|DropColumn\|DeleteData" $MIGRATIONS 2>/dev/null)

  [ -n "$DESTRUCTIVE" ] && {
    echo "⚠️ DESTRUCTIVE OPERATIONS DETECTED"
    grep -n "DropTable\|DropColumn\|DeleteData" $DESTRUCTIVE

    # Require explicit confirmation
    AskUserQuestion:
      header: "Confirm"
      question: "Destructive migration detected. Continue?"
      options:
        - label: "Yes, I understand the risks"
        - label: "No, let me review"

    # Log to audit trail
    echo "$(date) - DESTRUCTIVE: $DESTRUCTIVE - User: $USER" >> .claude/gitflow/logs/audit.log
  }
fi
```

## SQL Objects Note

SQL objects (TVFs, views, stored procedures) are applied **automatically at application startup** via `SqlObjectHelper.ApplyAllAsync()`. No injection into migrations is needed. Migrations no longer need to contain `SqlObjectHelper.ApplyAll(migrationBuilder)` calls.
