# oleg-agapov/tablediff architecture

> Architecture diagram of the GitHub repository oleg-agapov/tablediff, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/oleg-agapov/tablediff
- Repository: https://github.com/oleg-agapov/tablediff

## Overview

##### Project type & purpose (what you’re diagramming)
`tablediff` is a **Python CLI tool** that performs **data diffs and schema diffs** between:
- two tables in the same DB,
- tables across two different DBs (`--conn` + `--conn2`),
- two CSV files (`tablediff files`).

It delegates the heavy lifting of cross-database connectivity/diffing to **`reladiff`** (per README), and focuses on:
- CLI UX (commands/options),
- orchestration (“engine”),
- domain models (diff results),
- rendering results to terminal (and possibly a UI via `start-ui.py`).

- user interaction layer (CLI),
- orchestration layer (engine),
- integration layer (reladiff + DB adapters),
- sources/targets (DBs/CSV),
- output rendering.

##### How to read the repo structure into architectural components
From the file tree, the “real system” lives in `tablediff/`:

- `tablediff/cli.py`
**Entry point / command definitions**: parses args, maps subcommands (`compare`, `schema`, `files`) to engine functions, validates options (`--pk`, `--conn`, `--conn2`, `--where`, `--extended`).

- `tablediff/engine.py`
**Core orchestration**: builds connections, configures reladiff, executes diff operations, handles cross-db logic, applies `--where`, picks correct workflow for schema-vs-data-vs-files.

- `tablediff/models.py`

- `tablediff/renderers.py`
**Presentation layer**: converts models/results into terminal output (tables, summaries, extended sections). This is the output boundary.

- `tablediff/__init__.py`
Package metadata; not a component.

- `pyproject.toml`, `uv.lock` → packaging, dependency management, extras for adapters (`[snowflake]`, `[postgresql]`, `[all]`).
- `scripts/generate_duckdb_test_data.py` (+ `.sql`) → dev tooling to create sample data.
- `tests/…` → validates behavior across CLI, engine, CSV, cross-database, and reladiff integration.
- `start-ui.py` + `img/demo.cast` → indicates there may be an additional UI runner (even if it’s just a demo wrapper). Include it as an *optional entrypoint* if it’s actually used.

##### `tablediff compare TABLE_A TABLE_B --pk … --conn … [--conn2 …] [--where …] [--extended]`
**Flow (high level):**
##### Engine → reladiff
- instantiate adapters/drivers based on connection strings
- perform row-level diff keyed by `--pk`
- apply optional `--where` filter on both sides
##### CLI → Renderer prints results to terminal

**Cross-db** variant adds: Engine creates **two** connections (possibly different adapter types).

##### `tablediff schema TABLE_A TABLE_B --conn … [--conn2 …]`
Similar to compare, but the engine invokes **schema inspection** path and yields schema diff models (including types, per README).

##### `tablediff files FILE_A FILE_B --pk …`
This is a separate source type:
##### Results → Renderer

You want these three flows explicit, because they are the “product surface area”.

##### External dependencies & boundaries (must be
- **reladiff library** (core diff engine and DB abstraction)
- **Database systems** (DuckDB, Snowflake, Postgres, etc.)
- **DB drivers/adapters** enabled by install extras (e.g., `tablediff-cli[snowflake]`)
- **File system** for CSV inputs and DuckDB local file

In other words: `tablediff` is an orchestrator + UX wrapper around reladiff + adapters.

Purpose: show who uses it and what it talks to.
Include:
- Actor: “Data engineer / analyst”
- System: “tablediff CLI tool”
- External: “Databases (DuckDB/Snowflake/…)”
- External: “CSV files (filesystem)”
- External: “reladiff library” (as a dependency boundary)

- User → tablediff: invokes commands
- tablediff → DBs: connect/query/inspect schema
- tablediff → filesystem: read CSV

Purpose: show major runtime modules and responsibilities.
- **CLI Layer** (`tablediff/cli.py`)
- **Engine / Orchestration** (`tablediff/engine.py`)
- **Domain Models** (`tablediff/models.py`)
- **Renderers** (`tablediff/renderers.py`)
- **Integration: reladiff** (external dependency, but show it adjacent)
- **Connectors/Adapters** (as part of reladiff + installed extras)
- **Data Sources** (DB A, DB B, CSV A, CSV B)

