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

Command‑line Interface

# Install from source (requires Rust)
cargo install dbt_migrator

# Or run directly
cargo run -- \
  --project-dir /path/to/dbt_project \
  --sql-path /path/to/legacy_sql \
  --config migrate_config.yml \
  --report-dir ./reports

Python Package (via PyO3)

pip install dbt-migrator
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.1.2.tar.gz (36.6 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.1.2-cp38-abi3-win_amd64.whl (971.8 kB view details)

Uploaded CPython 3.8+Windows x86-64

dbt_migrator-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

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

dbt_migrator-0.1.2-cp38-abi3-macosx_11_0_arm64.whl (976.9 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: dbt_migrator-0.1.2.tar.gz
  • Upload date:
  • Size: 36.6 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.1.2.tar.gz
Algorithm Hash digest
SHA256 8fed124950253e50186cec6cc900c233462c680de124b53964d3d3a632b760be
MD5 2baeffe5fc09fd21bbae949edf426796
BLAKE2b-256 70cc0483cf5681df0a0bab63b1d29b1e021cfa6f31cff5092aa99cdf25ad9887

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dbt_migrator-0.1.2-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 971.8 kB
  • 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.1.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 123134e59c1a4617b924aeaf51e4aeecf5efbc0f59c4d37a1ebfc8f882a873ab
MD5 337f8f4229122e79c88bd536e325a032
BLAKE2b-256 f3815db0fcbefbb3288653e1745e96d1ee5379625cf6d7fd6e3f9b0b3b76a5cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dbt_migrator-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 82df7697336b285acb41f1c57944b547e112e244073a19e0bab2425ce7f397f7
MD5 718c5749936da6d431c790f2b05847a2
BLAKE2b-256 582693edd69a3944654800d668bbdda90701fe4ec1a211b589ba3a407b73e2d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dbt_migrator-0.1.2-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3e0d0f1aaf75d3fa20d86f295d85c68ad078ad986ca3cfaa8c8fe9fb2f85b2ea
MD5 7eb202e0b1804af6a2f2895201074e74
BLAKE2b-256 2aa495079eed05415f64461308b0e5972044b31d92cb7eb4084503c05628f7b3

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