Principle:173TECH Sayn Cross Database Copy
| 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:
- Introspect source:
source_db._get_table()retrieves SQLAlchemy Table definition - Determine columns: If not specified, infer from source (full load) or destination (incremental)
- Build read query: SQLAlchemy select with column renaming (
.label()), incremental filtering, ordering - Create target table:
create_table()with DDL - Stream load:
source_db._read_data_stream()→target_db.load_data()in batches - 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)
- Full:
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