# Production Deployment - Quick Start Guide

**Last Updated**: October 9, 2025

---

## 🚀 Quick Deployment (5 Minutes)

### On Development Server

```bash
cd /root/maternal-app

# 1. Run pre-deployment checks
./pre-deploy-check.sh

# 2. Commit and push (if checks pass)
git add .
git commit -m "feat: describe your changes"
git push origin main
```

### On Production Server

```bash
# SSH to production
ssh user@production-server

# Run automated deployment
cd /var/www/maternal-app
./deploy.sh

# Monitor logs
pm2 logs --lines 100
```

---

## 📋 Manual Deployment Steps

### Development Server (Pre-Deploy)

1. **Test Builds**
   ```bash
   cd /root/maternal-app/maternal-web && npm run build
   cd /root/maternal-app/maternal-app/maternal-app-backend && npm run build
   ```

2. **Commit Changes**
   ```bash
   git add .
   git commit -m "your message"
   git push origin main
   ```

### Production Server (Deploy)

1. **Backup**
   ```bash
   pg_dump -U postgres -d parentflowprod -F c -f /backup/db_$(date +%Y%m%d).dump
   ```

2. **Pull Code**
   ```bash
   cd /var/www/maternal-app
   git pull origin main
   ```

3. **Install & Build**
   ```bash
   # Frontend
   cd maternal-web
   npm ci --production
   npm run build

   # Backend
   cd ../maternal-app/maternal-app-backend
   npm ci --production
   npm run build
   ```

4. **Migrate Database**
   ```bash
   cd /var/www/maternal-app/maternal-app/maternal-app-backend
   npm run migration:run
   ```

5. **Restart Services**
   ```bash
   pm2 restart all
   pm2 status
   ```

6. **Verify**
   ```bash
   curl http://localhost:3020/api/v1/health
   curl http://localhost:3030
   ```

---

## ⚡ Emergency Rollback

```bash
cd /var/www/maternal-app

# 1. Rollback code
git log -5 --oneline
git reset --hard <previous-commit>

# 2. Restore database
pg_restore -U postgres -d parentflowprod -c /backup/db_YYYYMMDD.dump

# 3. Rebuild & restart
cd maternal-web && npm run build
cd ../maternal-app/maternal-app-backend && npm run build
pm2 restart all
```

---

## 🔍 Health Checks

```bash
# Backend
curl http://localhost:3020/api/v1/health

# Frontend
curl http://localhost:3030

# PM2 Status
pm2 status
pm2 logs --lines 50

# Database
psql -U postgres -d parentflowprod -c "SELECT COUNT(*) FROM users;"
```

---

## 📁 Important Paths

**Development Server**:
- App: `/root/maternal-app/`
- Database: `10.0.0.207:5432/parentflowdev`
- Scripts: `/root/maternal-app/*.sh`

**Production Server**:
- App: `/var/www/maternal-app/`
- Database: `localhost:5432/parentflowprod`
- Backups: `/backup/`
- Logs: `/var/log/maternal-app-deploy-*.log`

---

## 🛠️ Common Issues

**Build Fails**:
```bash
rm -rf .next node_modules/.cache dist
npm install
npm run build
```

**Migration Fails**:
```bash
# Check migration history
npm run migration:show

# Rollback last migration
npm run migration:revert
```

**Service Won't Start**:
```bash
pm2 stop all
pm2 delete all
pm2 start ecosystem.config.js
```

**Database Connection Issues**:
```bash
# Check PostgreSQL is running
systemctl status postgresql

# Check connections
psql -U postgres -c "SELECT count(*) FROM pg_stat_activity;"
```

---

## 📞 Support

- **Full Documentation**: See `PRODUCTION_DEPLOYMENT_CHECKLIST.md`
- **Pre-Deploy Script**: `./pre-deploy-check.sh`
- **Production Deploy**: Copy deploy script from checklist to production server

---

## ✅ Pre-Deployment Checklist (Quick)

- [ ] All builds pass locally
- [ ] Tests pass
- [ ] Database migrations created
- [ ] `.env` files reviewed
- [ ] Backup created
- [ ] Team notified (if major release)

## ✅ Post-Deployment Checklist (Quick)

- [ ] Health checks pass
- [ ] Login works
- [ ] Critical features tested
- [ ] No errors in logs
- [ ] PM2 processes healthy

---

**For detailed instructions, see `PRODUCTION_DEPLOYMENT_CHECKLIST.md`**
