File size: 4,804 Bytes
12eff8e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | # MorphSQL β Project Context Document
> **Purpose:** Give this file to any AI assistant or new developer to continue work without prior conversation context.
## Naming
| Layer | Name |
|-------|------|
| **Product (UI, docs, Hugging Face Space title)** | **MorphSQL** |
| Python import | `morphsql` |
| PyPI package | `morphsql` |
| GitHub repo | `morphsql` |
| HF Space / model slug | `dgvj-work/morphsql` |
Always use **MorphSQL** / `morphsql` consistently (product, package, CLI, Hub).
## What this project is
**MorphSQL** is an open-source **SQL migration toolkit** (package import: `morphsql`).
It is **not** only a single-query converter. It analyzes legacy SQL repositories and produces:
1. **Discovery** β scan zip files/directories for SQL, procedures, views, dbt models
2. **Lineage** β dependency graphs (table/object level)
3. **Risk assessment** β complexity scoring, unsupported syntax, downstream impact
4. **Workload rationalization** β migrate / review / rewrite / retire per object
5. **Hybrid translation** β rule-based + sqlglot dialect conversion (pandas / PySpark / warehouse / dbt)
6. **dbt architecture** β decompose procedures into staging/intermediate/mart
7. **Validation** β reconciliation tests (row count, null rate, checksums)
8. **Migration runbook** β phased cutover plan with object action table
9. **Migration copilot** β LLM advisor grounded in scan context (HF Inference API)
## Repository layout
```
morphsql/
βββ app.py # Hugging Face Gradio Space (MorphSQL UI)
βββ demo/
β βββ handlers.py # Gradio event handlers
β βββ theme.py # Dark theme CSS/constants
βββ morphsql/ # Core Python package
β βββ scanner/ # Repository discovery
β βββ parser/ # SQL parsing (sqlglot)
β βββ lineage/ # Dependency graphs (networkx)
β βββ translator/ # Hybrid SQL conversion (+ pandas/pyspark codegen)
β βββ risk/ # Complexity & risk scoring
β βββ dbt_generator/ # dbt project decomposition
β βββ validation/ # Reconciliation tests
β βββ knowledge/ # Platform behavior differences KB
β βββ intelligence/ # Runbook, rationalization, lineage viz
β βββ assistant/ # LLM copilot (huggingface_hub)
β βββ report/ # HTML report generation
β βββ pipeline.py # Agent orchestration
β βββ models.py # Pydantic data models
β βββ cli.py # `morphsql` CLI
βββ examples/vertica_legacy/ # Sample legacy Vertica repository
βββ tests/
βββ PROJECT.md # This file
βββ README.md
βββ README_HF_SPACE.md # HF Space card (title: MorphSQL)
βββ MODEL_CARD.md
βββ pyproject.toml
```
## Architecture
```
Repository (zip / directory)
β
MigrationPipeline.analyze() β Discovery + Lineage + Risk
β
MigrationPipeline.convert() β Hybrid SQL translation
β
MigrationPipeline.validate() β Reconciliation tests
β
generate_runbook() β Cutover plan
generate_rationalization() β Migrate/retire recommendations
decompose_to_dbt() β dbt scaffold
MigrationCopilot.respond() β LLM Q&A grounded in report
```
## Key modules
| Module | File | Purpose |
|--------|------|---------|
| Pipeline | `morphsql/pipeline.py` | Orchestrates full workflow |
| Translator | `morphsql/translator/engine.py` | ZEROIFNULL, DATEADD, procedure wrappers, sqlglot |
| Pandas / PySpark | `morphsql/translator/*_codegen.py` | Notebook / Spark DataFrame codegen |
| Copilot | `morphsql/assistant/copilot.py` | HF Inference API + fallback KB |
| Runbook | `morphsql/intelligence/runbook.py` | Migration runbook markdown |
| Lineage viz | `morphsql/intelligence/lineage_viz.py` | Plotly network graph |
| Rationalization | `morphsql/intelligence/rationalization.py` | Workload action plan |
## CLI commands
```bash
morphsql analyze PATH -s vertica -t snowflake
morphsql convert PATH -s snowflake -t pandas
morphsql migrate PATH -s vertica -t snowflake -o migration-output
morphsql version
```
## Hugging Face
- Space title: **MorphSQL**
- Space URL: https://huggingface.co/spaces/dgvj-work/morphsql
- Deploy: `./scripts/deploy_hf.sh`
- Preflight: `python scripts/check_space.py`
- Copilot model env: `MORPHSQL_MODEL`
## Gradio UI tabs
1. **Convert** β primary: paste/upload SQL β pandas/PySpark/SQL/dbt + preview + download
2. **Guide** β how-to for DS / AI users
3. **More** β object assess, repo workbench, feature SQL, copilot, RAG, eval
|