# Quick Deployment Guide - WordPress Integration Fixes
**Date:** November 30, 2025

## Pre-Deployment Checklist

- [ ] Backup database
- [ ] Backup WordPress site
- [ ] Stop Celery workers
- [ ] Note current content count in "Published" status

---

## Step 1: Apply Database Migration

```bash
cd /data/app/igny8/backend
source .venv/bin/activate
python manage.py migrate integration
```

**Expected output:**
```
Running migrations:
  Applying integration.0002_add_sync_event_model... OK
```

**Verify migration:**
```bash
python manage.py showmigrations integration
```

Should show:
```
integration
 [X] 0001_initial
 [X] 0002_add_sync_event_model
```

---

## Step 2: Restart Backend Services

### If using systemd:
```bash
sudo systemctl restart igny8-backend
sudo systemctl restart igny8-celery-worker
sudo systemctl status igny8-backend
sudo systemctl status igny8-celery-worker
```

### If using Docker:
```bash
cd /data/app/igny8
docker-compose restart backend
docker-compose restart celery
docker-compose logs -f celery  # Check for errors
```

### If using screen/tmux:
```bash
# Stop Celery worker (Ctrl+C in screen session)
# Start again:
celery -A igny8_core worker --loglevel=info
```

---

## Step 3: Update WordPress Plugin (if needed)

**Option A: If plugin files were updated via git:**
```bash
# On server
cd /data/app/igny8/igny8-wp-plugin
git pull origin main

# Copy to WordPress plugins directory
cp -r /data/app/igny8/igny8-wp-plugin /var/www/html/wp-content/plugins/igny8-bridge
```

**Option B: Manual file transfer:**
Upload these modified files to WordPress:
- `igny8-wp-plugin/sync/igny8-to-wp.php`
- `igny8-wp-plugin/sync/post-sync.php`

**No WordPress settings changes needed!**

---

## Step 4: Verify Everything Works

### Test 1: Check Database Table
```bash
cd /data/app/igny8/backend
source .venv/bin/activate
python manage.py dbshell
```

```sql
-- Check table exists
\dt igny8_sync_events

-- Check initial structure
SELECT COUNT(*) FROM igny8_sync_events;
```

### Test 2: Publish Test Content
1. Go to IGNY8 Review page
2. Click "Publish" on any content
3. Wait 10 seconds
4. Go to Published page
5. **Expected:** WP Status shows "Published" (green)

### Test 3: Check Debug Status Page
1. Go to Settings → Debug Status
2. Select WordPress integration
3. **Expected:** See sync event for the test publish

### Test 4: Check WordPress
1. Go to WordPress admin → Posts
2. Find the published post
3. **Expected:** Post exists with all fields (categories, tags, SEO, image)

### Test 5: Test Status Sync from WordPress
1. In WordPress, change post from "Published" to "Draft"
2. Wait 5 seconds
3. Go to IGNY8 Published page
4. **Expected:** WP Status shows "Draft" (gray)
5. **Expected:** Debug Status shows webhook event

---

## Step 5: Monitor for Issues

### Watch Celery logs:
```bash
# Docker
docker-compose logs -f celery | grep "publish_content_to_wordpress"

# Systemd
sudo journalctl -u igny8-celery-worker -f

# Manual
# Just check the screen/tmux session
```

**Look for:**
- ✅ "Successfully published content X to WordPress post Y"
- ✅ "Content model updated: external_id=..."
- ✅ "Status webhook sent for content..."

**Red flags:**
- ❌ "Failed to publish"
- ❌ "Exception during publish"
- ❌ "Status webhook failed"

### Watch WordPress error log:
```bash
tail -f /var/www/html/wp-content/debug.log
```

**Look for:**
- ✅ "IGNY8: Status webhook sent for content..."
- ✅ "IGNY8: ✅ WordPress post created"

**Red flags:**
- ❌ "IGNY8: Status webhook failed"
- ❌ "IGNY8: NOT AUTHENTICATED"

---

## Rollback Plan (if needed)

### If migration breaks:
```bash
cd /data/app/igny8/backend
source .venv/bin/activate
python manage.py migrate integration 0001_initial
```

### If Celery errors:
```bash
# Restore old task file from git
cd /data/app/igny8/backend
git checkout HEAD~1 igny8_core/tasks/wordpress_publishing.py
sudo systemctl restart igny8-celery-worker
```

### If WordPress errors:
```bash
# Restore old plugin files from git
cd /data/app/igny8/igny8-wp-plugin
git checkout HEAD~1 sync/igny8-to-wp.php sync/post-sync.php
# Copy to WordPress
cp -r /data/app/igny8/igny8-wp-plugin /var/www/html/wp-content/plugins/igny8-bridge
```

---

## Common Issues & Fixes

### Issue: "No module named 'SyncEvent'"
**Cause:** Migration not applied  
**Fix:** Run `python manage.py migrate integration`

### Issue: Celery task failing with "SyncEvent not found"
**Cause:** Celery running old code  
**Fix:** `sudo systemctl restart igny8-celery-worker`

### Issue: Webhook returns 404
**Cause:** URLs not registered  
**Fix:** `sudo systemctl restart igny8-backend`

### Issue: WordPress webhook not sending
**Cause:** API key not set or wrong  
**Fix:** Check WordPress Settings → IGNY8 Bridge → API Key

### Issue: Debug status shows no events
**Cause:** Database not created or migration failed  
**Fix:** Check migration status, verify table exists

---

## Performance Monitoring

### Check SyncEvent table size:
```sql
SELECT COUNT(*), 
       pg_size_pretty(pg_total_relation_size('igny8_sync_events')) as size
FROM igny8_sync_events;
```

### Check recent events:
```sql
SELECT event_type, COUNT(*), 
       AVG(duration_ms) as avg_duration,
       MAX(duration_ms) as max_duration
FROM igny8_sync_events
WHERE created_at > NOW() - INTERVAL '1 hour'
GROUP BY event_type;
```

### Cleanup old events (optional):
```sql
-- Delete events older than 30 days
DELETE FROM igny8_sync_events WHERE created_at < NOW() - INTERVAL '30 days';
```

---

## Success Metrics

After deployment, you should see:

- ✅ 0 errors in Celery logs for publishing
- ✅ 100% of published content has `external_id` set
- ✅ All published content shows WP Status on Published page
- ✅ Debug Status page shows real events for each publish
- ✅ WordPress posts have all fields (categories, tags, images, SEO)
- ✅ Status changes in WordPress sync back to IGNY8 within 5 seconds

---

## Contact/Support

If you encounter issues:
1. Check logs (Celery, Django, WordPress debug.log)
2. Review troubleshooting section in main documentation
3. Verify all services restarted after deployment
4. Check network connectivity (IGNY8 ↔ WordPress)
5. Verify API keys match on both sides

**Good luck with deployment!** 🚀
