# Custom Commands Migration - Quick Reference

## TL;DR - Ready to Execute

### Current Status
✅ **Local Testing**: Complete - 140 commands migrated successfully  
✅ **Production API**: Working - Tables created, endpoints functional  
✅ **Migration Script**: Ready - Handles conflicts, grace periods, logging  

### Execute Production Migration (Copy & Paste)

```bash
# 1. Copy files to production
scp /mnt/NV2/Development/major-domo/sba_is_fun.db sba-database:/tmp/
scp /mnt/NV2/Development/major-domo/database/migrate_custom_commands.py sba-database:/tmp/

# 2. SSH and prepare
ssh sba-database
docker cp /tmp/sba_is_fun.db sba_database:/usr/src/app/storage/
docker cp /tmp/migrate_custom_commands.py sba_database:/usr/src/app/storage/

# 3. DRY RUN FIRST (always!)
docker exec sba_database python /usr/src/app/storage/migrate_custom_commands.py \
  --source /usr/src/app/storage/sba_is_fun.db \
  --target /usr/src/app/storage/sba_master.db \
  --dry-run

# 4. If dry run looks good, run actual migration
docker exec sba_database python /usr/src/app/storage/migrate_custom_commands.py \
  --source /usr/src/app/storage/sba_is_fun.db \
  --target /usr/src/app/storage/sba_master.db

# 5. Validate results
curl -s "https://sba.manticorum.com/api/v3/custom_commands/stats"
curl -s "https://sba.manticorum.com/api/v3/custom_commands/by_name/yeli"
```

### Expected Results
- **30 creators** migrated  
- **140 commands** migrated  
- **All last_used dates** preserved or updated to prevent deletion  
- **Migration tags** added to all commands  
- **API endpoints** immediately functional  

### What Migration Does
1. Maps old schema (`creator`, `command`) to new schema (`custom_command_creators`, `custom_commands`)
2. Preserves all data: names, content, timestamps, relationships
3. Updates `last_used` dates for commands older than 60 days (prevents deletion)
4. Adds `["migrated"]` tag to all migrated commands
5. Updates creator statistics automatically
6. Handles conflicts gracefully (skips existing entries)

### Rollback If Needed
```bash
# Remove migrated data (keeps any manually created commands)
docker exec sba_database python -c "
import sqlite3
conn = sqlite3.connect('/usr/src/app/storage/sba_master.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM custom_commands WHERE tags LIKE \"%migrated%\"')
cursor.execute('DELETE FROM custom_command_creators WHERE discord_id NOT IN (SELECT DISTINCT creator_discord_id FROM custom_commands WHERE creator_discord_id IS NOT NULL)')
conn.commit()
conn.close()
print('Rollback complete')
"
```

### Key Files
- **Migration Script**: `/mnt/NV2/Development/major-domo/database/migrate_custom_commands.py`
- **Source DB**: `/mnt/NV2/Development/major-domo/sba_is_fun.db`  
- **Production API**: https://sba.manticorum.com/api/v3/custom_commands

---
**Ready to execute. No additional setup required.**