- CLI → Engine (command invocation)
- Engine ↔ reladiff (diff computation + schema inspection)
- Engine → Models (construct/return results)
- CLI/Engine → Renderer (format output)
- Renderer → Terminal (stdout)

- `--pk` affects diff key
- `--where` affects query/filter
- `--extended` affects renderer verbosity/model detail
- `--conn`/`--conn2` decide whether one or two DB endpoints are created

Purpose: show internal responsibilities to help future changes.
Decompose like this (even if some are functions, it helps):
- In `cli.py`:
- Command routing: `compare`, `schema`, `files`
- Option parsing/validation
- Error handling/user messaging
- In `engine.py`:
- Connection factory (from conn strings)
- Diff job builder (table identifiers, pk, where)
- Execution paths:
- data diff path
- schema diff path
- CSV diff path
- Result normalization into models
- In `renderers.py`:
- Summary renderer
- Extended renderer sections
- Schema diff renderer

Then link each component to the relevant external interaction (DB/CSV/reladiff).

## Diagram (Mermaid)

```mermaid
flowchart TD

%% =========================================================
%% Diagram 1 — System Context (vertical)
%% =========================================================
subgraph "Diagram 1 — System Context"
direction TB

U["Data engineer / analyst"]:::actor

TDCLI_CTX["tablediff CLI tool"]:::system
TERM["Terminal (stdout report)"]:::external
REL_CTX["reladiff (external library)"]:::external
DBS["Databases (DuckDB / Snowflake / Postgres / ...)"]:::external
FS["Filesystem (CSV files, DuckDB local file)"]:::external

U -->|"invokesCommands(compare/schema/files)"| TDCLI_CTX
TDCLI_CTX -->|"printsDiffReport"| TERM
TDCLI_CTX -->|"delegatesDiffAndSchemaInspection"| REL_CTX
REL_CTX -->|"connectsQueriesInspects"| DBS
TDCLI_CTX -->|"readsCSVInputs(files)"| FS
end

%% =========================================================
%% Diagram 2 — Container / Runtime Building Blocks (vertical)
%% =========================================================
subgraph "Diagram 2 — Runtime Building Blocks (inside tablediff)"
direction TB

subgraph "Process lifecycle (single-run CLI invocation)"
direction TB
PROC["Invocation lifecycle\nstart → parse args → run diff → render → exit"]:::note
end

subgraph "tablediff package"
direction TB

CLI["CLI Layer\n(tablediff/cli.py)"]:::internal
ENGINE["Engine / Orchestration\n(tablediff/engine.py)"]:::internal
MODELS["Domain Models\n(tablediff/models.py)"]:::internal
RENDER["Renderers (terminal output)\n(tablediff/renderers.py)"]:::internal
end

REL["reladiff (dependency)"]:::external

subgraph "Connectivity / Adapters (activated via install extras)"
direction TB
ADAPTERS["DB adapters/drivers\n(snowflake/postgresql/all extras)"]:::external
end

subgraph "Data sources / targets"
direction TB
DBA["DB Endpoint A"]:::external
DBB["DB Endpoint B (optional: --conn2)"]:::external
CSVA["CSV File A"]:::external
CSVB["CSV File B"]:::external
end

OUT["Terminal / Stdout"]:::external

CLI -->|"routesCommand(compare/schema/files)"| ENGINE
ENGINE -->|"constructsResults"| MODELS
MODELS -->|"providesStructuredDiffTypes"| RENDER
CLI -->|"invokesRenderer"| RENDER
RENDER -->|"writesOutput"| OUT

ENGINE -->|"configuresAndCalls"| REL
REL -->|"uses"| ADAPTERS

ADAPTERS -->|"connectsTo"| DBA
ADAPTERS -->|"connectsTo(optional)"| DBB

ENGINE -->|"readsFiles(files)"| CSVA
ENGINE -->|"readsFiles(files)"| CSVB

%% runtime options as notes on key edges
CLI -.->|"--pk/--where/--extended/--conn/--conn2"| ENGINE
ENGINE -.->|"--pkKeying/--wherePropagation"| REL
RENDER -.->|"--extendedControlsVerbosity"| OUT
end

%% =========================================================
%% Diagram 3 — Component-level (inside CLI + Engine + Renderers)
%% =========================================================
subgraph "Diagram 3 — Component-level responsibilities"
direction TB

subgraph "CLI internals (tablediff/cli.py)"
direction TB
CLI_PARSE["Option parsing & validation\n(--pk, --conn, --conn2, --where, --extended)"]:::internal
CLI_ROUTE["Command routing\ncompare | schema | files"]:::internal
CLI_ERR["Error handling & user messaging"]:::internal
end

subgraph "Engine internals (tablediff/engine.py)"
direction TB
ENG_CONN["Connection factory\n(from connection strings)"]:::internal
ENG_NORM["Identifier normalization\n(db.schema.table)"]:::internal
ENG_JOB["Diff job builder\n(pk, where, endpoints)"]:::internal
subgraph "Execution paths"
direction TB
ENG_DATA["Data diff path\n(tables: compare)"]:::internal
ENG_SCHEMA["Schema diff path\n(schema command)"]:::internal
ENG_CSV["CSV diff path\n(files command)"]:::internal
end
ENG_WHERE["WHERE clause propagation\n(apply to both sides)"]:::internal
ENG_XDB["Cross-DB orchestration\n(dual connections when --conn2)"]:::internal
ENG_RES["Result normalization\n→ models"]:::internal
end

subgraph "Renderer internals (tablediff/renderers.py)"
direction TB
REN_SUM["Summary renderer\n(counts, high-level)"]:::internal
REN_EXT["Extended sections renderer\n(--extended details)"]:::internal
REN_SCHEMA["Schema diff renderer\n(types/changes)"]:::internal
end

%% Wiring
CLI_ROUTE -->|"dispatches"| CLI_PARSE
CLI_PARSE -->|"buildsInvocationConfig"| ENG_JOB
CLI_ERR -.->|"reportsUsageOrFailures"| OUT

ENG_JOB -->|"needs"| ENG_CONN
ENG_JOB -->|"needs"| ENG_NORM
ENG_JOB -->|"applies"| ENG_WHERE
ENG_JOB -->|"selectsWorkflow"| ENG_DATA
ENG_JOB -->|"selectsWorkflow"| ENG_SCHEMA
ENG_JOB -->|"selectsWorkflow"| ENG_CSV
ENG_XDB -->|"enabledBy(--conn2)"| ENG_CONN

ENG_DATA -->|"callsReladiffRowDiff"| REL
ENG_SCHEMA -->|"callsReladiffSchemaInspect"| REL
ENG_CSV -->|"readsCSVAndDiffs"| FS
ENG_DATA -->|"normalizes"| ENG_RES
ENG_SCHEMA -->|"normalizes"| ENG_RES
ENG_CSV -->|"normalizes"| ENG_RES
ENG_RES -->|"creates"| MODELS

MODELS -->|"renderAsSummary"| REN_SUM
MODELS -->|"renderAsExtended"| REN_EXT
MODELS -->|"renderAsSchema"| REN_SCHEMA
REN_SUM -->|"stdout"| OUT
REN_EXT -->|"stdout"| OUT
REN_SCHEMA -->|"stdout"| OUT
end

%% =========================================================
%% Supporting subsystems (packaging, tooling, tests, CI)
%% =========================================================
subgraph "Supporting subsystems"
direction TB

subgraph "Configuration / Packaging"
direction TB
PYPROJ["Packaging config & extras\n(pyproject.toml)"]:::support
UVLOCK["Dependency lockfile\n(uv.lock)"]:::support
end

subgraph "Dev tooling"
direction TB
GENPY["DuckDB sample data generator\n(scripts/generate_duckdb_test_data.py)"]:::support
GENSQL["DuckDB sample data SQL\n(scripts/generate_duckdb_test_data.sql)"]:::support
DEMO["Demo cast recording\n(img/demo.cast)"]:::support
end

subgraph "Optional UI entrypoint"
direction TB
UI["start-ui.py (optional runner)"]:::support
end

subgraph "Test harness"
direction TB
TESTS["tests/ (suite)"]:::support
TCONF["tests/conftest.py (fixtures)"]:::support
TCLI["tests/test_cli.py"]:::support
TENG["tests/test_engine.py"]:::support
TXDB["tests/test_cross_database.py"]:::support
TCSV["tests/test_csv.py"]:::support
TREL["tests/test_reladiff_integration.py"]:::support
end

subgraph "CI/CD"
direction TB
RELWF["Release workflow\n(.github/workflows/release.yml)"]:::support
end

%% Relationships
PYPROJ -->|"declaresExtrasEnableAdapters"| ADAPTERS
UVLOCK -->|"pinsDependencies"| REL

GENPY -->|"createsSampleDataFor"| DBA
GENSQL -->|"usedBy"| GENPY

UI -->|"invokesSameEnginePaths"| ENGINE
DEMO -.->|"documentsUX"| UI

TESTS -.->|"validates"| CLI
TESTS -.->|"validates"| ENGINE
TCONF -.->|"providesFixturesFor"| TESTS
TCLI -.->|"covers"| CLI
TENG -.->|"covers"| ENGINE
TXDB -.->|"coversCrossDB(--conn2)"| ENGINE
TCSV -.->|"coversFilesCommand"| ENGINE
TREL -.->|"coversIntegration"| REL

RELWF -.->|"buildTestRelease"| PYPROJ
RELWF -.->|"runs"| TESTS
end

%% =========================================================
%% Click events (from component_mapping)
%% =========================================================
click CLI "https://github.com/oleg-agapov/tablediff/blob/main/tablediff/cli.py"
click ENGINE "https://github.com/oleg-agapov/tablediff/blob/main/tablediff/engine.py"
click MODELS "https://github.com/oleg-agapov/tablediff/blob/main/tablediff/models.py"
click RENDER "https://github.com/oleg-agapov/tablediff/blob/main/tablediff/renderers.py"
click UI "https://github.com/oleg-agapov/tablediff/blob/main/start-ui.py"
click PYPROJ "https://github.com/oleg-agapov/tablediff/blob/main/pyproject.toml"
click UVLOCK "https://github.com/oleg-agapov/tablediff/blob/main/uv.lock"
click GENPY "https://github.com/oleg-agapov/tablediff/blob/main/scripts/generate_duckdb_test_data.py"
click GENSQL "https://github.com/oleg-agapov/tablediff/blob/main/scripts/generate_duckdb_test_data.sql"
click DEMO "https://github.com/oleg-agapov/tablediff/blob/main/img/demo.cast"
click TESTS "https://github.com/oleg-agapov/tablediff/tree/main/tests/"
click TCLI "https://github.com/oleg-agapov/tablediff/blob/main/tests/test_cli.py"
click TXDB "https://github.com/oleg-agapov/tablediff/blob/main/tests/test_cross_database.py"
click TCSV "https://github.com/oleg-agapov/tablediff/blob/main/tests/test_csv.py"
click TENG "https://github.com/oleg-agapov/tablediff/blob/main/tests/test_engine.py"
click TREL "https://github.com/oleg-agapov/tablediff/blob/main/tests/test_reladiff_integration.py"
click TCONF "https://github.com/oleg-agapov/tablediff/blob/main/tests/conftest.py"
click RELWF "https://github.com/oleg-agapov/tablediff/blob/main/.github/workflows/release.yml"

%% =========================================================
%% Styles (color required)
%% =========================================================
classDef actor fill:#FFE08A,stroke:#8A6D1D,color:#1F1F1F,stroke-width:1.5px
classDef system fill:#8EC5FF,stroke:#1E4E8C,color:#0B1B2B,stroke-width:1.8px
classDef internal fill:#BFE7C6,stroke:#1E6B3A,color:#0E2A18,stroke-width:1.5px
classDef external fill:#FFD2D2,stroke:#9B2C2C,color:#3A0D0D,stroke-width:1.5px
classDef support fill:#E6E6E6,stroke:#6B7280,color:#111827,stroke-width:1.2px,stroke-dasharray: 4 3
classDef note fill:#FFF7CC,stroke:#A78B00,color:#3B2F00,stroke-width:1px,stroke-dasharray: 3 3
```
