Update script safemigration

This commit is contained in:
2025-11-08 00:36:07 +07:00
parent 42fb17d5e4
commit 6165ea2bfa
2 changed files with 48 additions and 139 deletions

View File

@@ -477,46 +477,22 @@ for attempt in 1 2 3; do
# If pg_dump fails, fall back to EF Core migration script # If pg_dump fails, fall back to EF Core migration script
warn "⚠️ pg_dump failed, falling 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 # Generate complete backup script (all migrations from beginning)
FIRST_MIGRATION=$(get_first_migration) 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
if [ -n "$FIRST_MIGRATION" ]; then log "✅ Complete EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY"
log "📋 Generating complete backup script from initial migration: $FIRST_MIGRATION" BACKUP_SUCCESS=true
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 break
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
else else
# Fallback: generate script 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 (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE"); then if [ $attempt -lt 3 ]; then
log "✅ EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY" warn "⚠️ Backup attempt $attempt failed. Retrying in 5 seconds..."
BACKUP_SUCCESS=true warn " EF CLI Output: $ERROR_OUTPUT"
break sleep 5
else else
# Try fallback without specifying from migration error "❌ Database backup failed after 3 attempts."
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) error " EF CLI Output: $ERROR_OUTPUT"
if [ $attempt -lt 3 ]; then error " Migration aborted for safety reasons."
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 fi
fi 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 # If pg_dump is not available, use EF Core migration script
warn "⚠️ pg_dump not available, using EF Core migration script for backup..." warn "⚠️ pg_dump not available, using EF Core migration script for backup..."
# Get the first migration name to generate complete script # Generate complete backup script (all migrations from beginning)
FIRST_MIGRATION=$(get_first_migration) 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
if [ -n "$FIRST_MIGRATION" ]; then log "✅ Complete EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY"
log "📋 Generating complete backup script from initial migration: $FIRST_MIGRATION" BACKUP_SUCCESS=true
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 break
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
else else
# Fallback: generate script 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 (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$BACKUP_FILE"); then if [ $attempt -lt 3 ]; then
log "✅ EF Core Migration SQL Script generated: $BACKUP_FILE_DISPLAY" warn "⚠️ Backup attempt $attempt failed. Retrying in 5 seconds..."
BACKUP_SUCCESS=true warn " EF CLI Output: $ERROR_OUTPUT"
break sleep 5
else else
# Try fallback without specifying from migration error "❌ Database backup failed after 3 attempts."
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) error " EF CLI Output: $ERROR_OUTPUT"
if [ $attempt -lt 3 ]; then error " Migration aborted for safety reasons."
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 fi
fi fi
fi fi
@@ -672,74 +624,31 @@ fi
# Generate migration script based on database state # Generate migration script based on database state
if [ "$DB_HAS_TABLES" = "true" ]; then if [ "$DB_HAS_TABLES" = "true" ]; then
# For databases with existing tables, we need to generate a complete script # For databases with existing tables, generate a complete idempotent script
# that includes all migrations from the beginning log "📝 Generating complete migration script (idempotent) for database with existing tables..."
log "📝 Generating complete migration script from initial 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 "✅ Complete migration script generated (all migrations, idempotent): $(basename "$MIGRATION_SCRIPT")"
# 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
else else
# Fallback: generate script 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 "$MIGRATION_SCRIPT") 2>&1 || true )
log "📝 Fallback: Generating migration script without specifying from migration..." error " Failed to generate complete migration script."
if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --idempotent --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT"); then error " EF CLI Output: $ERROR_OUTPUT"
log "✅ Migration script generated (idempotent): $(basename "$MIGRATION_SCRIPT")" error " Check the .NET project logs for detailed errors."
else if [ "$CREATE_BACKUP" = "true" ] && [ -n "$BACKUP_FILE_DISPLAY" ]; then
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 " Backup script available at: $BACKUP_FILE_DISPLAY"
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
fi fi
fi fi
else else
# Use full script generation for empty databases (generate script from the very beginning) # Use full script generation for empty databases (generate script from the very beginning)
log "📝 Generating full migration script for empty database..." log "📝 Generating full migration script for empty database..."
if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT"); then
# Get the first migration name to generate script from the beginning log "✅ Complete migration script generated (all migrations): $(basename "$MIGRATION_SCRIPT")"
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
else else
# Fallback: generate script without specifying from migration 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 )
log "📝 Fallback: Generating migration script without specifying from migration..." error " Failed to generate complete migration script."
if (cd "$DB_PROJECT_PATH" && ASPNETCORE_ENVIRONMENT="$ENVIRONMENT" dotnet ef migrations script --no-build --startup-project "$API_PROJECT_PATH" --output "$MIGRATION_SCRIPT"); then error " EF CLI Output: $ERROR_OUTPUT"
log "✅ Migration script generated (fallback): $(basename "$MIGRATION_SCRIPT")" error " Check the .NET project logs for detailed errors."
else if [ "$CREATE_BACKUP" = "true" ] && [ -n "$BACKUP_FILE_DISPLAY" ]; then
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 " Backup script available at: $BACKUP_FILE_DISPLAY"
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
fi fi
fi fi
fi fi

View File

@@ -1,7 +1,7 @@
{ {
"PostgreSql": { "PostgreSql": {
"ConnectionString": "Host=kaigen-db.kaigen.managing.live;Port=5433;Database=managing;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=5433;Database=orleans;Username=postgres;Password=2ab5423dcca4aa2d" "Orleans": "Host=kaigen-db.kaigen.managing.live;Port=5432;Database=orleans;Username=postgres;Password=2ab5423dcca4aa2d"
}, },
"InfluxDb": { "InfluxDb": {
"Url": "https://influx-db.kaigen.managing.live", "Url": "https://influx-db.kaigen.managing.live",