chore: remove obsolete migration documentation files

This commit is contained in:
MythEclipse
2026-05-14 16:40:43 +07:00
parent 4fbbc056bb
commit 49d4bbf781
3 changed files with 0 additions and 993 deletions

View File

@@ -1,159 +0,0 @@
# Drizzle ORM Migration - Complete ✅
**Date:** 2026-05-14
**Status:** Production Ready
## Summary
Successfully migrated Discord moderation bot from raw SQL queries to **Drizzle ORM**, providing type-safe database operations, automatic migrations, and support for both SQLite and PostgreSQL.
## What Was Accomplished
### Infrastructure
- ✅ Drizzle ORM schema definitions for all 4 tables
- ✅ Database client initialization with connection pooling
- ✅ Drizzle Kit configuration for automatic migrations
- ✅ Support for both SQLite and PostgreSQL
### Code Migration
- ✅ muxer-queue.ts: 70+ lines of raw SQL → Drizzle queries
- ✅ messageStore.ts: 11 functions refactored to Drizzle ORM
- ✅ All call sites updated (messageCapture, attachmentUploader, aiAnalyzer, webserver, index)
- ✅ Old adapter pattern completely removed
### Quality Assurance
- ✅ All 11 tests passing
- ✅ No TypeScript errors
- ✅ Clean linting (41 files)
- ✅ Successful startup with SQLite
- ✅ 10 clean commits in git history
## Files Created
```
src/database/
├── schema.ts # Drizzle table definitions
└── drizzle.ts # Database client initialization
drizzle/
└── migrations/ # Auto-generated migration files
drizzle.config.ts # Drizzle Kit configuration
```
## Files Removed
```
src/database/
├── adapter.ts # Old adapter pattern (removed)
├── postgres.ts # Old PostgreSQL client (removed)
└── migrations.ts # Old migration runner (removed)
```
## Usage
### Development (SQLite - Default)
```bash
# No setup needed, works immediately
pnpm run dev
```
### Production (PostgreSQL)
```bash
# 1. Set environment variables in .env
DATABASE_TYPE=postgres
DATABASE_URL=postgresql://user:password@host:5432/database
# 2. Run migrations
pnpm run db:migrate
# 3. Start bot
pnpm run dev
```
### Database Management Commands
```bash
# Generate migrations from schema changes
pnpm run db:generate
# Apply pending migrations
pnpm run db:migrate
# Open Drizzle Studio UI for data browsing
pnpm run db:studio
```
## Key Features
1. **Type-Safe Queries** — Full TypeScript support with Drizzle's query builder
2. **Dual Database Support** — Works seamlessly with SQLite and PostgreSQL
3. **Automatic Migrations** — Drizzle Kit generates migrations from schema
4. **Connection Pooling** — Configurable pool size for PostgreSQL
5. **Clean Code** — Replaced raw SQL with expressive query builder
6. **Better Error Handling** — Type-safe operations prevent SQL injection
## Schema
### Tables
1. **muxer_jobs** — Job queue for audio processing
- id, data, status, attempts, maxAttempts, createdAt, updatedAt, error
2. **messages** — Text messages with AI analysis
- id, guild_id, channel_id, thread_id, user_id, username, avatar_url
- content, edited_content, created_at, edited_at, deleted_at, type, metadata
- ai_status, ai_moderation_flags, ai_moderation_score, ai_moderation_raw
- ai_analysis, ai_analyzed_at, ai_error
3. **attachments** — File metadata with foreign key to messages
- id, message_id, guild_id, channel_id, thread_id, user_id
- filename, size, type, discord_url, uploaded_url
- upload_status, upload_error, created_at, uploaded_at
4. **ui_state** — Persistent UI state storage
- key, value, updated_at
## Commit History
```
b9d0a06 fix: update drizzle config to read env vars directly for CLI compatibility
b600dad fix: correct import ordering and update tests for drizzle-orm migration
50d4517 refactor: remove old database adapter files
9ff0f0b feat: update application initialization for drizzle
1c4b0af refactor: migrate messageStore to drizzle-orm
dfe3444 refactor: migrate muxer-queue to drizzle-orm
7e528a4 feat: create drizzle database client
4e28cf9 feat: add drizzle configuration and initial migrations
52b36c9 feat: create drizzle schema definitions
b833b6d feat: add drizzle-orm and drizzle-kit dependencies
```
## Testing Results
- **Unit Tests:** 11/11 passing ✅
- **Type Checking:** 0 errors ✅
- **Linting:** 0 errors ✅
- **Startup:** Successful with SQLite ✅
- **Database Operations:** All CRUD operations working ✅
## PostgreSQL Connection Notes
If you encounter connection timeouts with PostgreSQL:
1. **Verify network connectivity** to your database host
2. **Check firewall/security groups** allow your IP
3. **Test connection manually** from your machine
4. **Use SQLite for development** (default) and PostgreSQL for production
5. **Check database credentials** in `.env` are correct
The migration is complete and production-ready! 🎉
## Next Steps
1. **For immediate use:** Bot works with SQLite (default)
2. **For PostgreSQL:** Verify network connectivity, then run `pnpm run db:migrate`
3. **For development:** Use `pnpm run db:studio` to browse data visually
4. **For schema changes:** Update `src/database/schema.ts`, then run `pnpm run db:generate`

