---
name: step-04-validate
description: Validate squash - build and generate script
next_step: null
---

# Step 4: Validate

## YOUR TASK:

Validate the squash by building the project and generating an idempotent script.

**Previous step:** `step-03-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 ""
  echo "Restoring from backup..."
  cp "$BACKUP_DIR"/*.cs "$MIGRATIONS_DIR/"
  echo "Restored. Please fix issues and retry."
  exit 1
fi

echo ""
echo "Build: OK"
```

### 2. List Migrations

```bash
echo ""
echo "Migration list:"
echo ""

dotnet ef migrations list \
  --context "$DBCONTEXT" \
  --project "$INFRA_PROJECT" \
  --startup-project "$STARTUP_PROJECT" \
  --no-build
```

### 3. Generate Idempotent Script (Validation)

```bash
echo ""
echo "Generating idempotent script..."
echo ""

SCRIPT_FILE="$BACKUP_DIR/migration_script.sql"

dotnet ef migrations script \
  --context "$DBCONTEXT" \
  --project "$INFRA_PROJECT" \
  --startup-project "$STARTUP_PROJECT" \
  --idempotent \
  --no-build \
  -o "$SCRIPT_FILE"

if [ $? -ne 0 ]; then
  echo ""
  echo "ERROR: Script generation FAILED"
  echo ""
  echo "This indicates a migration problem."
  echo "Restoring from backup..."
  cp "$BACKUP_DIR"/*.cs "$MIGRATIONS_DIR/"
  exit 1
fi

echo "Script generated: $SCRIPT_FILE"
```

### 4. Final Summary

```bash
# Count final migrations
FINAL_MIGRATION_COUNT=$(find "$MIGRATIONS_DIR" -name "*.cs" 2>/dev/null | grep -v "Designer\|Snapshot" | wc -l)

echo ""
echo "==========================================="
echo "SQUASH COMPLETE"
echo "==========================================="
echo ""
echo "Branch:      $CURRENT_BRANCH"
echo "Parent:      $BASE_BRANCH"
echo "DbContext:   $DBCONTEXT ($DBCONTEXT_TYPE)"
echo ""
echo "Recovered from $BASE_BRANCH:"
echo "  - ModelSnapshot"
echo "  - $BASE_MIGRATION_COUNT migrations"
echo ""
echo "Consolidated:"
echo "  - Before: $MIGRATION_COUNT migrations (branch-specific)"
echo "  - After:  1 migration ($MIGRATION_NAME)"
echo ""
echo "Total:       $FINAL_MIGRATION_COUNT migrations"
echo ""
echo "Backup:      $BACKUP_DIR"
echo "Script:      $SCRIPT_FILE"
echo ""
echo "==========================================="
echo ""
echo "Next steps:"
echo "  /efcore db-reset      # Reset local database"
echo "  /efcore db-deploy     # Apply migrations"
echo "  /gitflow commit       # Commit changes"
echo ""
```

---

## OUTPUT FORMAT:

```
SQUASH COMPLETE
===============
Branch:      {current_branch}
Parent:      {base_branch}
DbContext:   {dbcontext} ({dbcontext_type})

Recovered from {base_branch}:
  - ModelSnapshot
  - {base_migration_count} migrations

Consolidated:
  - Before: {migration_count} migrations (branch-specific)
  - After:  1 migration ({migration_name})

Total:       {final_migration_count} migrations

Backup:      {backup_dir}
Script:      {backup_dir}/migration_script.sql

Next: /efcore db-reset, /efcore db-deploy, /gitflow commit
```

---

## ERROR HANDLING:

| Error | Action |
|-------|--------|
| Build fails | Restore backup, show errors |
| Script fails | Restore backup, investigate |

**Restore command:**
```bash
cp "{BACKUP_DIR}"/*.cs "{MIGRATIONS_DIR}/"
```

---

## SUCCESS CRITERIA:

- Build passes
- Migration list shows correct migrations
- Idempotent script generates successfully
- Summary displayed

## COMPLETION:

Squash workflow complete. Database not modified (use `/efcore db-deploy` to apply).
