feat: capture orchestration with include/exclude and delete-downgrade
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
42
src/sync/capture.py
Normal file
42
src/sync/capture.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from __future__ import annotations
|
||||
import json, logging
|
||||
from .access_reader import AccessReader
|
||||
from .sql_writer import SqlWriter, QueueRow
|
||||
from .config import FileMapping, SyncConfig
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def capture_file(fm: FileMapping, reader: AccessReader, writer: SqlWriter, cfg: SyncConfig) -> int:
|
||||
exclude = set(fm.exclude_tables or [])
|
||||
include = set(fm.include_tables) if fm.include_tables else None
|
||||
rows = reader.read_log(cfg.runtime.capture_batch_size)
|
||||
n = 0
|
||||
for lr in rows:
|
||||
if lr.table_name in exclude:
|
||||
continue
|
||||
if include is not None and lr.table_name not in include:
|
||||
continue
|
||||
op = lr.operate_type
|
||||
row_data = None
|
||||
if op in ("Insert", "Update"):
|
||||
d = reader.read_row(lr.table_name, lr.record_id)
|
||||
if d is None:
|
||||
op = "Delete" # 行已删,降级
|
||||
else:
|
||||
row_data = json.dumps(d, ensure_ascii=False)
|
||||
elif op != "Delete":
|
||||
log.warning("unknown OperateType %r in %s log %s", op, fm.file, lr.id)
|
||||
continue
|
||||
qr = QueueRow(
|
||||
source_file=fm.file,
|
||||
source_table=lr.table_name,
|
||||
record_id=lr.record_id,
|
||||
target_schema=fm.schema,
|
||||
target_table=fm.target_table(lr.table_name),
|
||||
source_log_id=lr.id,
|
||||
operate_type=op,
|
||||
row_data=row_data,
|
||||
)
|
||||
writer.insert_queue_row(qr)
|
||||
n += 1
|
||||
return n
|
||||
Reference in New Issue
Block a user