View File

@@ -1,330 +0,0 @@
# Drizzle ORM Migration - Final Summary ✅
**Date:** 2026-05-14
**Status:** ✅ Complete & Production Ready
**Total Commits:** 12
---
## Executive Summary
Successfully migrated the Discord moderation bot from **raw SQL queries** to **Drizzle ORM**, delivering a modern, type-safe database layer with support for both SQLite and PostgreSQL.
### Key Achievements
- ✅ 100% of raw SQL queries replaced with Drizzle ORM
- ✅ Type-safe database operations with full TypeScript support
- ✅ Automatic migration management via Drizzle Kit
- ✅ Dual database support (SQLite & PostgreSQL)
- ✅ All tests passing (11/11)
- ✅ Zero TypeScript errors
- ✅ Zero linting errors
- ✅ Production-ready code
---
## Project Scope
### What Was Migrated
| Component | Status | Details |
|-----------|--------|---------|
| muxer-queue.ts | ✅ | 70+ lines of SQL → Drizzle queries |
| messageStore.ts | ✅ | 11 functions refactored |
| messageCapture.ts | ✅ | Updated call sites |
| attachmentUploader.ts | ✅ | Updated call sites |
| aiAnalyzer.ts | ✅ | Updated call sites |
| webserver.ts | ✅ | Updated call sites |
| index.ts | ✅ | Database initialization |
### What Was Created
```
src/database/
├── schema.ts # Drizzle table definitions (4 tables)
├── drizzle.ts # Database client initialization
└── migrate.ts # Programmatic migration runner
drizzle/
└── migrations/ # Auto-generated migration files
drizzle.config.ts # Drizzle Kit configuration
```
### What Was Removed
```
src/database/
├── adapter.ts # Old adapter pattern ✓ Removed
├── postgres.ts # Old PostgreSQL client ✓ Removed
└── migrations.ts # Old migration runner ✓ Removed
```
---
## Database Schema
### Tables Created
1. **muxer_jobs** (Job Queue)
- Columns: id, data, status, attempts, maxAttempts, createdAt, updatedAt, error
- Indexes: status, createdAt
2. **messages** (Text Messages)
- Columns: id, guild_id, channel_id, thread_id, user_id, username, avatar_url, content, edited_content, created_at, edited_at, deleted_at, type, metadata, ai_status, ai_moderation_flags, ai_moderation_score, ai_moderation_raw, ai_analysis, ai_analyzed_at, ai_error
- Indexes: channel_id, user_id, created_at, thread_id
3. **attachments** (File Metadata)
- Columns: id, message_id, guild_id, channel_id, thread_id, user_id, filename, size, type, discord_url, uploaded_url, upload_status, upload_error, created_at, uploaded_at
- Indexes: channel_id, message_id, upload_status
- Foreign Key: message_id → messages.id (ON DELETE CASCADE)
4. **ui_state** (Persistent State)
- Columns: key, value, updated_at
---
## Usage Guide
### Development (SQLite - Default)
```bash
# No setup needed, works immediately
pnpm run dev
```
**Output:**
```
✅ SQLite database initialized
✅ Database initialized (type: sqlite)
✅ Bot logged in
✅ Message capture handlers registered
✅ AI analysis worker started
✅ WebSocket server listening on port 3000
```
### Production (PostgreSQL)
```bash
# 1. Update .env
DATABASE_TYPE=postgres
DATABASE_URL=postgresql://user:password@host:5432/database
# 2. Run migrations (programmatic - more reliable)
pnpm run db:migrate:programmatic
# 3. Start bot
pnpm run dev
```
### Database Management Commands
```bash
# Generate migrations from schema changes
pnpm run db:generate
# Apply pending migrations (Drizzle Kit CLI)
pnpm run db:migrate
# Apply pending migrations (Programmatic - recommended for PostgreSQL)
pnpm run db:migrate:programmatic
# Open Drizzle Studio UI for visual data management
pnpm run db:studio
```
---
## Technical Details
### Drizzle ORM Features Used
- **Query Builder** — Type-safe SELECT, INSERT, UPDATE, DELETE operations
- **Schema Definitions** — TypeScript-first table definitions
- **Migrations** — Automatic migration generation and tracking
- **Connection Pooling** — Configurable pool for PostgreSQL
- **Foreign Keys** — Referential integrity with CASCADE delete
- **Indexes** — Performance optimization on frequently queried columns
### Database Support
| Database | Status | Notes |
|----------|--------|-------|
| SQLite | ✅ Production Ready | Default, works immediately |
| PostgreSQL | ✅ Production Ready | Requires network connectivity |
### Migration Approaches
| Method | Command | Use Case |
|--------|---------|----------|
| Drizzle Kit CLI | `pnpm run db:migrate` | Local development |
| Programmatic | `pnpm run db:migrate:programmatic` | Production, CI/CD, reliable |
---
## Verification Results
### Testing
```
✅ Unit Tests: 11/11 passing
✅ Type Checking: 0 errors
✅ Linting: 0 errors (41 files checked)
✅ Startup: Successful with SQLite
✅ Database Operations: All CRUD operations working
```
### Database Verification
```bash
$ sqlite3 .muxer-queue.db ".tables"
__drizzle_migrations attachments messages muxer_jobs ui_state
```
### Schema Verification
```bash
$ sqlite3 .muxer-queue.db ".schema messages"
CREATE TABLE messages (
id TEXT PRIMARY KEY,
guild_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
thread_id TEXT,
user_id TEXT NOT NULL,
username TEXT NOT NULL,
avatar_url TEXT,
content TEXT NOT NULL,
edited_content TEXT,
created_at INTEGER NOT NULL,
edited_at INTEGER,
deleted_at INTEGER,
type TEXT NOT NULL DEFAULT 'text',
metadata TEXT,
ai_status TEXT NOT NULL DEFAULT 'pending',
ai_moderation_flags TEXT,
ai_moderation_score REAL,
ai_moderation_raw TEXT,
ai_analysis TEXT,
ai_analyzed_at INTEGER,
ai_error TEXT
);
```
---
## Commit History
```
9889d20 feat: add programmatic migration runner for better PostgreSQL support
b580430 docs: add drizzle orm migration completion summary
b9d0a06 fix: update drizzle config to read env vars directly for CLI compatibility
b600dad fix: correct import ordering and update tests for drizzle-orm migration
50d4517 refactor: remove old database adapter files
9ff0f0b feat: update application initialization for drizzle
1c4b0af refactor: migrate messageStore to drizzle-orm
dfe3444 refactor: migrate muxer-queue to drizzle-orm
7e528a4 feat: create drizzle database client
4e28cf9 feat: add drizzle configuration and initial migrations
52b36c9 feat: create drizzle schema definitions
b833b6d feat: add drizzle-orm and drizzle-kit dependencies
```
---
## Benefits Delivered
### Code Quality
- ✅ Type-safe database operations
- ✅ Compile-time error detection
- ✅ IDE autocomplete for all queries
- ✅ Refactoring support with rename
### Maintainability
- ✅ Cleaner, more readable code
- ✅ Reduced SQL injection risk
- ✅ Centralized schema definitions
- ✅ Automatic migration tracking
### Performance
- ✅ Connection pooling for PostgreSQL
- ✅ Optimized indexes on key columns
- ✅ Same performance as raw SQL
- ✅ Better query optimization
### Developer Experience
- ✅ Visual data management (Drizzle Studio)
- ✅ Automatic migration generation
- ✅ Clear error messages
- ✅ Comprehensive documentation
---
## Troubleshooting
### PostgreSQL Connection Timeout
**Problem:** `pnpm run db:migrate` times out when connecting to PostgreSQL
**Solutions:**
1. Use programmatic migration: `pnpm run db:migrate:programmatic`
2. Verify network connectivity to database host
3. Check firewall/security group allows your IP
4. Verify DATABASE_URL in .env is correct
5. Use SQLite for development (default)
### Table Already Exists Error
**Problem:** Migration fails with "table X already exists"
**Solution:** This is expected when running migrations on an already-initialized database. The tables are already created and migrations are tracked in `__drizzle_migrations` table.
### Missing Environment Variables
**Problem:** Config validation fails for DISCORD_TOKEN
**Solution:** Ensure `.env` file exists in project root with all required variables
---
## Next Steps
### Immediate (Development)
- ✅ Bot works with SQLite (no action needed)
- ✅ All tests passing
- ✅ Ready for feature development
### Short Term (Production)
1. Configure PostgreSQL connection in `.env`
2. Run `pnpm run db:migrate:programmatic` to create schema
3. Deploy bot with PostgreSQL
### Long Term (Maintenance)
1. Update `src/database/schema.ts` for schema changes
2. Run `pnpm run db:generate` to create migrations
3. Use `pnpm run db:studio` for data management
4. Monitor migration history in `__drizzle_migrations` table
---
## Documentation
- 📄 **Implementation Plan:** `/docs/superpowers/plans/2026-05-14-drizzle-orm-migration.md`
- 📄 **Completion Summary:** `/DRIZZLE_MIGRATION_COMPLETE.md`
- 📄 **PostgreSQL Guide:** `/POSTGRES_MIGRATION.md`
- 📄 **This Document:** `/DRIZZLE_ORM_FINAL_SUMMARY.md`
---
## Conclusion
The Discord moderation bot has been successfully migrated to **Drizzle ORM**, providing:
**Type-safe database operations**
**Modern, maintainable code**
**Dual database support (SQLite & PostgreSQL)**
**Automatic migration management**
**Production-ready implementation**
The codebase is cleaner, more maintainable, and ready for future enhancements. All functionality is preserved, tests are passing, and the bot is ready for production deployment.
---
**Status: COMPLETE & VERIFIED ✅**
*Migration completed on 2026-05-14 with 12 commits and zero breaking changes.*

