8.0 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
This is a data synchronization system (BLD_sync) that extracts data from:
- Microsoft Access databases (.accdb) located on network shares
- Excel files (.xlsm) containing production execution cards and contract data
And synchronizes it to:
- Microsoft SQL Server (CompanyDB) with multiple schemas organized by data type
The system supports both full initialization sync and incremental sync driven by a change log table.
Architecture
Data Flow
┌─────────────────────────────────────────────────────────────────────┐
│ Network File Sources │
│ Access DBs (\\192.168.110.114\生产进度表\) │
│ Excel Files (\\192.168.110.113\生产执行卡\) │
└─────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ Sync Scripts │
│ • etl_manager.py - Main ETL orchestrator (primary) │
│ • sync_excel_to_sql.py - Excel to SQL sync (legacy/alternative) │
│ • migration.py - Legacy customer product type migration │
│ • run_incremental_sync.py - Change log driven incremental sync │
│ • init_full_sync.py - Full table truncation/reload │
└─────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ SQL Server (CompanyDB) │
│ Schemas: productionContractData, productWarehousing, │
│ workshopOne/Two/Three, machining, contractPlanning, │
│ inspectionRecords, partsWarehouse, etc. │
└─────────────────────────────────────────────────────────────────────┘
Key Components
etl_manager.py - Primary ETL orchestrator
DataSynchronizerclass manages Excel to SQL sync- Supports incremental sync via file modification time comparison
- Caches Excel files locally in
excel_cache/directory - Uses SQLAlchemy with
fast_executemany=Truefor bulk operations - Generates
contractDatatable via MERGE statement fromexecutionCardData
config.py - Central configuration
SYNC_MAPPING: Nested dict mapping Access files → tables → SQL targetsEXCEL_CONFIGS: List of Excel file configs with sheet names and field mappingsTABLE_SCHEMA: Column type definitions for data cleaningNTFY_CONFIG: Push notification settingsLOG_TABLE_CONFIG: Change log table schema for incremental sync
db_utils.py - Database connection utilities
get_sql_conn(): SQL Server connection via pyodbcget_access_conn(): Access database connectionfmt_table(): Safe table name formatting[schema].[table]generate_insert_sql(): Dynamic INSERT statement generation
ntfy_utils.py - Push notifications
- Sends alerts to ntfy server on errors/completion
- Uses Bearer token authentication
run_incremental_sync.py - Log-driven incremental sync
- Polls
TableChangeLogtable for unsynced records (Synced=0) - Queries by
TableAddressto match configured file paths - Processes deletions and insertions in batches
- Long-running service with configurable polling interval
init_full_sync.py - Full table reload
- Truncates target tables and reloads all data from Access
- Handles IDENTITY_INSERT ON/OFF for tables with identity columns
- Progress logging with row counts and throughput metrics
Common Development Tasks
Install Dependencies
pip install -r requirements.txt
Run Full Initial Sync (Reload All Data)
python init_full_sync.py
Run Excel to SQL Sync (Standard)
python etl_manager.py
Force sync all files (ignore modification times):
python etl_manager.py --force
Run Incremental Sync Service (Change Log Driven)
python run_incremental_sync.py
Run Legacy Migration Script
python migration.py
Edit FORCE_UPDATE = True in migration.py to force full refresh.
Configuration Management
Main Config File: config.py
- Contains all database credentials, file paths, and mapping configurations
- Modify
SYNC_MAPPINGto add new Access files/tables - Modify
EXCEL_CONFIGSfor new Excel sources
Alternative Config: update_config.py
- Used by
sync_excel_to_sql.py - Similar structure but different variable names
- Contains
EXECUTION_CARD_FIELDS,CONTRACT_DATA_MAPPING
Important Implementation Details
SQL Server Connection
- Uses ODBC Driver 18 for SQL Server
- Requires
TrustServerCertificate=yesdue to self-signed cert - SQLAlchemy URL:
mssql+pyodbc:///?odbc_connect=... - Always enable
fast_executemany=Truefor bulk operations
Access Database Connection
- Driver:
{Microsoft Access Driver (*.mdb, *.accdb)} - Direct file path connection via pyodbc
Data Cleaning
- Integer fields:
pd.to_numeric().round().astype('Int64') - String fields: Truncate to max length, replace empty with None
- Date fields: Convert to
date()objects, None for NaT - Duplicate removal: Based on primary key (usually
IDor总排号)
Identity Column Handling
Tables with identity columns require:
SET IDENTITY_INSERT [schema].[table] ON
-- perform inserts
SET IDENTITY_INSERT [schema].[table] OFF
See has_identity_column() in init_full_sync.py for detection logic.
File Path Matching in Incremental Sync
The change log table stores paths in VBA format:
- Network:
;DATABASE=\\server\share\file.accdb - Local:
LOCAL:\path\to\file.accdborLOCAL=\path\to\file.accdb
The code constructs multiple match patterns for robust matching.
Database Schema Organization
SQL Server schemas by function:
warehouseOutbound- Execution card data, contract data, customer product typesproductionContractData- Contract data by year (25年/26年压力表/温度计)productWarehousing- Finished product inspection/warehousing recordsworkshopOne/Two/Three- Workshop production recordsmachining- Machining process records (膜片焊接, 喷涂寄出, etc.)contractPlanning- Work order assignment recordsinspectionRecords- Quality inspection recordspartsWarehouse- Component inventory recordsthermometerRecord- Thermometer calibration/testing recordssolderingData- Soldering operation recordsTIGWelding- TIG welding recordsexecutionCardIssuanceRecord- Execution card issuance records
Notifications
The system uses ntfy for push notifications:
- Configured in
NTFY_CONFIGwithinconfig.py - Sends on: errors, critical failures, task completion
- Authenticated via Bearer token
Logging
- File logs:
log/directory with timestamp rotation - Console output: With emoji prefixes for status (✅ ❌ ⚠️ 🔄)
- Incremental sync: Uses
TimedRotatingFileHandlerfor daily log files