Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:173TECH Sayn CopyTask

From Leeroopedia
Knowledge Sources
Domains Data_Engineering, ETL
Last Updated 2026-10-17 00:00 GMT

Overview

Concrete tool for copying tables between databases with full, incremental, and append modes, including column mapping and batched merge processing provided by the SAYN framework.

Description

The CopyTask class (inherits from SqlTask) moves data between database connections. It:

  • Reads from a source database using SQLAlchemy introspection
  • Creates a destination table with validated DDL
  • Streams data from source to destination using server-side cursors
  • Supports three modes: full (replace), inc (merge by delete_key), append (add with timestamp)
  • Supports batched merge via max_merge_rows for large incremental loads
  • Supports column renaming via dst_name and type inference from source

Usage

  • Full copy: No incremental_key — replaces destination table entirely
  • Incremental merge: incremental_key + delete_key — only loads new records, merges by delete_key
  • Incremental append: incremental_key + append: true — loads new records with _sayn_load_ts timestamp
  • Batched merge: Set max_merge_rows for large incremental loads

Code Reference

Source Location

  • Repository: SAYN
  • File: sayn/tasks/copy.py
  • Lines: L15-66 (Source, Destination models), L69-109 (Config model), L112-691 (CopyTask class)

Signature

class Source(BaseModel):
    db_schema: Optional[str] = Field(None, alias="schema")
    table: str
    db: str
    db_name: Optional[str]

class Destination(BaseModel):
    tmp_schema: Optional[str]
    db_schema: Optional[str] = Field(None, alias="schema")
    table: str
    db: Optional[str]
    db_name: Optional[str]

class Config(BaseModel):
    source: Source
    destination: Destination
    delete_key: Optional[str]
    append: bool = False
    incremental_key: Optional[str]
    max_merge_rows: Optional[int]
    max_batch_rows: Optional[int]
    columns: Optional[List[Union[str, Dict[str, Any]]]] = list()
    table_properties: Optional[List[Dict[str, Any]]] = list()
    post_hook: Optional[List[Dict[str, Any]]] = list()

class CopyTask(SqlTask):
    def config(self, **config) -> Result
    def setup(self) -> Result
    def run(self) -> Result
    def compile(self) -> Result
    def test(self) -> Result
    def execute(self, execute: bool, debug: bool, is_full_load: bool, limit: int = None) -> Result[int]
    def get_columns(self) -> Result
    def get_read_query(self, execute: bool, debug: bool, is_full_load: bool, limit: int = None) -> Result[Select]

Import

from sayn.tasks.copy import CopyTask

I/O Contract

Inputs

Name Type Required Description
source Source Yes Source db/schema/table configuration
destination Destination Yes Destination db/schema/table configuration
delete_key str No (required for inc) Merge deduplication key
incremental_key str No Column for incremental filtering
append bool No Append mode (with incremental_key)
max_merge_rows int No Batch size for incremental merge
max_batch_rows int No Insert batch size
columns List No Column mapping with dst_name, type

Outputs

Name Type Description
records loaded int Number of records loaded (from execute)
destination table Database object Created, moved, or merged

Usage Examples

Full Copy

# tasks/copy.yaml
copy_users:
  type: copy
  source:
    table: users
    db: source_db
  destination:
    table: users
    schema: raw
    db: warehouse

Incremental Merge

copy_orders:
  type: copy
  source:
    table: orders
    db: source_db
  destination:
    table: orders
    db: warehouse
  incremental_key: updated_at
  delete_key: order_id
  max_merge_rows: 100000

Append Mode

copy_events:
  type: copy
  source:
    table: events
    db: source_db
  destination:
    table: events_log
    db: warehouse
  incremental_key: event_id
  append: true

With Column Mapping

copy_users:
  type: copy
  source:
    table: users
    db: source_db
  destination:
    table: users
    db: warehouse
  columns:
    - name: user_id
      type: INTEGER
      tests:
        - unique
        - not_null
    - name: email_address
      dst_name: email
      type: VARCHAR

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment