mirror of
https://github.com/hpd840321/starRiverProperty.git
synced 2026-06-09 08:20:31 +08:00
feat: add idempotency check — skip DB restore/DDL if already populated
Former-commit-id: 9db5bc17dcaaefc7148625f081bdb845cc6820d0
This commit is contained in:
@@ -28,6 +28,14 @@ declare -A DB_MAP=(
|
||||
["$DB_P"]="p_2026_04_23_17_28_33.sql.gz"
|
||||
)
|
||||
|
||||
# Check if DB exists and has tables — skip restore if already populated
|
||||
db_has_tables() {
|
||||
local db="$1"
|
||||
local count
|
||||
count=$($MYSQL_CMD -N -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$db'" 2>/dev/null || echo "0")
|
||||
[[ "$count" -gt 0 ]]
|
||||
}
|
||||
|
||||
for db_name in "${!DB_MAP[@]}"; do
|
||||
backup_file="${DB_MAP[$db_name]}"
|
||||
backup_path="$DATA_BACKUP/$backup_file"
|
||||
@@ -37,6 +45,12 @@ for db_name in "${!DB_MAP[@]}"; do
|
||||
continue
|
||||
fi
|
||||
|
||||
# Idempotency: skip if DB already has data
|
||||
if db_has_tables "$db_name"; then
|
||||
log_ok " $db_name already exists ($(du -h "$backup_path" | cut -f1) backup skipped)"
|
||||
continue
|
||||
fi
|
||||
|
||||
log_info "Restoring $db_name from $backup_file ($(du -h "$backup_path" | cut -f1))..."
|
||||
|
||||
# Create DB if not exists
|
||||
@@ -52,16 +66,26 @@ for db_name in "${!DB_MAP[@]}"; do
|
||||
fi
|
||||
done
|
||||
|
||||
# Elevator app also needs second backup (34_*.sql.gz)
|
||||
# Elevator app also needs second backup (34_*.sql.gz) — skip if DB already populated
|
||||
if [[ -f "$DATA_BACKUP/34_2026_04_23_17_28_33.sql.gz" ]]; then
|
||||
if db_has_tables "$DB_ELEVATOR"; then
|
||||
log_ok " $DB_ELEVATOR partition 34 skipped (DB already populated)"
|
||||
else
|
||||
log_info "Restoring elevator DB partition 34..."
|
||||
zcat "$DATA_BACKUP/34_2026_04_23_17_28_33.sql.gz" | sed '/SET @@GLOBAL.GTID_PURGED=/,/;/d' | $MYSQL_CMD "$DB_ELEVATOR"
|
||||
log_ok " $DB_ELEVATOR partition 34 restored"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Apply V2.0.7 DDL (tenant_visitor_floor_policy)
|
||||
log_info "Applying V2.0.7 DDL (tenant_visitor_floor_policy)..."
|
||||
$MYSQL_CMD "$DB_ELEVATOR" < "$REPO_ROOT/docs/sql/tenant_visitor_floor_policy.sql"
|
||||
log_ok " V2 DDL applied"
|
||||
# Apply V2.0.7 DDL (tenant_visitor_floor_policy) — skip if table exists
|
||||
log_info "Checking V2.0.7 DDL (tenant_visitor_floor_policy)..."
|
||||
TABLE_EXISTS=$($MYSQL_CMD -N -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$DB_ELEVATOR' AND table_name='tenant_visitor_floor_policy'" 2>/dev/null || echo "0")
|
||||
if [[ "$TABLE_EXISTS" -gt 0 ]]; then
|
||||
log_ok " tenant_visitor_floor_policy already exists — DDL skipped"
|
||||
else
|
||||
log_info "Applying V2.0.7 DDL..."
|
||||
$MYSQL_CMD "$DB_ELEVATOR" < "$REPO_ROOT/docs/sql/tenant_visitor_floor_policy.sql"
|
||||
log_ok " V2 DDL applied"
|
||||
fi
|
||||
|
||||
log_info "Database preparation complete"
|
||||
|
||||
Reference in New Issue
Block a user