fix_sync_meta.py 3.78 KB
# -*- coding: utf-8 -*-
"""
修正 _sync_meta: 将 last_sync_time 设为各ODS表实际的 max(updated_date)
然后跑一次增量同步验证
"""
import pymysql
from pymysql.cursors import DictCursor

TARGET_CONFIG = {
    'host': '192.168.2.155', 'port': 3308,
    'user': 'user_jiangmy', 'password': 'PYuxMTB1',
    'database': 'test_excel', 'charset': 'utf8mb4',
    'cursorclass': DictCursor,
}

# ODS表名列表
ODS_TABLES = [
    'ods_service_merchant',
    'ods_shop_merchant_director_config',
    'ods_merchant_brand_model',
    'ods_staff_info',
    'ods_base_goods',
    'ods_goods_info',
    'ods_upgrade_goods',
    'ods_merchant_goods_info',
    'ods_merchant_package_goods_info',
    'ods_staff_daily_report_huawei',
    'ods_staff_daily_report',
    'ods_merchant_daily_report_huawei',
    'ods_goods_order',
    'ods_goods_sub_order',
    'ods_goods_order_customer',
    'ods_merchant_deliver_target_config',
    'ods_merchant_output_target_config',
    'ods_employee_delivery_deal_rate',
]

# ODS表名 -> 源表名映射
SOURCE_MAP = {
    'ods_service_merchant': 'dj_car_life_service_merchant',
    'ods_shop_merchant_director_config': 'dj_car_life_shop_merchant_director_config',
    'ods_merchant_brand_model': 'dj_car_life_merchant_brand_model',
    'ods_staff_info': 'dj_car_life_staff_info',
    'ods_base_goods': 'dj_car_life_base_goods',
    'ods_goods_info': 'dj_car_life_goods_info',
    'ods_upgrade_goods': 'dj_car_life_upgrade_goods',
    'ods_merchant_goods_info': 'dj_car_life_merchant_goods_info',
    'ods_merchant_package_goods_info': 'dj_car_life_merchant_package_goods_info',
    'ods_staff_daily_report_huawei': 'dj_car_life_staff_daily_report_huawei',
    'ods_staff_daily_report': 'dj_car_life_staff_daily_report',
    'ods_merchant_daily_report_huawei': 'dj_car_life_merchant_daily_report_huawei',
    'ods_goods_order': 'dj_car_life_goods_order',
    'ods_goods_sub_order': 'dj_car_life_goods_sub_order',
    'ods_goods_order_customer': 'dj_car_life_goods_order_customer',
    'ods_merchant_deliver_target_config': 'dj_car_life_merchant_deliver_target_config',
    'ods_merchant_output_target_config': 'dj_car_life_merchant_output_target_config',
    'ods_employee_delivery_deal_rate': 'employee_delivery_deal_rate',
}

conn = pymysql.connect(**TARGET_CONFIG)

print(f"{'ODS表名':<45} {'max(updated_date)':<25} {'当前sync_time':<25}")
print("-" * 95)

for ods_table in ODS_TABLES:
    source_table = SOURCE_MAP[ods_table]

    # 获取ODS表中实际的 max(updated_date)
    with conn.cursor() as cur:
        cur.execute(f"SELECT MAX(updated_date) AS max_ud FROM `{ods_table}`")
        row = cur.fetchone()
        max_updated = row['max_ud'] if row else None

    # 获取当前 _sync_meta 中的 last_sync_time
    with conn.cursor() as cur:
        cur.execute(
            "SELECT last_sync_time FROM `_sync_meta` WHERE source_table=%s AND target_table=%s",
            (source_table, ods_table)
        )
        row = cur.fetchone()
        current_sync = row['last_sync_time'] if row else None

    print(f"{ods_table:<45} {str(max_updated):<25} {str(current_sync):<25}")

    # 更新 _sync_meta: 将 last_sync_time 设为实际的 max(updated_date)
    if max_updated:
        with conn.cursor() as cur:
            cur.execute("""
                INSERT INTO `_sync_meta` (source_table, target_table, last_sync_time, last_sync_rows, sync_mode)
                VALUES (%s, %s, %s, 0, 'incremental')
                ON DUPLICATE KEY UPDATE last_sync_time = VALUES(last_sync_time), sync_mode = 'incremental'
            """, (source_table, ods_table, max_updated))
        conn.commit()

print("\n_sync_meta 已修正为各表实际 max(updated_date)")
print("现在可以安全地跑增量同步了")

conn.close()