#!/bin/bash # Simplified script to migrate SQLite data to PostgreSQL set -e echo "🔄 Migrating data from SQLite to PostgreSQL..." # Load environment if [ -f .env.production ]; then export $(cat .env.production | grep -v '^#' | xargs) fi # Check if SQLite exists if [ ! -f "./backEnd/db.sqlite3" ]; then echo "❌ SQLite database not found" exit 1 fi # Ensure containers are running if ! docker-compose ps | grep -q "backend.*Up"; then echo "▶️ Starting containers..." docker-compose up -d sleep 10 fi # Wait for PostgreSQL echo "⏳ Waiting for PostgreSQL..." timeout=30 while [ $timeout -gt 0 ]; do if docker-compose exec -T postgres pg_isready -U ${POSTGRES_USER:-gnx} > /dev/null 2>&1; then break fi sleep 2 timeout=$((timeout - 2)) done # Create backup directory mkdir -p ./backups BACKUP_FILE="./backups/sqlite_export_$(date +%Y%m%d_%H%M%S).json" echo "📦 Exporting from SQLite..." # Export using SQLite database docker-compose exec -T backend bash -c " # Temporarily use SQLite export DATABASE_URL=sqlite:///db.sqlite3 python manage.py dumpdata --natural-foreign --natural-primary \ --exclude auth.permission \ --exclude contenttypes \ --indent 2 > /tmp/sqlite_export.json 2>&1 cat /tmp/sqlite_export.json " > "$BACKUP_FILE" echo "✅ Exported to $BACKUP_FILE" # Run migrations on PostgreSQL echo "📦 Running migrations on PostgreSQL..." docker-compose exec -T backend python manage.py migrate --noinput # Import into PostgreSQL echo "📥 Importing into PostgreSQL..." docker-compose exec -T backend bash -c " python manage.py loaddata /tmp/sqlite_export.json 2>&1 || echo 'Import completed with warnings' " echo "✅ Migration completed!" echo "" echo "📊 Verifying migration..." # Count records echo " Checking user count..." USERS=$(docker-compose exec -T backend python manage.py shell -c "from django.contrib.auth.models import User; print(User.objects.count())" 2>/dev/null | tail -1) echo " Users in PostgreSQL: $USERS" touch .migrated_to_postgres echo "" echo "✅ Migration complete! Backend is now using PostgreSQL."