#!/bin/bash # Script to migrate data from SQLite to PostgreSQL set -e echo "🔄 Starting SQLite to PostgreSQL Migration..." # Check if SQLite database exists SQLITE_DB="./backEnd/db.sqlite3" if [ ! -f "$SQLITE_DB" ]; then echo "❌ SQLite database not found at $SQLITE_DB" exit 1 fi echo "✅ Found SQLite database" # Check if PostgreSQL is running if ! docker-compose ps postgres | grep -q "Up"; then echo "❌ PostgreSQL container is not running. Please start it first:" echo " docker-compose up -d postgres" exit 1 fi echo "✅ PostgreSQL container is running" # Load environment variables if [ -f .env.production ]; then export $(cat .env.production | grep -v '^#' | xargs) fi # Check if DATABASE_URL is set for PostgreSQL if [ -z "$DATABASE_URL" ] || ! echo "$DATABASE_URL" | grep -q "postgresql://"; then echo "❌ DATABASE_URL is not set to PostgreSQL" echo " Please update .env.production with PostgreSQL DATABASE_URL" exit 1 fi echo "✅ PostgreSQL DATABASE_URL is configured" # Create backup directory BACKUP_DIR="./backups" mkdir -p "$BACKUP_DIR" TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="$BACKUP_DIR/sqlite_backup_$TIMESTAMP.json" echo "📦 Exporting data from SQLite..." echo " Backup will be saved to: $BACKUP_FILE" # Export data from SQLite using Django's dumpdata # First, temporarily switch to SQLite docker-compose exec -T backend bash -c " export DATABASE_URL=sqlite:///db.sqlite3 python manage.py dumpdata --natural-foreign --natural-primary --exclude auth.permission --exclude contenttypes > /tmp/sqlite_export.json 2>&1 || true cat /tmp/sqlite_export.json " > "$BACKUP_FILE" # Check if export was successful if [ ! -s "$BACKUP_FILE" ] || grep -q "Error\|Traceback\|Exception" "$BACKUP_FILE"; then echo "⚠️ Warning: Export may have issues, but continuing..." fi echo "✅ Data exported to $BACKUP_FILE" echo " File size: $(du -h "$BACKUP_FILE" | cut -f1)" # Wait for PostgreSQL to be ready echo "⏳ Waiting for PostgreSQL to be ready..." timeout=30 while [ $timeout -gt 0 ]; do if docker-compose exec -T postgres pg_isready -U ${POSTGRES_USER:-gnx} > /dev/null 2>&1; then echo "✅ PostgreSQL is ready" break fi echo " Waiting for PostgreSQL... ($timeout seconds remaining)" sleep 2 timeout=$((timeout - 2)) done if [ $timeout -le 0 ]; then echo "❌ PostgreSQL is not ready. Please check the logs:" echo " docker-compose logs postgres" exit 1 fi # Create database if it doesn't exist echo "📊 Ensuring PostgreSQL database exists..." docker-compose exec -T postgres psql -U ${POSTGRES_USER:-gnx} -d postgres -c "SELECT 1 FROM pg_database WHERE datname='${POSTGRES_DB:-gnxdb}'" | grep -q 1 || \ docker-compose exec -T postgres psql -U ${POSTGRES_USER:-gnx} -d postgres -c "CREATE DATABASE ${POSTGRES_DB:-gnxdb};" echo "✅ Database exists or created" # Run migrations on PostgreSQL echo "📦 Running migrations on PostgreSQL..." docker-compose exec -T backend python manage.py migrate --noinput echo "✅ Migrations completed" # Import data into PostgreSQL echo "📥 Importing data into PostgreSQL..." if docker-compose exec -T backend bash -c "python manage.py loaddata /tmp/sqlite_export.json" < "$BACKUP_FILE" 2>&1 | tee /tmp/import_log.txt; then echo "✅ Data imported successfully" else echo "⚠️ Warning: Some data may not have imported. Check the log above." echo " You can retry the import manually:" echo " docker-compose exec backend python manage.py loaddata /tmp/sqlite_export.json" fi # Verify data transfer echo "🔍 Verifying data transfer..." SQLITE_COUNT=$(docker-compose exec -T backend bash -c "export DATABASE_URL=sqlite:///db.sqlite3 && python manage.py shell -c \"from django.contrib.auth.models import User; print(User.objects.count())\"" 2>/dev/null | tail -1 || echo "0") POSTGRES_COUNT=$(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 "0") echo "" echo "📊 Migration Summary:" echo " SQLite Users: $SQLITE_COUNT" echo " PostgreSQL Users: $POSTGRES_COUNT" echo "" # Create a flag file to indicate migration is complete touch .migrated_to_postgres echo "✅ Migration completed!" echo "" echo "📋 Next steps:" echo " 1. Verify the data in PostgreSQL:" echo " docker-compose exec backend python manage.py shell" echo "" echo " 2. Test the application with PostgreSQL" echo "" echo " 3. Once verified, you can backup and remove SQLite:" echo " mv backEnd/db.sqlite3 backEnd/db.sqlite3.backup" echo "" echo " Backup file saved at: $BACKUP_FILE"