de973f5613
Test and Lint / backend-test (push) Successful in 1m9s
continuous-integration/drone/push Build is passing
Test and Lint / frontend-test (push) Successful in 2m15s
Version and Release / version-bump (push) Successful in 36s
Version and Release / trigger-drone (push) Successful in 4s
Major fixes for production deployment issues: 1. Migration System: - Add safe migration runner that handles existing schema - Create migration helper functions for idempotent operations - Auto-detect existing tables and mark migrations as applied - Handle "relation already exists" errors gracefully 2. Production Initialization: - Create init-production.sh script for proper startup sequence - Fix directory creation and permissions - Add admin user creation from environment variables - Ensure proper service initialization order 3. Documentation: - Add comprehensive PRODUCTION_DEPLOYMENT_GUIDE.md - Add MIGRATION_ERROR_FIX.md for immediate issue resolution - Document all known production issues and solutions - Include backup/restore procedures 4. Safety Improvements: - Add migrate:safe npm script for production use - Update wait-for-db.sh to use safe migrations in production - Add proper error handling and logging This resolves the "relation already exists" error and prevents similar issues in future deployments. The safe migration system can handle both fresh installations and existing databases. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
111 lines
2.9 KiB
Markdown
111 lines
2.9 KiB
Markdown
# Quick Fix for Migration Error
|
|
|
|
## Immediate Fix
|
|
|
|
The error "relation photo_categories already exists" occurs because the database already has tables but the migration tracking doesn't know they were applied.
|
|
|
|
### Option 1: Use Safe Migration Runner (Recommended)
|
|
|
|
Update your `docker-compose.prod.yml` to use the safe migration command:
|
|
|
|
```yaml
|
|
backend:
|
|
environment:
|
|
- NODE_ENV=production
|
|
# ... other config ...
|
|
```
|
|
|
|
Then update the `wait-for-db.sh` (already done) to use `npm run migrate:safe` in production.
|
|
|
|
### Option 2: Quick Manual Fix
|
|
|
|
If you need to fix the running system immediately:
|
|
|
|
```bash
|
|
# 1. Enter the backend container
|
|
docker-compose -f docker-compose.prod.yml exec backend sh
|
|
|
|
# 2. Run the safe migration script
|
|
npm run migrate:safe
|
|
|
|
# 3. If that fails, manually mark migrations as applied:
|
|
docker-compose -f docker-compose.prod.yml exec db psql -U picpeak -d picpeak
|
|
|
|
# In PostgreSQL:
|
|
CREATE TABLE IF NOT EXISTS migrations (
|
|
id SERIAL PRIMARY KEY,
|
|
filename VARCHAR(255) UNIQUE NOT NULL,
|
|
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Mark existing migrations as applied
|
|
INSERT INTO migrations (filename) VALUES
|
|
('init.js'),
|
|
('004_add_categories_and_cms.js'),
|
|
('006_add_photo_counter_to_categories.js'),
|
|
('007_add_read_at_to_activity_logs.js'),
|
|
('008_add_language_support_to_email_templates.js'),
|
|
('009_update_german_email_templates.js'),
|
|
('010_add_missing_email_templates.js'),
|
|
('011_add_user_upload_settings.js'),
|
|
('012_add_hero_photo_id.js'),
|
|
('013_fix_email_links_and_date_format.js'),
|
|
('014_add_default_welcome_message.js'),
|
|
('014_add_host_name_to_events.js'),
|
|
('015_add_login_attempts_table.js'),
|
|
('016_add_auth_security_columns.js'),
|
|
('017_add_token_revocation_tables.js')
|
|
ON CONFLICT (filename) DO NOTHING;
|
|
|
|
\q
|
|
```
|
|
|
|
### Option 3: Fresh Start (Nuclear Option)
|
|
|
|
If you don't have important data yet:
|
|
|
|
```bash
|
|
# Stop everything
|
|
docker-compose -f docker-compose.prod.yml down
|
|
|
|
# Remove database volume
|
|
docker volume rm wedding-photo-sharing_postgres_data
|
|
|
|
# Start fresh
|
|
docker-compose -f docker-compose.prod.yml up -d
|
|
```
|
|
|
|
## Root Cause
|
|
|
|
The issue happens when:
|
|
1. Database volume persists between deployments
|
|
2. Migration tracking table gets out of sync
|
|
3. The original migration runner doesn't check for existing tables
|
|
|
|
## Permanent Solution
|
|
|
|
The new safe migration runner (`migrate:safe`) handles this by:
|
|
1. Checking if tables exist before creating them
|
|
2. Catching "already exists" errors gracefully
|
|
3. Auto-detecting existing schema and marking migrations as applied
|
|
|
|
## Next Steps
|
|
|
|
After fixing the migration issue:
|
|
|
|
1. Create admin user:
|
|
```bash
|
|
docker-compose -f docker-compose.prod.yml exec backend node scripts/create-admin.js \
|
|
--username admin \
|
|
--email admin@yourdomain.com
|
|
```
|
|
|
|
2. Check health:
|
|
```bash
|
|
curl http://yourdomain.com/api/health
|
|
```
|
|
|
|
3. Monitor logs:
|
|
```bash
|
|
docker-compose -f docker-compose.prod.yml logs -f backend
|
|
``` |