|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import uuid |
| 5 | + |
| 6 | +import typer |
| 7 | + |
| 8 | +from sqlcompare.config import get_default_schema |
| 9 | +from sqlcompare.db import DBConnection |
| 10 | +from sqlcompare.helpers import create_table_from_select, detect_input, ensure_schema |
| 11 | +from sqlcompare.table import compare_table |
| 12 | + |
| 13 | + |
| 14 | +def _resolve_connection(connection: str | None) -> str: |
| 15 | + if connection: |
| 16 | + return connection |
| 17 | + default_conn = os.getenv("SQLCOMPARE_CONN_DEFAULT") or os.getenv("DTK_CONN_DEFAULT") |
| 18 | + if not default_conn: |
| 19 | + raise typer.BadParameter( |
| 20 | + "No connection specified. Use --connection or set SQLCOMPARE_CONN_DEFAULT." |
| 21 | + ) |
| 22 | + return default_conn |
| 23 | + |
| 24 | + |
| 25 | +def run_cmd( |
| 26 | + table1: str = typer.Argument( |
| 27 | + ..., help="Previous table name, CSV/XLSX file path, or SQL" |
| 28 | + ), |
| 29 | + table2: str = typer.Argument( |
| 30 | + ..., help="Current table name, CSV/XLSX file path, or SQL" |
| 31 | + ), |
| 32 | + index: str = typer.Argument( |
| 33 | + ..., help="Comma-separated key column(s), e.g. 'id' or 'user_id,tenant_id'" |
| 34 | + ), |
| 35 | + connection: str | None = typer.Option( |
| 36 | + None, "--connection", "-c", help="Database connector name" |
| 37 | + ), |
| 38 | + schema: str | None = typer.Option(None, "--schema", help="Schema for test tables"), |
| 39 | + columns: str | None = typer.Option( |
| 40 | + None, |
| 41 | + "--columns", |
| 42 | + help="Comma-separated non-index columns to compare (default: all common columns)", |
| 43 | + ), |
| 44 | + ignore_columns: str | None = typer.Option( |
| 45 | + None, |
| 46 | + "--ignore-columns", |
| 47 | + help="Comma-separated non-index columns to skip from comparison", |
| 48 | + ), |
| 49 | +) -> None: |
| 50 | + """Run a comparison from tables, files, or SQL text. |
| 51 | +
|
| 52 | + This command auto-detects inputs: |
| 53 | + - CSV/XLSX paths are treated as files |
| 54 | + - .sql paths are read as SQL text |
| 55 | + - Inline SQL starting with SELECT/WITH is treated as SQL |
| 56 | + - Otherwise, inputs are treated as table names |
| 57 | +
|
| 58 | + Examples: |
| 59 | + # Tables |
| 60 | + sqlcompare run analytics.users analytics.users_new id |
| 61 | +
|
| 62 | + # SQL inline |
| 63 | + sqlcompare run "SELECT * FROM previous" "SELECT * FROM current" id -c duckdb_test |
| 64 | +
|
| 65 | + # SQL files |
| 66 | + sqlcompare run queries/previous.sql queries/current.sql id -c snowflake_prod |
| 67 | +
|
| 68 | + # Files (uses DuckDB) |
| 69 | + sqlcompare run exports/prev.csv exports/current.csv customer_id |
| 70 | + """ |
| 71 | + schema = schema or get_default_schema() |
| 72 | + |
| 73 | + prev_spec = detect_input(table1) |
| 74 | + new_spec = detect_input(table2) |
| 75 | + |
| 76 | + if prev_spec.kind == "file" or new_spec.kind == "file": |
| 77 | + if prev_spec.kind != "file" or new_spec.kind != "file": |
| 78 | + raise typer.BadParameter( |
| 79 | + "Both table arguments must be file paths when using CSV/XLSX inputs." |
| 80 | + ) |
| 81 | + compare_table( |
| 82 | + prev_spec.value, |
| 83 | + new_spec.value, |
| 84 | + index, |
| 85 | + connection, |
| 86 | + schema, |
| 87 | + include_columns=columns, |
| 88 | + ignore_columns=ignore_columns, |
| 89 | + ) |
| 90 | + return |
| 91 | + |
| 92 | + if prev_spec.kind == "sql" or new_spec.kind == "sql": |
| 93 | + connection = _resolve_connection(connection) |
| 94 | + schema_prefix = f"{schema}." if schema else "" |
| 95 | + suffix = uuid.uuid4().hex[:8] |
| 96 | + previous_table = ( |
| 97 | + prev_spec.value |
| 98 | + if prev_spec.kind == "table" |
| 99 | + else f"{schema_prefix}sqlcompare_sql_{suffix}_previous" |
| 100 | + ) |
| 101 | + new_table = ( |
| 102 | + new_spec.value |
| 103 | + if new_spec.kind == "table" |
| 104 | + else f"{schema_prefix}sqlcompare_sql_{suffix}_new" |
| 105 | + ) |
| 106 | + |
| 107 | + with DBConnection(connection) as db: |
| 108 | + ensure_schema(db, schema) |
| 109 | + if prev_spec.kind == "sql": |
| 110 | + create_table_from_select(db, previous_table, prev_spec.value) |
| 111 | + if new_spec.kind == "sql": |
| 112 | + create_table_from_select(db, new_table, new_spec.value) |
| 113 | + |
| 114 | + compare_table( |
| 115 | + previous_table, |
| 116 | + new_table, |
| 117 | + index, |
| 118 | + connection, |
| 119 | + schema, |
| 120 | + include_columns=columns, |
| 121 | + ignore_columns=ignore_columns, |
| 122 | + ) |
| 123 | + return |
| 124 | + |
| 125 | + compare_table( |
| 126 | + table1, |
| 127 | + table2, |
| 128 | + index, |
| 129 | + connection, |
| 130 | + schema, |
| 131 | + include_columns=columns, |
| 132 | + ignore_columns=ignore_columns, |
| 133 | + ) |
0 commit comments