CoolFace
Apppublic

Harikantemt/sp-migrator

sourceHugging Faceupdated 2mo agoView on Hugging Face
1likes
App README

⚡ 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 .xlsx file; they are skipped automatically
  • —Cross-database FDW detection — detects 3-part (db.schema.table) references and generates a postgres_fdw setup 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.md

Requirements


Local Installation

bash
git clone <your-repo-url>
cd sp-migrator

pip install -r requirements.txt

python app.py
# → http://localhost:5000

# Custom port
python app.py 7860

HuggingFace Spaces Deployment

This app uses sdk: docker. Add a Dockerfile in your Space root:

dockerfile
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

SettingDefaultDescription
PG Version18Target PostgreSQL version
RPM limit12API requests per minute
Simple batch20SPs per API call (simple complexity)
Medium batch6SPs per API call (medium complexity)
Rolling download every N200Seal a chunk file every N OK procs

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

FileContents
ZIP (all)Everything below in one archive
OK Procs SQLAll status=ok procedures — safe to run
manual_review.sqlConverted code + original T-SQL for SPs needing manual fix
failed_conversions.sqlOriginal T-SQL for SPs that failed entirely
retry_fixed.sqlSPs fixed by the retry pass
retry_still_review.sqlSPs still needing review after retry
FDW Setuppostgres_fdw setup SQL for cross-database references
Download So FarMid-run ZIP of all converted procs so far
Errors CSVAll non-OK SPs with error and notes
Rolling chunksconverted_batch_001.sql etc. — sealed every N OK procs

Conversion Rules (summary)

  • —PROCEDURE vs FUNCTION — bare SELECT returning 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 DECLARE block
  • —T-SQL removal — SET NOCOUNT ON, WITH(NOLOCK), BEGIN TRAN, GO, USE all removed

License

Internal tool - Database Migration Team.