guoziyun 1 månad sedan
förälder
incheckning
04402862cf
1 ändrade filer med 31 tillägg och 3 borttagningar
  1. 31 3
      oms/scripts/migrate-clickhouse-events-partition.sh

+ 31 - 3
oms/scripts/migrate-clickhouse-events-partition.sh

@@ -292,12 +292,36 @@ printf 'Reconcile only: %s\n' "$([[ $RECONCILE_ONLY -eq 1 ]] && echo yes || echo
 diff_sql="SELECT src.ym, src.rows AS source_rows, ifNull(dst.rows, 0) AS destination_rows, src.rows - ifNull(dst.rows, 0) AS missing_rows FROM (SELECT toYYYYMM(time) AS ym, count() AS rows FROM ${DB_NAME}.${SRC_TABLE} GROUP BY ym) AS src LEFT JOIN (SELECT toYYYYMM(time) AS ym, count() AS rows FROM ${DB_NAME}.${DST_TABLE} GROUP BY ym) AS dst USING (ym) ORDER BY ym FORMAT PrettyCompact"
 
 current_month_start="${CURRENT_MONTH:0:4}-${CURRENT_MONTH:4:2}-01 00:00:00"
-reconcile_current_month_sql="INSERT INTO ${DB_NAME}.${DST_TABLE} SELECT src.* FROM ${DB_NAME}.${SRC_TABLE} AS src LEFT JOIN ${DB_NAME}.${DST_TABLE} AS dst ON src.log_id = dst.log_id WHERE src.time >= toDateTime('${current_month_start}') AND dst.log_id IS NULL"
+
+build_reconcile_sql_for_month() {
+  local ym="$1"
+  local month_start="${ym:0:4}-${ym:4:2}-01 00:00:00"
+  cat <<EOF
+INSERT INTO ${DB_NAME}.${DST_TABLE}
+SELECT src.*
+FROM ${DB_NAME}.${SRC_TABLE} AS src
+LEFT JOIN ${DB_NAME}.${DST_TABLE} AS dst ON src.log_id = dst.log_id
+WHERE src.time >= toDateTime('${month_start}')
+  AND src.time < addMonths(toDateTime('${month_start}'), 1)
+  AND dst.log_id IS NULL
+EOF
+}
 
 print_sql_block "Per-month diff" "$diff_sql"
 
 if [[ $RECONCILE_ONLY -eq 1 ]]; then
-  print_sql_block "Reconcile current month missing rows" "$reconcile_current_month_sql"
+  reconcile_months="$(run_sql "SELECT src.ym FROM (SELECT toYYYYMM(time) AS ym, count() AS rows FROM ${DB_NAME}.${SRC_TABLE} GROUP BY ym) AS src LEFT JOIN (SELECT toYYYYMM(time) AS ym, count() AS rows FROM ${DB_NAME}.${DST_TABLE} GROUP BY ym) AS dst USING (ym) WHERE src.rows > ifNull(dst.rows, 0) ORDER BY ym FORMAT TSV")"
+
+  if [[ -z "$reconcile_months" ]]; then
+    printf '\nNo missing months detected. Source and destination are already aligned by month.\n'
+    exit 0
+  fi
+
+  while IFS= read -r ym; do
+    [[ -z "$ym" ]] && continue
+    reconcile_sql="$(build_reconcile_sql_for_month "$ym")"
+    print_sql_block "Reconcile month ${ym} missing rows" "$reconcile_sql"
+  done <<< "$reconcile_months"
 
   if [[ $DRY_RUN -eq 1 ]]; then
     printf '\nDry-run only. Re-run with --execute --reconcile-only to perform reconciliation.\n'
@@ -311,7 +335,11 @@ if [[ $RECONCILE_ONLY -eq 1 ]]; then
   printf '\nPer-month diff before reconcile:\n'
   run_sql "$diff_sql"
 
-  run_sql_step "Reconcile current month missing rows" "$reconcile_current_month_sql"
+  while IFS= read -r ym; do
+    [[ -z "$ym" ]] && continue
+    reconcile_sql="$(build_reconcile_sql_for_month "$ym")"
+    run_sql_step "Reconcile month ${ym} missing rows" "$reconcile_sql"
+  done <<< "$reconcile_months"
 
   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")"