---
name: step-reset
description: Reset database - drop and recreate
next_step: null
---

# Database Reset

## YOUR TASK:

Completely reset the database: drop, recreate, and apply all migrations.

**WARNING:** This is DESTRUCTIVE. All data will be lost!

**Requires:** `steps/shared/step-00-init.md` completed

---

## COMPLIANCE:

| FORBIDDEN | REQUIRED |
|-----------|----------|
| `sqlcmd` for DDL/DML | `dotnet ef database drop` |
| SQL scripts in repo | `dotnet ef database update` |
| INSERT via scripts | `HasData()` for seeding |

**Exception:** `sqlcmd` allowed ONLY for `BACKUP DATABASE`.

---

## EXECUTION SEQUENCE:

### 1. Environment Check

```bash
# Validate required variables from step-00-init
for VAR_NAME in DBCONTEXT DBCONTEXT_TYPE INFRA_PROJECT STARTUP_PROJECT SELECTED_ENV; 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

echo "Reset Configuration"
echo "==================="
echo ""
echo "DbContext:   $DBCONTEXT ($DBCONTEXT_TYPE)"
echo "Environment: $SELECTED_ENV"
echo "Database:    $DATABASE_NAME"
echo "Server:      $SERVER_NAME"
echo ""
echo "WARNING: ALL DATA WILL BE LOST!"

# Block production
block_production
```

### 2. Confirmation (MANDATORY)

```yaml
AskUserQuestion:
  header: "Reset DB"
  question: "DANGER: Permanently delete '{DATABASE_NAME}'?

ALL DATA WILL BE LOST!

This action is IRREVERSIBLE."
  options:
    - label: "Yes, delete {DATABASE_NAME}"
      description: "Drop + Recreate (IRREVERSIBLE)"
    - label: "No, cancel"
      description: "Keep database"
  multiSelect: false
```

### 3. Optional Backup

```yaml
AskUserQuestion:
  header: "Backup"
  question: "Backup database before deletion?"
  options:
    - label: "Yes, backup (Recommended)"
      description: "Create .bak via sqlcmd"
    - label: "No"
      description: "Dev data only - continue"
    - label: "Manual backup"
      description: "Via SSMS/Azure Portal (stops here)"
  multiSelect: false
```

**If backup requested:**

```bash
BACKUP_DIR=".claude/gitflow/backup/database"
mkdir -p "$BACKUP_DIR"

BACKUP_FILE="$BACKUP_DIR/${DATABASE_NAME}_$(date +%Y%m%d_%H%M%S).bak"

echo "Creating backup: $BACKUP_FILE"

sqlcmd -S "$SERVER_NAME" -E -Q "BACKUP DATABASE [$DATABASE_NAME] TO DISK='$BACKUP_FILE'"

if [ $? -eq 0 ]; then
  echo "Backup created: $BACKUP_FILE"
else
  echo "WARNING: Backup failed. Continue anyway?"
fi
```

### 4. Drop Database

```bash
echo ""
echo "Dropping database..."

dotnet ef database drop \
  --context "$DBCONTEXT" \
  --project "$INFRA_PROJECT" \
  --startup-project "$STARTUP_PROJECT" \
  --force
EXIT_CODE=$?

if [ $EXIT_CODE -ne 0 ]; then
  echo "ERROR: Drop failed (exit code: $EXIT_CODE)"
  echo "Database may be in use. Close connections and retry."
  exit 1
fi

echo "Database dropped."
```

### 5. Recreate + Apply Migrations

```bash
echo ""
echo "Recreating database with migrations..."

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

if [ $EXIT_CODE -ne 0 ]; then
  echo ""
  echo "ERROR: Recreation failed (exit code: $EXIT_CODE)"
  exit 1
fi

echo "Database recreated."
```

### 6. Optional Seed

```yaml
AskUserQuestion:
  header: "Seed"
  question: "Populate with test data (HasData)?"
  options:
    - label: "Yes (recommended)"
      description: "Apply HasData configurations"
    - label: "No"
      description: "Empty database"
  multiSelect: false
```

**If seed requested:**

```bash
echo ""
echo "Running seed..."

# HasData is already applied via migrations
# For custom seeding:
dotnet run --project "$STARTUP_PROJECT" -- --seed 2>/dev/null || echo "No --seed argument supported"
```

### 7. Summary

```bash
MIGRATION_COUNT=$(dotnet ef migrations list \
  --context "$DBCONTEXT" \
  --project "$INFRA_PROJECT" \
  --startup-project "$STARTUP_PROJECT" 2>/dev/null | \
  grep -c "^[0-9]" || echo "0")

echo ""
echo "==========================================="
echo "RESET COMPLETE"
echo "==========================================="
echo ""
echo "Database:    $DATABASE_NAME"
echo "Environment: $SELECTED_ENV"
echo "DbContext:   $DBCONTEXT"
echo ""
echo "Operations:"
echo "  - Drop: OK"
echo "  - Create: OK"
echo "  - Migrations: $MIGRATION_COUNT applied"
if [ -n "$BACKUP_FILE" ]; then
echo "  - Backup: $BACKUP_FILE"
fi
echo ""
echo "Compliance:  EF Core CLI only"
echo ""
echo "==========================================="
echo ""
echo "Next steps:"
echo "  /efcore db-status    # Verify status"
echo "  /efcore db-seed      # Add test data"
echo ""
```

---

## OUTPUT FORMAT:

```
RESET COMPLETE
==============
Database:    {database_name}
Environment: {env}
DbContext:   {dbcontext}

Operations:
  - Drop: OK
  - Create: OK
  - Migrations: {N} applied
  - Backup: {path | none}

Compliance:  EF Core CLI only

Next: /efcore db-status, /efcore db-seed
```

---

## PROTECTIONS:

| Protection | Description |
|------------|-------------|
| Env Display | Shows database BEFORE confirmation |
| Confirmation | Explicit request with database NAME |
| Production Block | Blocks if Production detected |
| EF Core Only | No raw SQL |

---

## BOTH CONTEXTS:

If "Both" selected:

```bash
# Drop + Recreate CoreDbContext
DBCONTEXT="CoreDbContext"
# ... execute reset ...

# Then ExtensionsDbContext
DBCONTEXT="ExtensionsDbContext"
# ... execute reset ...
```

---

## OPTIONS:

| Option | Description |
|--------|-------------|
| `--env {name}` | Use appsettings.{name}.json |
| `--no-backup` | Skip backup step |
| `--context {name}` | Specify DbContext |
| `--seed` | Auto-seed after reset |

---

## COMPLETION:

Database reset complete. Fresh state with all migrations applied.
