Skip to main content

Migrate Legacy SQL codebase to dbt : automaticaly generate Lineage.

Project description

dbt-migrator

Migrate legacy SQL scripts to dbt models with safe, lineage‑aware {{ ref(...) }} replacement.

Crates.io PyPI


Why dbt_migrator ?

In modern data platforms, lineage is the foundation of trust. dbt excels at providing column‑level and table‑level lineage – but only if all references are expressed via {{ ref(...) }}. Legacy SQL scripts often contain hard‑coded database/schema/table names, breaking lineage and making impact analysis, testing, and CI/CD impossible.

dbt_migrator bridges that gap:

  • Parses legacy SQL using a full SQL parser (sqlparser‑rs), not fragile regex.
  • Matches table references against your actual dbt model files (respecting schema, database, and tags inheritance from dbt_project.yml).
  • Rewrites them to {{ ref('model_name') }} only when an unambiguous match exists – conflicts are flagged, never silently resolved.
  • Works with any dialect – Snowflake, BigQuery, Redshift, SQL Server, DuckDB, and more (via sqlparser).
  • Delivers reports (JSON/CSV) of all replacements, warnings, and conflicts, so you can review changes before committing.

The result: clean, dbt‑native SQL with full lineage, enabling:

  • Reliable impact analysis (“Which dashboards break if I change this model?”)
  • Automated testing (data tests, freshness, etc.) with full dependency resolution.
  • CI/CD pipelines that only run models affected by a pull request.
  • Improved data quality through visibility and governance.

Key Features

  • 🧠 Smart resolution – three‑level priority (full db.schema.tableschema.table → bare table) with explicit conflict detection.
  • 🔍 Parses real SQL – uses sqlparser‑rs AST visitor, no fragile regex.
  • 📦 dbt‑aware – reads dbt_project.yml, traverses models: hierarchy, accumulates schema/database/tags per model directory.
  • ⚙️ Configurable – per‑table rules, output modes (overwrite or .migrated copy), thread control.
  • 📊 Rich reports – JSON for automation, CSV for human review.
  • 🐍 Python bindings – use it as a library in your dbt orchestration scripts (via PyO3/Maturin).
  • 🔀 Multithreaded – processes thousands of files in parallel (Rayon), with optional single‑thread mode for debugging.

Quick Start

Installation

  • Rust (cargo) :

    cargo install dbt_migrator
    
  • Python (uv / pip) :

    uv pip install dbt-migrator
    # or
    pip install dbt-migrator
    

Usage

  • CLI :

    dbt_migrator --project-dir /path/to/dbt_project \
        --sql-path /path/to/legacy_sql \
        --config migrate_config.yml \
        --report-dir ./reports
    
  • Library :

    # Python usage example
    from dbt_migrator import migrate_project
    
    report = migrate_project(
        project_dir="path/to/dbt_project",
        sql_path="path/to/legacy_sql",
        config_path="migrate_config.yml"
    )
    print(report.summary())
    

The Python API returns the same report data (replacements, warnings, conflicts) as the CLI, ready for integration into your CI/CD workflows.


Configuration

migrate_config.yml (example):

dialect: "snowflake"        # or "bigquery", "mssql", "duckdb", …
multithreading: true
output_mode: "overwrite"    # or "copy" (saves as .migrated)

conflicts:
  - script: "report_orders.sql"
    table: "dim_customers"
    target: "dim_customers_master"  # explicit override

normalization:
  # Optional regex for non‑standard table names (e.g., Dremio paths)
  regex: "(?P<schema>[^/]+)/(?P<table>[^/]+)"

All settings are optional; defaults are documented in the example config.


How It Works

  1. Parse dbt_project.yml – extract model paths and the models: tree.
  2. Build resolution index – walk every .sql file under model-paths, accumulate schema/database/tags from the models: hierarchy (exact dbt inheritance logic: deepest schema wins, tags union).
  3. Parse each legacy SQL file – using the chosen dialect.
  4. Visit every table reference – with sqlparser’s VisitorMut:
    • Try explicit rules from config.
    • Query the index (full → schema.table → table, with conflict detection).
    • If unambiguous, replace ObjectName with {{ ref('model') }} (AST‑level rewrite).
    • Otherwise, log a warning/conflict and keep the original.
  5. Write back (overwrite or side‑by‑side) and generate report.

⚠️ Important: The tool uses the dbt_project.yml alone (not manifest.json), so it’s a reasonable approximation of dbt’s final model names. If you have a manifest.json available, you can plug it into the index as a more accurate source.


Demo

A demo project is included. Four legacy SQL references are tested:

Legacy Reference Result
[analytics].[marts_finance].[fct_orders] {{ ref('fct_orders') }}
marts.dim_customers {{ ref('dim_customers') }}
dbo.legacy_unmapped_table kept (unmapped, warning)
dim_customers (ambiguous) kept (conflict reported)

Run the tests:

cargo test

Or using Python:

pytest tests/

Build from Source

Rust (CLI)

cargo build --release
target/release/dbt_migrator --help

Python Wheel

pip install maturin
maturin build --release --features python
# Wheel is in target/wheels/

For development:

maturin develop --features python

Limitations & Future Plans

  • No manifest.json support yet – using dbt_project.yml only. A future version will optionally read manifest.json for 100% accuracy.
  • Tree‑based fallback – disabled by default (spec’s “last resort”); can be enabled via config flag, but we recommend resolving conflicts manually.

Contributing

Issues and pull requests are welcome !


License

GPL-3.0-or-later


Give your dbt project the lineage it deserves. Try dbt_migrator today.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbt_migrator-0.2.1.tar.gz (37.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

dbt_migrator-0.2.1-cp38-abi3-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.8+Windows x86-64

dbt_migrator-0.2.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

dbt_migrator-0.2.1-cp38-abi3-macosx_11_0_arm64.whl (1.8 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file dbt_migrator-0.2.1.tar.gz.

File metadata

  • Download URL: dbt_migrator-0.2.1.tar.gz
  • Upload date:
  • Size: 37.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dbt_migrator-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fc208a56018e1e8252cb46337d90ebcabe5fec83e89f07d44604fafcd9113944
MD5 159273fa76effad04571614e6f397a9b
BLAKE2b-256 4fdaeb13c23517ccb3a517c2a500d4cdac916205a9f1aca2d12fe3cd098fb1c1

See more details on using hashes here.

File details

Details for the file dbt_migrator-0.2.1-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: dbt_migrator-0.2.1-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dbt_migrator-0.2.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 63ccf99d821487be45280d3d1fb95a5cceadd2c9c1a85bbd46f98c1edf77371a
MD5 d20d2fe9ef399fac6605fab7d594e359
BLAKE2b-256 e876d9f1dfcdcec57fc7473b3f3b90948566d6fbc46ae690e3825ad539f7118f

See more details on using hashes here.

File details

Details for the file dbt_migrator-0.2.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dbt_migrator-0.2.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5b93e5a4aff9c07027a737c576b7637f0d412a990c9a58a39a67db84c447a710
MD5 34a47958bad12316b0011465aee6f264
BLAKE2b-256 94d043c4807e5da93a0462c77eca17a590971b63e55c5b1098cec74cf970a8b3

See more details on using hashes here.

File details

Details for the file dbt_migrator-0.2.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dbt_migrator-0.2.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7eff2fc27390a2ef136a6867294d0f903bcb08d82629b7aacc11f0fe7194d07d
MD5 0b927f602861d7b50617647bb7fb00c0
BLAKE2b-256 31973f384fa5d33ac13511e8935154948ccd0e10455fe71c5e3f602fce714853

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page