# 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 ```