guoziyun 1 mēnesi atpakaļ
vecāks
revīzija
c6a2702d3f
1 mainītis faili ar 21 papildinājumiem un 11 dzēšanām
  1. 21 11
      oms/scripts/migrate-clickhouse-events-partition.sh

+ 21 - 11
oms/scripts/migrate-clickhouse-events-partition.sh

@@ -182,13 +182,23 @@ print_sql_block() {
   printf '\n[%s]\n%s\n' "$title" "$sql"
 }
 
-run_step() {
+run_sql_step() {
   local title="$1"
-  local command="$2"
+  local sql="$2"
+  printf '\n== %s ==\n' "$title"
+  printf 'SQL:\n%s\n' "$sql"
+  if [[ $DRY_RUN -eq 0 ]]; then
+    run_sql "$sql"
+  fi
+}
+
+run_cmd_step() {
+  local title="$1"
+  shift
   printf '\n== %s ==\n' "$title"
-  printf '%s\n' "$command"
+  printf 'CMD: %s\n' "$*"
   if [[ $DRY_RUN -eq 0 ]]; then
-    eval "$command"
+    "$@"
   fi
 }
 
@@ -291,19 +301,19 @@ if [[ $DRY_RUN -eq 1 ]]; then
   exit 0
 fi
 
-run_step "Create partitioned destination table" "run_sql \"$create_sql\""
+run_sql_step "Create partitioned destination table" "$create_sql"
 
 for ym in "${historical_months[@]}"; do
   month_start="${ym:0:4}-${ym:4:2}-01 00:00:00"
   insert_sql="INSERT INTO ${DB_NAME}.${DST_TABLE} SELECT * FROM ${DB_NAME}.${SRC_TABLE} WHERE time >= toDateTime('${month_start}') AND time < addMonths(toDateTime('${month_start}'), 1)"
-  run_step "Backfill month ${ym}" "run_sql \"$insert_sql\""
+  run_sql_step "Backfill month ${ym}" "$insert_sql"
 done
 
 if [[ $SKIP_PM2 -eq 0 ]]; then
-  run_step "Stop PM2 ingestor" "pm2 stop ${INGESTOR_PM2_NAME}"
+  run_cmd_step "Stop PM2 ingestor" pm2 stop "${INGESTOR_PM2_NAME}"
 fi
 
-run_step "Backfill current month ${CURRENT_MONTH}" "run_sql \"$current_month_sql\""
+run_sql_step "Backfill current month ${CURRENT_MONTH}" "$current_month_sql"
 
 old_rows="$(run_sql "SELECT count() FROM ${DB_NAME}.${SRC_TABLE} FORMAT TabSeparatedRaw")"
 new_rows="$(run_sql "SELECT count() FROM ${DB_NAME}.${DST_TABLE} FORMAT TabSeparatedRaw")"
@@ -317,13 +327,13 @@ if [[ "$old_rows" != "$new_rows" ]]; then
   exit 1
 fi
 
-run_step "Atomic rename" "run_sql \"$rename_sql\""
+run_sql_step "Atomic rename" "$rename_sql"
 
 if [[ $SKIP_PM2 -eq 0 ]]; then
-  run_step "Start PM2 ingestor" "pm2 start ${INGESTOR_PM2_NAME}"
+  run_cmd_step "Start PM2 ingestor" pm2 start "${INGESTOR_PM2_NAME}"
 fi
 
 verify_sql="SELECT partition, sum(rows) AS rows, formatReadableSize(sum(data_compressed_bytes)) AS compressed_size, count() AS parts FROM system.parts WHERE database = '${DB_NAME}' AND \`table\` = '${SRC_TABLE}' AND active = 1 GROUP BY partition ORDER BY partition"
-run_step "Verify partitions" "run_sql \"$verify_sql\""
+run_sql_step "Verify partitions" "$verify_sql"
 
 printf '\nMigration completed. Keep %s.%s for rollback until you are satisfied.\n' "$DB_NAME" "$BACKUP_TABLE"