From 52fa9c7babd53e088858e8d55d77dcbd58bbb5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=8D=E7=BC=96=E8=AF=91=E5=B7=A5=E4=BD=9C=E5=8C=BA?= Date: Fri, 1 May 2026 22:33:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20idempotency=20check=20=E2=80=94?= =?UTF-8?q?=20skip=20DB=20restore/DDL=20if=20already=20populated?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 9db5bc17dcaaefc7148625f081bdb845cc6820d0 --- scripts/test-env/prepare-db.sh | 40 +++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/scripts/test-env/prepare-db.sh b/scripts/test-env/prepare-db.sh index b3ed1333..9e049bc1 100755 --- a/scripts/test-env/prepare-db.sh +++ b/scripts/test-env/prepare-db.sh @@ -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 - 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" + 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"