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

Principle:173TECH Sayn Cross Database Copy

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

Overview

Technique for copying tables between heterogeneous databases with column mapping, type conversion, and configurable load modes (full, incremental, append).

Description

Cross-database copy solves the problem of moving data between different database systems (e.g., PostgreSQL to Snowflake, SQLite to BigQuery). The copy task:

  • Reads from a source database using SQLAlchemy introspection
  • Maps columns with optional renaming (dst_name) and type inference
  • Creates a destination table (temp or final) with appropriate DDL
  • Streams data from source to destination in batches
  • Finalizes with move (full), merge (incremental), or append

Key concepts:

  • Source/Destination models: Separate configurations for source and target, each with db, schema, table
  • Column mapping: Source columns can be renamed via dst_name, types inferred from source or specified
  • Mode determination: full (no incremental_key), inc (incremental_key + delete_key), append (incremental_key + append)
  • Streaming: Uses server-side cursor (_read_data_stream) for memory-efficient transfer

Usage

Use this principle when data must be moved between different database systems. It is the right choice for ingestion pipelines, data replication, and cross-platform data movement.

Theoretical Basis

The copy execution pipeline:

  1. Introspect source: source_db._get_table() retrieves SQLAlchemy Table definition
  2. Determine columns: If not specified, infer from source (full load) or destination (incremental)
  3. Build read query: SQLAlchemy select with column renaming (.label()), incremental filtering, ordering
  4. Create target table: create_table() with DDL
  5. Stream load: source_db._read_data_stream() → target_db.load_data() in batches
  6. Finalize:
    • Full: move_table() (drop target, rename temp)
    • Incremental: merge_tables() (delete by delete_key, insert new)
    • Append: No finalization (records already inserted with _sayn_load_ts)

Practical Guide

# tasks/copy.yaml
copy_users:
  type: copy
  source:
    table: users
    db: source_db
  destination:
    table: users
    schema: raw
    db: warehouse
  columns:
    - name: user_id
      type: INTEGER
      tests:
        - unique
        - not_null
    - name: email
      dst_name: email_address
      type: VARCHAR

Related Pages

Implemented By

Page Connections

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