From 6165ea2bfa508562bbb9170c0a3ae396010264ff Mon Sep 17 00:00:00 2001 From: cryptooda Date: Sat, 8 Nov 2025 00:36:07 +0700 Subject: [PATCH] Update script safemigration --- scripts/safe-migrate.sh | 183 +++++------------- .../appsettings.ProductionLocal.json | 4 +- 2 files changed, 48 insertions(+), 139 deletions(-) diff --git a/scripts/safe-migrate.sh b/scripts/safe-migrate.sh index 4b6380af..6dcf8d5e 100755 --- a/scripts/safe-migrate.sh +++ b/scripts/safe-migrate.sh @@ -477,46 +477,22 @@ for attempt in 1 2 3; do # If pg_dump fails, fall back to EF Core migration script warn "⚠️ pg_dump failed, falling back to EF Core migration script..." - # Get the first migration name to generate complete script - FIRST_MIGRATION=$(get_first_migration) - - if [ -n "$FIRST_MIGRATION" ]; then - log "📋 Generating complete backup script from initial migration: $FIRST_MIGRATION" - if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --from "$FIRST_MIGRATION" --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE"); then - log "✅ Complete EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY" - BACKUP_SUCCESS=true - break - else - # Try fallback without specifying from migration - ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE") 2>&1 || true) - if [ $attempt -lt 3 ]; then - warn "⚠️ Backup attempt $attempt failed. Retrying in 5 seconds..." - warn " EF CLI Output: $ERROR_OUTPUT" - sleep 5 - else - error "❌ Database backup failed after 3 attempts." - error " EF CLI Output: $ERROR_OUTPUT" - error " Migration aborted for safety reasons." - fi - fi + # Generate complete backup script (all migrations from beginning) + log "📋 Generating complete backup script (all migrations)..." + if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE"); then + log "✅ Complete EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY" + BACKUP_SUCCESS=true + break else - # Fallback: generate script without specifying from migration - if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE"); then - log "✅ EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY" - BACKUP_SUCCESS=true - break + ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE") 2>&1 || true) + if [ $attempt -lt 3 ]; then + warn "⚠️ Backup attempt $attempt failed. Retrying in 5 seconds..." + warn " EF CLI Output: $ERROR_OUTPUT" + sleep 5 else - # Try fallback without specifying from migration - ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE") 2>&1 || true) - if [ $attempt -lt 3 ]; then - warn "⚠️ Backup attempt $attempt failed. Retrying in 5 seconds..." - warn " EF CLI Output: $ERROR_OUTPUT" - sleep 5 - else - error "❌ Database backup failed after 3 attempts." - error " EF CLI Output: $ERROR_OUTPUT" - error " Migration aborted for safety reasons." - fi + error "❌ Database backup failed after 3 attempts." + error " EF CLI Output: $ERROR_OUTPUT" + error " Migration aborted for safety reasons." fi fi fi @@ -524,46 +500,22 @@ for attempt in 1 2 3; do # If pg_dump is not available, use EF Core migration script warn "⚠️ pg_dump not available, using EF Core migration script for backup..." - # Get the first migration name to generate complete script - FIRST_MIGRATION=$(get_first_migration) - - if [ -n "$FIRST_MIGRATION" ]; then - log "📋 Generating complete backup script from initial migration: $FIRST_MIGRATION" - if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --from "$FIRST_MIGRATION" --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE"); then - log "✅ Complete EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY" - BACKUP_SUCCESS=true - break - else - # Try fallback without specifying from migration - ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE") 2>&1 || true) - if [ $attempt -lt 3 ]; then - warn "⚠️ Backup attempt $attempt failed. Retrying in 5 seconds..." - warn " EF CLI Output: $ERROR_OUTPUT" - sleep 5 - else - error "❌ Database backup failed after 3 attempts." - error " EF CLI Output: $ERROR_OUTPUT" - error " Migration aborted for safety reasons." - fi - fi + # Generate complete backup script (all migrations from beginning) + log "📋 Generating complete backup script (all migrations)..." + if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE"); then + log "✅ Complete EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY" + BACKUP_SUCCESS=true + break else - # Fallback: generate script without specifying from migration - if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE"); then - log "✅ EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY" - BACKUP_SUCCESS=true - break + ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE") 2>&1 || true) + if [ $attempt -lt 3 ]; then + warn "⚠️ Backup attempt $attempt failed. Retrying in 5 seconds..." + warn " EF CLI Output: $ERROR_OUTPUT" + sleep 5 else - # Try fallback without specifying from migration - ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE") 2>&1 || true) - if [ $attempt -lt 3 ]; then - warn "⚠️ Backup attempt $attempt failed. Retrying in 5 seconds..." - warn " EF CLI Output: $ERROR_OUTPUT" - sleep 5 - else - error "❌ Database backup failed after 3 attempts." - error " EF CLI Output: $ERROR_OUTPUT" - error " Migration aborted for safety reasons." - fi + error "❌ Database backup failed after 3 attempts." + error " EF CLI Output: $ERROR_OUTPUT" + error " Migration aborted for safety reasons." fi fi fi @@ -672,74 +624,31 @@ fi # Generate migration script based on database state if [ "$DB_HAS_TABLES" = "true" ]; then - # For databases with existing tables, we need to generate a complete script - # that includes all migrations from the beginning - log "📝 Generating complete migration script from initial migration..." - - # Get the first migration name to generate script from the beginning - FIRST_MIGRATION=$(get_first_migration) - - if [ -n "$FIRST_MIGRATION" ]; then - log "📋 Generating complete script for all migrations (idempotent)..." - if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT"); then - log "✅ Complete migration script generated (all migrations, idempotent): $(basename "$MIGRATION_SCRIPT")" - else - ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT") 2>&1 || true ) - error "❌ Failed to generate complete migration script." - error " EF CLI Output: $ERROR_OUTPUT" - error " Check the .NET project logs for detailed errors." - if [ "$CREATE_BACKUP" = "true" ] && [ -n "$BACKUP_FILE_DISPLAY" ]; then - error " Backup script available at: $BACKUP_FILE_DISPLAY" - fi - fi + # For databases with existing tables, generate a complete idempotent script + log "📝 Generating complete migration script (idempotent) for database with existing tables..." + if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT"); then + log "✅ Complete migration script generated (all migrations, idempotent): $(basename "$MIGRATION_SCRIPT")" else - # Fallback: generate script without specifying from migration - log "📝 Fallback: Generating migration script without specifying from migration..." - if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT"); then - log "✅ Migration script generated (idempotent): $(basename "$MIGRATION_SCRIPT")" - else - ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT") 2>&1 || true ) - error "❌ Failed to generate idempotent migration script." - error " EF CLI Output: $ERROR_OUTPUT" - error " Check the .NET project logs for detailed errors." - if [ "$CREATE_BACKUP" = "true" ] && [ -n "$BACKUP_FILE_DISPLAY" ]; then - error " Backup script available at: $BACKUP_FILE_DISPLAY" - fi + ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT") 2>&1 || true ) + error "❌ Failed to generate complete migration script." + error " EF CLI Output: $ERROR_OUTPUT" + error " Check the .NET project logs for detailed errors." + if [ "$CREATE_BACKUP" = "true" ] && [ -n "$BACKUP_FILE_DISPLAY" ]; then + error " Backup script available at: $BACKUP_FILE_DISPLAY" fi fi else # Use full script generation for empty databases (generate script from the very beginning) log "📝 Generating full migration script for empty database..." - - # Get the first migration name to generate script from the beginning - FIRST_MIGRATION=$(get_first_migration) - - if [ -n "$FIRST_MIGRATION" ]; then - log "📋 Generating complete script for all migrations..." - if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT"); then - log "✅ Complete migration script generated (all migrations): $(basename "$MIGRATION_SCRIPT")" - else - ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT") 2>&1 || true ) - error "❌ Failed to generate complete migration script." - error " EF CLI Output: $ERROR_OUTPUT" - error " Check the .NET project logs for detailed errors." - if [ "$CREATE_BACKUP" = "true" ] && [ -n "$BACKUP_FILE_DISPLAY" ]; then - error " Backup script available at: $BACKUP_FILE_DISPLAY" - fi - fi + if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT"); then + log "✅ Complete migration script generated (all migrations): $(basename "$MIGRATION_SCRIPT")" else - # Fallback: generate script without specifying from migration - log "📝 Fallback: Generating migration script without specifying from migration..." - if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT"); then - log "✅ Migration script generated (fallback): $(basename "$MIGRATION_SCRIPT")" - else - ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT") 2>&1 || true ) - error "❌ Failed to generate fallback migration script." - error " EF CLI Output: $ERROR_OUTPUT" - error " Check the .NET project logs for detailed errors." - if [ "$CREATE_BACKUP" = "true" ] && [ -n "$BACKUP_FILE_DISPLAY" ]; then - error " Backup script available at: $BACKUP_FILE_DISPLAY" - fi + ERROR_OUTPUT=$( (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT") 2>&1 || true ) + error "❌ Failed to generate complete migration script." + error " EF CLI Output: $ERROR_OUTPUT" + error " Check the .NET project logs for detailed errors." + if [ "$CREATE_BACKUP" = "true" ] && [ -n "$BACKUP_FILE_DISPLAY" ]; then + error " Backup script available at: $BACKUP_FILE_DISPLAY" fi fi fi diff --git a/src/Managing.Api/appsettings.ProductionLocal.json b/src/Managing.Api/appsettings.ProductionLocal.json index 8af3fdc4..3f4b9df5 100644 --- a/src/Managing.Api/appsettings.ProductionLocal.json +++ b/src/Managing.Api/appsettings.ProductionLocal.json @@ -1,7 +1,7 @@ { "PostgreSql": { - "ConnectionString": "Host=kaigen-db.kaigen.managing.live;Port=5433;Database=managing;Username=postgres;Password=2ab5423dcca4aa2d", - "Orleans": "Host=kaigen-db.kaigen.managing.live;Port=5433;Database=orleans;Username=postgres;Password=2ab5423dcca4aa2d" + "ConnectionString": "Host=kaigen-db.kaigen.managing.live;Port=5432;Database=managing;Username=postgres;Password=2ab5423dcca4aa2d", + "Orleans": "Host=kaigen-db.kaigen.managing.live;Port=5432;Database=orleans;Username=postgres;Password=2ab5423dcca4aa2d" }, "InfluxDb": { "Url": "https://influx-db.kaigen.managing.live",