Rewrite Ruby on Rails Applications to Python (FastAPI or Django) with the LEAP Protocol

TL;DR — To rewrite a Rails codebase to Python without a big-bang rewrite: 1) freeze current behavior as contracts (OpenAPI, DB invariants, job/mail semantics), 2) auto-generate a pytest parity suite from your running Rails app, 3) re-implement endpoints, models, jobs in Python (FastAPI+SQLAlchemy or Django+DRF), 4) run Rails and Python in parallel, mirror (shadow) traffic, and cut over gradually. Quick-start commands you will use: rails routes and rails db:schema:dump (Rails Guides), django-admin startproject + python manage.py startapp and python manage.py inspectdb > models.py (Django), or pip install fastapi uvicorn sqlalchemy alembic pydantic celery && alembic init alembic && uvicorn app:app --reload (FastAPI/SQLAlchemy). Do not run both frameworks’ DB migrations on the same database at the same time; designate one owner. See: Rails Routing/Migrations, Django Project/InspectDB, FastAPI, SQLAlchemy, Alembic, Celery, OpenAPI, and the Strangler Fig pattern (Martin Fowler).

Why Python for a Rails migration

Two target stacks (pick one, both work)

Rails → Python quick mapping

Rails Django FastAPI/SQLAlchemy
routes.rb (resources :posts) urls.py + DRF DefaultRouter/ViewSets APIRouter + verb functions (@app.get/post/...)
Controllers Views/ViewSets (DRF) Path ops + dependency injection
Active Record models/migrations Django ORM models + migrations SQLAlchemy ORM + Alembic migrations
ActiveModel::Serializer/Blueprinter DRF Serializers Pydantic v2 models/schemas
Strong Parameters DRF Serializer fields/validators Pydantic model validation
Devise/Warden Django auth, sessions JWT/cookie auth (python-jose/passlib), Starlette sessions
Active Job / Sidekiq Celery (or Django integrations) Celery tasks (retry/backoff)
Action Mailer Django mail backends/providers SMTP/SDK providers
Action Cable Django Channels/WebSockets Starlette/FastAPI WebSockets
Active Storage Storage backends (e.g., S3/GCS) boto3/S3 or GCS SDKs

References: Rails Routing, Active Record, Strong Parameters, Action Mailer, Active Job, Action Cable, Active Storage; Django Project/URLConf/InspectDB/ORM/DRF; FastAPI; SQLAlchemy 2.0; Alembic; Pydantic v2; Celery; Starlette WebSockets; python-jose; passlib; boto3; Google Cloud Storage.

Step-by-step plan (LEAP Protocol)

1) Spec-first extraction (Rails-aware) - Catalog routes, controllers, serializers (ActiveModel::Serializer/Blueprinter), models/scopes, callbacks, Action Mailer, Active Job/Sidekiq, Action Cable channels, and Active Storage. - Derive OpenAPI from routes/controllers; capture DB schema (schema.rb/structure.sql), constraints, and transactional semantics; declare job and mail contracts. - Normalize business rules hidden in before_* callbacks, default_scope, and concerns. - Docs: Rails Routing, Migrations/Schema, Callbacks, Strong Parameters, Mailer, Active Job, Action Cable, Active Storage; OpenAPI.

2) Test generation (parity as a gate) - Spin up the current Rails app in CI and record golden responses for key paths, error cases, and auth flows. - Generate pytest suites: contract tests (OpenAPI), DB invariants, idempotency, N+1 guards, time-zone math, mailer snapshots, and Sidekiq semantics (retries, backoff, dedupe). - Tests fail closed—Python work cannot diverge from proven Rails behavior.

3) Parallel implementation (Python target) - Django path: scaffold project/apps, wire urls.py, implement ViewSets/Serializers/ORM models, configure auth and email backends. Use inspectdb to accelerate model stubs against the existing DB. - FastAPI path: routers/dependencies/middleware, SQLAlchemy 2.0 ORM/Core and Alembic migrations, Pydantic v2 models, Celery for background jobs (Redis/RabbitMQ), and Jinja2 or keep your frontend unchanged. - Map Rails concerns: Active Record ⇒ SQLAlchemy; Strong Parameters/serializers ⇒ Pydantic or DRF serializers; Devise/Warden ⇒ Django auth or FastAPI auth (JWT/cookie sessions via python-jose/passlib); Action Mailer ⇒ provider SDKs; Active Storage ⇒ boto3/S3/GCS; WebSockets ⇒ Starlette/FastAPI or Django Channels.

4) Rollout with verifiable parity - Run Rails and Python behind NGINX/Envoy; mirror (shadow) traffic and diff responses/headers/latency. - Gradual routing (by endpoint, cohort, or feature flag). Dual-run jobs with idempotency keys. - Session compatibility for cookie-signed/encrypted data. Observability (Prometheus, OpenTelemetry) and error budgets drive cutover. - Pattern: Strangler Fig Application (Martin Fowler).

5) Cutover and hardening - Switch traffic, enable write ownership in Python, and retire Rails after a burn-in window. - Deliver final artifacts: spec, tests, Python code, migration playbook, and runbooks.

Operational guardrails

Specific Ruby on Rails concerns we handle

Reference commands you will actually run

Proof

Documentation and primary sources

Pricing & timeline

Ready to move? Email hello@leapagentic.com to schedule a 30-minute scoping call. We’ll review your Rails repo, propose a Python target (usually FastAPI + SQLAlchemy 2.0 + Pydantic + Celery), and return a fixed-price, date-certain plan.

Leap Agentic is distinct from Legacyleap.ai and Impetus Leap AI.


Leap Agentic is distinct from Legacyleap.ai and Impetus Leap AI.