View File

@@ -1,504 +0,0 @@
# PostgreSQL Migration Guide
This guide walks you through migrating your Discord moderation bot from SQLite to PostgreSQL. PostgreSQL provides better performance, scalability, and concurrent access compared to SQLite, making it ideal for production deployments.
## Overview
The migration process involves:
1. Setting up PostgreSQL and creating a database
2. Configuring environment variables
3. Running the automated data migration script
4. Verifying the migration was successful
5. Starting the bot with PostgreSQL
The entire process typically takes 5-10 minutes depending on your data volume.
## Prerequisites
Before starting the migration, ensure you have:
- **PostgreSQL 12 or later** installed and running
- macOS: `brew install postgresql@15`
- Ubuntu/Debian: `sudo apt-get install postgresql postgresql-contrib`
- Windows: Download from https://www.postgresql.org/download/windows/
- Docker: `docker run -d -e POSTGRES_PASSWORD=password postgres:15`
- **psql command-line tool** (usually included with PostgreSQL)
- Test with: `psql --version`
- **Sufficient disk space** for your data (at least 2x your current SQLite database size)
- **Network access** to PostgreSQL (if using remote server)
## Step 1: Create PostgreSQL Database and User
Connect to PostgreSQL as the superuser (usually `postgres`):
```bash
psql -U postgres
```
Then run these commands in the psql prompt:
```sql
-- Create the database
CREATE DATABASE discord_bot;
-- Create a dedicated user for the bot
CREATE USER discord_bot WITH PASSWORD 'your_secure_password_here';
-- Grant all privileges on the database to the user
GRANT ALL PRIVILEGES ON DATABASE discord_bot TO discord_bot;
-- Connect to the database and grant schema privileges
\c discord_bot
GRANT ALL PRIVILEGES ON SCHEMA public TO discord_bot;
-- Exit psql
\q
```
**Important:** Replace `'your_secure_password_here'` with a strong password. Store this securely.
### Verify PostgreSQL Connection
Test the connection with your new credentials:
```bash
psql -U discord_bot -d discord_bot -h localhost -W
```
You'll be prompted for the password. If successful, you'll see the `discord_bot=#` prompt. Type `\q` to exit.
## Step 2: Configure Environment Variables
Update your `.env` file with PostgreSQL connection details. You have two options:
### Option A: Using DATABASE_URL (Recommended)
```bash
DATABASE_TYPE=postgres
DATABASE_URL=postgresql://discord_bot:your_secure_password_here@localhost:5432/discord_bot
```
### Option B: Using Individual Parameters
```bash
DATABASE_TYPE=postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=discord_bot
POSTGRES_PASSWORD=your_secure_password_here
POSTGRES_DB=discord_bot
POSTGRES_POOL_MIN=2
POSTGRES_POOL_MAX=10
```
### Connection Pool Configuration (Optional)
If you need to tune connection pooling for your workload:
```bash
# Minimum connections to maintain in the pool (default: 2)
POSTGRES_POOL_MIN=2
# Maximum connections allowed in the pool (default: 10)
POSTGRES_POOL_MAX=10
```
**Note:** For most deployments, the defaults are sufficient. Increase `POSTGRES_POOL_MAX` if you see "connection pool exhausted" errors.
### Remote PostgreSQL Server
If using a remote PostgreSQL server (e.g., AWS RDS, Heroku Postgres):
```bash
DATABASE_TYPE=postgres
DATABASE_URL=postgresql://user:password@your-server.example.com:5432/discord_bot
```
Or with individual parameters:
```bash
DATABASE_TYPE=postgres
POSTGRES_HOST=your-server.example.com
POSTGRES_PORT=5432
POSTGRES_USER=discord_bot
POSTGRES_PASSWORD=your_secure_password_here
POSTGRES_DB=discord_bot
```
## Step 3: Run the Data Migration
Before starting the bot, migrate your existing data from SQLite to PostgreSQL:
```bash
pnpm run migrate:data
```
This script will:
- Read all data from your SQLite database (`.muxer-queue.db`)
- Create PostgreSQL tables if they don't exist
- Insert all messages, attachments, muxer jobs, and UI state
- Handle duplicate records gracefully (skips if already migrated)
- Display a summary of migrated records
**Expected output:**
```
[migrate-data] Starting data migration from SQLite to PostgreSQL
[migrate-data] SQLite database opened
[migrate-data] PostgreSQL connection pool initialized
[migrate-data] Migrating muxer_jobs table...
[migrate-data] Migrated muxer_jobs (count: 42)
[migrate-data] Migrating messages table...
[migrate-data] Migrated messages (count: 1,234)
[migrate-data] Migrating attachments table...
[migrate-data] Migrated attachments (count: 567)
[migrate-data] Migrating ui_state table...
[migrate-data] Migrated ui_state (count: 3)
[migrate-data] Data migration completed successfully
```
### Troubleshooting Migration Errors
**Error: "Connection refused"**
- Verify PostgreSQL is running: `pg_isready -h localhost -p 5432`
- Check DATABASE_URL or POSTGRES_HOST is correct
- Ensure firewall allows connections to port 5432
**Error: "FATAL: role 'discord_bot' does not exist"**
- Verify the user was created: `psql -U postgres -c "\du"`
- Recreate the user if needed (see Step 1)
**Error: "permission denied for database 'discord_bot'"**
- Verify privileges were granted: `psql -U postgres -d discord_bot -c "\dp"`
- Re-run the GRANT commands from Step 1
**Error: "relation 'messages' does not exist"**
- This is normal on first migration. The script creates tables automatically.
- If it persists, check PostgreSQL logs for errors
## Step 4: Start the Bot with PostgreSQL
Once migration completes successfully, start the bot:
```bash
# Development mode (with auto-restart on file changes)
pnpm run dev
# Production mode
pnpm run start
```
The bot will now use PostgreSQL for all data operations. You should see in the logs:
```
[config] Database type: postgres
[webserver] Connected to PostgreSQL
```
## Step 5: Verify Migration Success
### Check Record Counts
Verify that all data was migrated correctly by comparing record counts:
```bash
# Check messages count
psql -U discord_bot -d discord_bot -c "SELECT COUNT(*) as message_count FROM messages;"
# Check attachments count
psql -U discord_bot -d discord_bot -c "SELECT COUNT(*) as attachment_count FROM attachments;"
# Check muxer jobs count
psql -U discord_bot -d discord_bot -c "SELECT COUNT(*) as job_count FROM muxer_jobs;"
```
Compare these counts with your SQLite database:
```bash
# SQLite counts
sqlite3 .muxer-queue.db "SELECT COUNT(*) as message_count FROM messages;"
sqlite3 .muxer-queue.db "SELECT COUNT(*) as attachment_count FROM attachments;"
sqlite3 .muxer-queue.db "SELECT COUNT(*) as job_count FROM muxer_jobs;"
```
### Check Data Integrity
Verify a sample of messages:
```bash
psql -U discord_bot -d discord_bot -c "
SELECT id, username, content, created_at
FROM messages
ORDER BY created_at DESC
LIMIT 5;
"
```
### Monitor Bot Logs
Watch the bot logs for any errors:
```bash
# If running with pnpm run dev, logs appear in the terminal
# Look for any database-related errors or warnings
```
### Test API Endpoints
If the bot has a web interface, test it:
```bash
# Check health endpoint
curl http://localhost:3000/health
# Check API endpoints
curl http://localhost:3000/api/messages?channel=<channel_id>
```
## Rollback to SQLite
If you need to revert to SQLite for any reason:
### Step 1: Stop the Bot
```bash
# Press Ctrl+C if running in foreground
# Or kill the process if running in background
```
### Step 2: Update Environment Variables
Change your `.env` file:
```bash
DATABASE_TYPE=sqlite
# Comment out or remove PostgreSQL variables
# DATABASE_URL=...
# POSTGRES_HOST=...
```
### Step 3: Restart the Bot
```bash
pnpm run dev
```
The bot will now use SQLite (`.muxer-queue.db`). Your SQLite data remains unchanged and available.
### Step 4: Verify Rollback
Check logs for:
```
[config] Database type: sqlite
```
**Note:** Any data created while using PostgreSQL will not be in SQLite. If you need that data, migrate it back to PostgreSQL or export it from PostgreSQL first.
## Performance Considerations
### Connection Pooling
PostgreSQL uses connection pooling to manage database connections efficiently:
- **POSTGRES_POOL_MIN=2** — Maintains at least 2 idle connections
- **POSTGRES_POOL_MAX=10** — Allows up to 10 concurrent connections
For most deployments, these defaults are optimal. Adjust if you see:
- "Connection pool exhausted" errors → increase POSTGRES_POOL_MAX
- High memory usage → decrease POSTGRES_POOL_MAX
### Indexes
PostgreSQL automatically creates indexes on frequently queried columns:
```sql
-- Messages table indexes
CREATE INDEX idx_messages_channel_id ON messages(channel_id);
CREATE INDEX idx_messages_user_id ON messages(user_id);
CREATE INDEX idx_messages_created_at ON messages(created_at);
CREATE INDEX idx_messages_guild_id ON messages(guild_id);
-- Attachments table indexes
CREATE INDEX idx_attachments_message_id ON attachments(message_id);
CREATE INDEX idx_attachments_channel_id ON attachments(channel_id);
CREATE INDEX idx_attachments_user_id ON attachments(user_id);
```
These indexes are created automatically during migration. They significantly improve query performance.
### Prepared Statements
All database queries use prepared statements, which:
- Prevent SQL injection attacks
- Improve performance through query plan caching
- Reduce parsing overhead
### Foreign Key Constraints
PostgreSQL enforces referential integrity:
```sql
-- Attachments reference messages
ALTER TABLE attachments
ADD CONSTRAINT fk_attachments_message_id
FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE;
```
This ensures data consistency and prevents orphaned records.
### Query Performance
PostgreSQL typically provides 2-10x better performance than SQLite for:
- Concurrent writes (multiple users sending messages simultaneously)
- Large result sets (querying thousands of messages)
- Complex queries (joins, aggregations)
- Concurrent reads (multiple dashboard users)
### Production Recommendations
For production deployments:
1. **Use a managed PostgreSQL service** (AWS RDS, Google Cloud SQL, Heroku Postgres)
- Automatic backups
- High availability
- Monitoring and alerts
2. **Enable SSL/TLS connections**
```bash
DATABASE_URL=postgresql://user:password@host:5432/db?sslmode=require
```
3. **Set up regular backups**
```bash
# Daily backup
pg_dump -U discord_bot -d discord_bot > backup_$(date +%Y%m%d).sql
```
4. **Monitor connection pool usage**
```bash
psql -U discord_bot -d discord_bot -c "
SELECT datname, count(*) as connections
FROM pg_stat_activity
GROUP BY datname;
"
```
5. **Tune POSTGRES_POOL_MAX based on load**
- Start with default (10)
- Monitor for "connection pool exhausted" errors
- Increase if needed, but keep under 20 for most workloads
6. **Enable query logging for slow queries**
```sql
ALTER SYSTEM SET log_min_duration_statement = 1000;
SELECT pg_reload_conf();
```
## Troubleshooting
### Connection Issues
**Problem:** "Connection refused" or "Connection timeout"
**Solutions:**
- Verify PostgreSQL is running: `pg_isready -h localhost -p 5432`
- Check firewall rules allow port 5432
- Verify DATABASE_URL or POSTGRES_HOST is correct
- Test connection manually: `psql -U discord_bot -d discord_bot -h localhost`
### Authentication Issues
**Problem:** "FATAL: password authentication failed"
**Solutions:**
- Verify password in .env matches the one set in Step 1
- Reset password: `psql -U postgres -c "ALTER USER discord_bot WITH PASSWORD 'new_password';"`
- Check for special characters in password (may need escaping)
### Migration Script Errors
**Problem:** Migration script fails partway through
**Solutions:**
- Check PostgreSQL logs: `tail -f /var/log/postgresql/postgresql.log`
- Verify database exists: `psql -U postgres -l | grep discord_bot`
- Check disk space: `df -h`
- Re-run migration (it's safe to run multiple times — duplicates are skipped)
### Performance Issues
**Problem:** Queries are slow after migration
**Solutions:**
- Verify indexes were created: `psql -U discord_bot -d discord_bot -c "\di"`
- Check query plans: `EXPLAIN ANALYZE SELECT ...`
- Monitor connection pool: `psql -U discord_bot -d discord_bot -c "SELECT count(*) FROM pg_stat_activity;"`
- Increase POSTGRES_POOL_MAX if connections are exhausted
### Data Inconsistencies
**Problem:** Some data appears missing after migration
**Solutions:**
- Compare record counts (see Step 5)
- Check for migration errors in logs
- Verify SQLite database wasn't modified during migration
- Re-run migration (safe to run multiple times)
## FAQ
**Q: Will the bot experience downtime during migration?**
A: Yes, briefly. Stop the bot, run the migration script (usually < 1 minute), then restart. Total downtime: 2-5 minutes.
**Q: Can I migrate data while the bot is running?**
A: Not recommended. Stop the bot first to ensure data consistency. The SQLite database may be locked otherwise.
**Q: What if the migration fails halfway?**
A: It's safe to re-run. The script uses `ON CONFLICT DO NOTHING` to skip duplicate records. Fix the error and run again.
**Q: Can I keep both SQLite and PostgreSQL running?**
A: Yes, but only one can be active at a time (controlled by DATABASE_TYPE). Switching between them requires restarting the bot.
**Q: How do I backup my PostgreSQL data?**
A: Use `pg_dump`:
```bash
pg_dump -U discord_bot -d discord_bot > backup.sql
```
**Q: Can I use PostgreSQL on a remote server?**
A: Yes. Set DATABASE_URL or POSTGRES_HOST to the remote server address. Ensure network connectivity and firewall rules allow access.
**Q: What's the performance difference between SQLite and PostgreSQL?**
A: PostgreSQL is typically 2-10x faster for concurrent operations and large datasets. SQLite is simpler for single-user, small-scale deployments.
**Q: Do I need to change any code?**
A: No. The database adapter handles both SQLite and PostgreSQL transparently. Just change the environment variables.
## Next Steps
After successful migration:
1. **Monitor the bot** for 24 hours to ensure stability
2. **Set up automated backups** for PostgreSQL
3. **Configure monitoring and alerts** for database health
4. **Document your PostgreSQL setup** for your team
5. **Consider archiving old SQLite data** after confirming migration success
## Support
If you encounter issues:
1. Check the troubleshooting section above
2. Review PostgreSQL logs: `tail -f /var/log/postgresql/postgresql.log`
3. Check bot logs for database errors
4. Verify environment variables are set correctly
5. Test PostgreSQL connection manually with psql
For additional help, consult:
- PostgreSQL documentation: https://www.postgresql.org/docs/
- Node.js PostgreSQL client: https://node-postgres.com/
- Project issues: Check the repository issue tracker