---
name: step-03-create
description: Create consolidated migration using MCP for naming
next_step: step-04-validate.md
---

# Step 3: Create Consolidated Migration

## YOUR TASK:

Create a single consolidated migration that captures all changes from the branch.

**MANDATORY:** Use MCP `suggest_migration` for compliant naming. NEVER hardcode names.

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

---

## EXECUTION SEQUENCE:

### 1. Build Description from Branch Context

```bash
case "$BRANCH_TYPE" in
  feature)
    # Extract feature name: feature/user-auth → UserAuth
    FEATURE_NAME=$(echo "$CURRENT_BRANCH" | sed 's|feature/||' | sed 's/-/ /g' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2))}1' | tr -d ' ')
    DESCRIPTION="${FEATURE_NAME}Consolidated"
    ;;
  release)
    VERSION=$(echo "$CURRENT_BRANCH" | sed 's|release/v\?||')
    DESCRIPTION="Release${VERSION}Initial"
    ;;
  hotfix)
    VERSION=$(echo "$CURRENT_BRANCH" | sed 's|hotfix/v\?||')
    DESCRIPTION="Hotfix${VERSION}Fix"
    ;;
  develop)
    DESCRIPTION="DevelopConsolidated"
    ;;
  *)
    DESCRIPTION="Consolidated"
    ;;
esac

echo "Description: $DESCRIPTION"
echo "DbContext Type: $DBCONTEXT_TYPE"
```

### 2. Get Migration Name from MCP (MANDATORY)

```javascript
// DBCONTEXT_TYPE = "core" or "extensions" (from detect_dbcontext)
// DESCRIPTION = built above (e.g., "MultitenantConsolidated")

const result = await mcp__smartstack__suggest_migration({
  description: DESCRIPTION,
  context: DBCONTEXT_TYPE,   // NEVER hardcode!
  squash: true               // Squash format: {context}_v{version} (no sequence/description)
});

// Result example: core_v1.9.0 (squash — just version, no _001_Description)
MIGRATION_NAME = result.migrationName;
```

**FORBIDDEN:**
- Calculating name manually
- Hardcoding context as "core"
- Using underscores instead of dots in version
- Adding sequence/description to squash migrations

### 3. Create Migration with EF Core

```bash
# MIGRATION_NAME = result from MCP (e.g., core_v1.9.0)
# DBCONTEXT = CoreDbContext or ExtensionsDbContext (from detect_dbcontext)

echo ""
echo "Creating migration: $MIGRATION_NAME"
echo "Context: $DBCONTEXT"
echo ""

dotnet ef migrations add "$MIGRATION_NAME" \
  --context "$DBCONTEXT" \
  --project "$INFRA_PROJECT" \
  --startup-project "$STARTUP_PROJECT" \
  -o Persistence/Migrations \
  --verbose
```

### 4. Verify Migration Created

```bash
# Check migration files exist
MIGRATION_FILE="$MIGRATIONS_DIR/${MIGRATION_NAME}.cs"
DESIGNER_FILE="$MIGRATIONS_DIR/${MIGRATION_NAME}.Designer.cs"

if [ ! -f "$MIGRATION_FILE" ]; then
  echo "ERROR: Migration file not created"
  echo "Expected: $MIGRATION_FILE"

  # Restore from backup
  cp "$BACKUP_DIR"/*.cs "$MIGRATIONS_DIR/"
  echo "Restored from backup"
  exit 1
fi

if [ ! -f "$DESIGNER_FILE" ]; then
  echo "ERROR: Designer file not created"
  exit 1
fi

echo ""
echo "Migration created:"
echo "  $MIGRATION_FILE"
echo "  $DESIGNER_FILE"
```

### 5. Check Migration Content

```bash
# Verify migration has content (not empty)
UP_METHODS=$(grep -c "protected override void Up" "$MIGRATION_FILE" || echo "0")
DOWN_METHODS=$(grep -c "protected override void Down" "$MIGRATION_FILE" || echo "0")

if [ "$UP_METHODS" -eq 0 ]; then
  echo "WARNING: Migration may be empty (no Up method found)"
fi

echo ""
echo "Migration content:"
echo "  Up methods: $UP_METHODS"
echo "  Down methods: $DOWN_METHODS"
```

### 6. Inject SQL Objects (Functions, Views, SP)

Load: `references/sql-objects-injection.md`

---

## EXPECTED RESULT:

```
After step-03-create:
├── {base_migration_1}.cs              ← From parent (preserved)
├── {base_migration_1}.Designer.cs
├── {NEW_MIGRATION_NAME}.cs            ← NEW: Consolidated migration
├── {NEW_MIGRATION_NAME}.Designer.cs   ← NEW: Designer file
└── {DbContext}ModelSnapshot.cs        ← Updated with new migration
```

---

## STATE OUTPUT:

| Variable | Description |
|----------|-------------|
| `{migration_name}` | Name from MCP (e.g., core_v1.9.0) |
| `{migration_file}` | Full path to .cs file |
| `{designer_file}` | Full path to .Designer.cs file |

---

## ERROR HANDLING:

| Error | Resolution |
|-------|------------|
| MCP fails | Check MCP connectivity, retry |
| Migration creation fails | Restore from backup, check errors |
| Empty migration | Warning only, may be valid |

---

## SUCCESS CRITERIA:

- MCP called for migration name
- Migration file created
- Designer file created
- Snapshot updated

## NEXT STEP:

→ `step-04-validate.md`
