# Migration Guide This comprehensive guide covers all migration scenarios for Readur, including version upgrades, storage migrations, database migrations, and platform migrations. ## Version Upgrades ### Upgrading from v2.x to v3.x #### Pre-Upgrade Checklist - [ ] Review breaking changes in release notes - [ ] Backup database and files - [ ] Test upgrade in staging environment - [ ] Schedule maintenance window - [ ] Notify users of planned downtime #### Upgrade Steps 1. **Stop the application** ```bash docker-compose down # or systemctl stop readur ``` **Backup current state:** Create comprehensive backups of both database and file data before proceeding. ```bash # Database backup pg_dump $DATABASE_URL > backup_v2_$(date +%Y%m%d).sql # File backup tar -czf files_backup_v2_$(date +%Y%m%d).tar.gz /var/readur/uploads ``` **Update configuration:** Add new environment variables and configuration options required for the new version. ```bash # New environment variables in v3 echo "OIDC_ENABLED=false" >> .env echo "FEATURE_MULTI_LANGUAGE_OCR=true" >> .env echo "FEATURE_LABELS=true" >> .env ``` **Run database migrations:** Apply database schema changes required for the new version. ```bash # Pull new version docker pull readur:v3.0.0 # Run migrations docker run --rm \ -e DATABASE_URL=$DATABASE_URL \ readur:v3.0.0 \ cargo run --bin migrate ``` **Update and start application:** Deploy the new version and restart the application services. ```bash # Update docker-compose.yml sed -i 's/readur:v2/readur:v3.0.0/g' docker-compose.yml # Start application docker-compose up -d ``` **Verify upgrade:** Confirm that the upgrade completed successfully and all systems are functioning correctly. ```bash # Check version curl http://localhost:8080/api/version # Check health curl http://localhost:8080/health # Verify migrations psql $DATABASE_URL -c "SELECT * FROM _sqlx_migrations ORDER BY installed_on DESC LIMIT 5;" ``` ### Rollback Procedure If issues occur during upgrade: ```bash # Stop v3 docker-compose down # Restore database psql $DATABASE_URL < backup_v2_$(date +%Y%m%d).sql # Restore configuration git checkout v2.x.x docker-compose.yml git checkout v2.x.x .env # Start v2 docker-compose up -d ``` ## Storage Migration ### Local to S3 Migration #### Prerequisites - [ ] S3 bucket created and accessible - [ ] IAM credentials with appropriate permissions - [ ] Sufficient network bandwidth - [ ] Migration tool installed: `cargo install --path . --bin migrate_to_s3` #### Step 1: Prepare S3 Environment ```bash # Test S3 access aws s3 ls s3://readur-documents/ # Create bucket structure aws s3api put-object --bucket readur-documents --key documents/ aws s3api put-object --bucket readur-documents --key thumbnails/ aws s3api put-object --bucket readur-documents --key processed/ ``` #### Step 2: Configure S3 Settings ```yaml # Update environment variables S3_ENABLED: true S3_BUCKET_NAME: readur-documents S3_REGION: us-east-1 S3_ACCESS_KEY_ID: AKIAIOSFODNN7EXAMPLE S3_SECRET_ACCESS_KEY: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY S3_PREFIX: production/ S3_STORAGE_CLASS: STANDARD_IA ``` #### Step 3: Run Migration ```bash # Dry run first ./migrate_to_s3 --dry-run # Expected output: # Files to migrate: 5,432 # Total size: 45.6 GB # Estimated time: 2.5 hours # Run actual migration with progress tracking ./migrate_to_s3 \ --enable-rollback \ --batch-size 100 \ --parallel-uploads 10 \ --verbose \ 2>&1 | tee migration_$(date +%Y%m%d).log ``` #### Step 4: Verify Migration ```bash # Check file count aws s3 ls s3://readur-documents/documents/ --recursive | wc -l # Verify random samples ./migrate_to_s3 --verify --sample-size 100 # Check database references psql $DATABASE_URL < dump.sql # 2. Stop old PostgreSQL systemctl stop postgresql-14 # 3. Install PostgreSQL 15 apt-get install postgresql-15 # 4. Initialize new cluster /usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/15/data # 5. Restore database psql -h new_host -U postgres < dump.sql # 6. Update connection string export DATABASE_URL="postgresql://readur:password@localhost:5432/readur?sslmode=require" # 7. Test connection psql $DATABASE_URL -c "SELECT version();" ``` ### Database Server Migration ```bash #!/bin/bash # migrate-database.sh OLD_DB="postgresql://user:pass@old-server/readur" NEW_DB="postgresql://user:pass@new-server/readur" # 1. Create new database psql $NEW_DB -c "CREATE DATABASE readur;" # 2. Dump schema and data pg_dump $OLD_DB --no-owner --clean --if-exists > readur_dump.sql # 3. Restore to new server psql $NEW_DB < readur_dump.sql # 4. Verify data psql $NEW_DB < 0.05 annotations: summary: "High error rate after migration" - alert: SlowQueries expr: pg_stat_database_blks_hit_ratio < 0.95 annotations: summary: "Database cache hit ratio low after migration" ``` ## Migration Best Practices 1. **Always test in staging** before production migration 2. **Take comprehensive backups** before starting 3. **Document the process** for future reference 4. **Monitor closely** after migration 5. **Have a rollback plan** ready 6. **Communicate with users** about downtime 7. **Validate data integrity** at each step 8. **Keep migration logs** for troubleshooting 9. **Update documentation** after migration 10. **Plan for peak traffic** when coming back online