# Rollback Procedure

## When to Rollback

- Service is down or unstable
- Critical bugs introduced
- Performance degradation
- Data corruption risk

## Rollback Steps

### Kubernetes Rollback

1. **Identify current version**
   ```bash
   kubectl get deployment iam-service -n production -o jsonpath='{.spec.template.spec.containers[0].image}'
   ```

2. **Rollback to previous version**
   ```bash
   kubectl rollout undo deployment/iam-service -n production
   ```

3. **Verify rollback**
   ```bash
   kubectl rollout status deployment/iam-service -n production
   ```

4. **Check service health**
   ```bash
   curl https://api.goodgo.vn/health
   ```

### Database Migration Rollback

**Note**: Prisma doesn't support automatic rollback. Create a new migration to reverse changes.

1. Create reverse migration:
   ```bash
   cd services/iam-service
   pnpm prisma migrate dev --name rollback_previous_change
   ```

2. Apply reverse migration:
   ```bash
   pnpm prisma migrate deploy
   ```

### Docker Compose Rollback

1. Stop current containers:
   ```bash
   docker-compose down
   ```

2. Checkout previous version:
   ```bash
   git checkout <previous-commit>
   ```

3. Rebuild and start:
   ```bash
   docker-compose up -d --build
   ```

## Post-Rollback

1. Verify functionality
2. Monitor metrics
3. Document rollback reason
4. Plan fix for next deployment
