Harikantemt/sp-migrator
⚡ SP Migrator
MSSQL → PostgreSQL stored procedure conversion tool powered by LLMs.
Batch-converts T-SQL stored procedures to PL/pgSQL using Google Gemini Flash or Mistral Codestral, with live streaming progress, rolling downloads, and a static validator.
Features
- Batch conversion — groups simple/medium/complex SPs into optimal API batches
- Two LLM providers — Gemini 2.0 Flash (Google) or Codestral (Mistral)
- Live SSE streaming — real-time log, per-SP status, progress bar
- Static validator — post-conversion checks for T-SQL remnants, dbo. prefixes, bare SELECTs in PROCEDUREs, shape mismatches, nested DECLAREs, etc.
- Rolling batch downloads — download OK procs in chunks without waiting for full run
- Resume / Stop — stop mid-run and resume from where you left off (same session)
- Manual retry — re-attempt failed and manual_review SPs with a targeted prompt
- Excel skip list — mark unused procs in an
.xlsxfile; they are skipped automatically - Cross-database FDW detection — detects 3-part (
db.schema.table) references and generates apostgres_fdwsetup script - Download So Far — mid-run ZIP download of all converted procs
- Separate output files —
all_procedures.sql,manual_review.sql,failed_conversions.sql,retry_fixed.sql,errors.csv,fdw_setup.sql
Project Structure
sp-migrator/
├── app.py # Flask backend — parsing, LLM calls, SSE streaming, downloads
├── templates/
│ └── index.html # Single-page frontend — all UI, JS, SSE handling
├── requirements.txt
└── README.mdRequirements
- Python 3.10+
- API key for one of:
- Google Gemini → aistudio.google.com/app/apikey
- Mistral Codestral → console.mistral.ai/api-keys
Local Installation
git clone <your-repo-url>
cd sp-migrator
pip install -r requirements.txt
python app.py
# → http://localhost:5000
# Custom port
python app.py 7860HuggingFace Spaces Deployment
This app uses sdk: docker. Add a Dockerfile in your Space root:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 7860
CMD ["python", "app.py", "7860"]HF Spaces will auto-build and serve on port 7860.
Usage
1. Upload SQL File
Drop your .sql file containing MSSQL stored procedures separated by GO statements.
2. Skip List (optional)
Upload an .xlsx file listing procedure names to skip (unused procs). The tool auto-detects the name column.
3. API Key
Paste your Gemini or Codestral API key. Use the 👁 button to show/hide.
4. Settings
5. Start / Stop / Resume
- ▶ Start Conversion — fresh run, resets all state
- ■ Stop — stops after current batch; session preserved
- ▶▶ Resume Conversion — continues from where it stopped (pending SPs only)
6. Retry
After conversion, if any SPs are manual_review or failed, the 🔄 Retry button appears. It re-attempts them with a targeted fix prompt.
Downloads
Conversion Rules (summary)
- PROCEDURE vs FUNCTION — bare
SELECTreturning rows →FUNCTION + RETURNS TABLE + RETURN QUERY; DML only →PROCEDURE - Naming — strict lowercase,
p_prefix for params,v_for local vars, no new underscores - No dbo. — all schema prefixes stripped from local objects
- Type mapping —
NVARCHAR→VARCHAR,DATETIME→TIMESTAMPTZ,BIT→BOOLEAN,MONEY→NUMERIC(19,4), etc. - Multiple result sets — split into separate functions (one per result set)
- Cross-DB refs — mapped to FDW schema (
fdw_<dbname>.<table>) - Single DECLARE — all variables hoisted to one top-level
DECLAREblock - T-SQL removal —
SET NOCOUNT ON,WITH(NOLOCK),BEGIN TRAN,GO,USEall removed
License
Internal tool - Database Migration Team.
