---
name: step-03-validate
description: Validate migration and optionally deploy
next_step: null
---

# Step 3: Validate Migration

## YOUR TASK:

Validate the created migration and optionally apply it to the local database.

**Previous step:** `step-02-create.md`

---

## EXECUTION SEQUENCE:

### 1. Build Project

```bash
echo "Building project..."
echo ""

dotnet build "$INFRA_PROJECT" --no-restore

if [ $? -ne 0 ]; then
  echo ""
  echo "ERROR: Build failed"
  echo "Please fix compilation errors and retry."
  exit 1
fi

echo "Build: OK"
```

### 2. Show Migration Content Summary

```bash
echo ""
echo "Migration content:"
echo ""

# Count operations in migration
UP_TABLES=$(grep -c "CreateTable" "$MIGRATION_FILE" || echo "0")
UP_COLUMNS=$(grep -c "AddColumn" "$MIGRATION_FILE" || echo "0")
UP_INDEXES=$(grep -c "CreateIndex" "$MIGRATION_FILE" || echo "0")
UP_FOREIGN=$(grep -c "AddForeignKey" "$MIGRATION_FILE" || echo "0")

echo "  Tables:      $UP_TABLES"
echo "  Columns:     $UP_COLUMNS"
echo "  Indexes:     $UP_INDEXES"
echo "  Foreign Keys:$UP_FOREIGN"

# Detect destructive operations
DROP_COL=$(grep -c "DropColumn" "$MIGRATION_FILE" || echo "0")
RENAME_COL=$(grep -c "RenameColumn" "$MIGRATION_FILE" || echo "0")
ALTER_COL=$(grep -c "AlterColumn" "$MIGRATION_FILE" || echo "0")
DROP_TABLE=$(grep -c "DropTable" "$MIGRATION_FILE" || echo "0")
DESTRUCTIVE=$((DROP_COL + RENAME_COL + ALTER_COL + DROP_TABLE))

if [ "$DESTRUCTIVE" -gt 0 ]; then
  echo ""
  echo "WARNING: Destructive operations detected:"
  [ "$DROP_COL" -gt 0 ] && echo "  DropColumn:   $DROP_COL"
  [ "$RENAME_COL" -gt 0 ] && echo "  RenameColumn: $RENAME_COL"
  [ "$ALTER_COL" -gt 0 ] && echo "  AlterColumn:  $ALTER_COL"
  [ "$DROP_TABLE" -gt 0 ] && echo "  DropTable:    $DROP_TABLE"
  echo ""
  echo "For production deployments, consider zero-downtime migration patterns."
  echo "See: references/zero-downtime-patterns.md"
fi
```

### 3. Ask User to Validate

**If NOT auto mode:**

```yaml
AskUserQuestion:
  header: "Validation"
  question: "Migration created. What do you want to do?"
  options:
    - label: "Apply to local DB"
      description: "Run /efcore db-deploy now"
    - label: "Keep without applying"
      description: "Apply later manually"
    - label: "Delete and retry"
      description: "Remove migration, start over"
  multiSelect: false
```

### 4. Handle Response

```javascript
if (answer === "Apply to local DB") {
  // Apply migration
  ACTION = "deploy";
} else if (answer === "Keep without applying") {
  ACTION = "keep";
} else {
  // Delete and start over
  ACTION = "delete";
}
```

### 5. Execute Action

**If deploy:**

```bash
echo ""
echo "Applying migration..."

dotnet ef database update \
  --context "$DBCONTEXT" \
  --project "$INFRA_PROJECT" \
  --startup-project "$STARTUP_PROJECT" \
  --verbose

if [ $? -eq 0 ]; then
  echo ""
  echo "Migration applied successfully."
else
  echo ""
  echo "ERROR: Migration failed to apply."
  echo "Database may need reset: /efcore db-reset"
fi
```

**If delete:**

```bash
echo ""
echo "Removing migration..."

dotnet ef migrations remove \
  --context "$DBCONTEXT" \
  --project "$INFRA_PROJECT" \
  --startup-project "$STARTUP_PROJECT" \
  --force

echo "Migration removed. Run /efcore migration to start over."
exit 0
```

### 6. Final Summary

```bash
echo ""
echo "==========================================="
echo "MIGRATION COMPLETE"
echo "==========================================="
echo ""
echo "Branch:      $CURRENT_BRANCH"
echo "DbContext:   $DBCONTEXT ($DBCONTEXT_TYPE)"
echo ""
echo "Migration:   $MIGRATION_NAME"
echo "Files:"
echo "  - ${MIGRATION_NAME}.cs"
echo "  - ${MIGRATION_NAME}.Designer.cs"
echo "  - ${DBCONTEXT}ModelSnapshot.cs (updated)"
echo ""
echo "Content:"
echo "  Tables:      $UP_TABLES"
echo "  Columns:     $UP_COLUMNS"
echo "  Indexes:     $UP_INDEXES"
echo "  Foreign Keys:$UP_FOREIGN"
echo ""
if [ "$ACTION" = "deploy" ]; then
  echo "Status:      Applied to local database"
else
  echo "Status:      Created (not applied)"
  echo "             Run /efcore db-deploy to apply"
fi
echo ""
echo "==========================================="
echo ""
echo "Next steps:"
echo "  /gitflow commit       # Commit changes"
echo "  /efcore db-status     # Check database status"
echo ""
```

---

## OUTPUT FORMAT:

```
MIGRATION COMPLETE
==================
Branch:      {current_branch}
DbContext:   {dbcontext} ({dbcontext_type})

Migration:   {migration_name}
Files:
  - {migration_name}.cs
  - {migration_name}.Designer.cs
  - {dbcontext}ModelSnapshot.cs (updated)

Content:
  Tables:      {count}
  Columns:     {count}
  Indexes:     {count}
  Foreign Keys:{count}

Status:      {Applied | Created (not applied)}

Next: /gitflow commit, /efcore db-status
```

---

## SUCCESS CRITERIA:

- Build passes
- Migration content reviewed
- User action executed

## COMPLETION:

Migration workflow complete.
