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