Schema Evolution Engine
Data Engineering
01: Problem
Every Schema Change Was a Hostage Negotiation
The PostgreSQL database backing my workforce education platform had accumulated many tables over the years. Every time a new program launched or a reporting requirement changed, the schema needed to evolve. Adding a column was simple. Renaming one was terrifying. Changing a foreign key relationship required a late-night deployment window because the migration locked the table and the application threw server errors until it completed. I had no way to verify whether a migration would break existing queries, API responses, or downstream reports until I ran it, and by then, the damage was done.
I needed a migration system that could answer one question before execution: “will this change break anything that currently works?”
02: Architecture
Validate Before You Migrate
The engine wraps standard Alembic migrations with a pre-flight validation layer that checks backward compatibility before any DDL statement touches the database:
Schema Diff Analyzer
Before running a migration, the engine generates a diff between the current schema and the proposed schema. It classifies every change into one of four categories: safe (adding a nullable column), cautious (adding a NOT NULL column with a default), breaking (dropping a column referenced by an active view), or destructive (dropping a table). Breaking and destructive changes require explicit confirmation and generate a rollback script automatically.
Query Compatibility Checker
The engine maintains a registry of known queries: extracted from application code and reporting templates. For each proposed schema change, it runs every registered query against the new schema using PostgreSQL’s EXPLAIN command (without executing). If a query would fail against the proposed schema, the migration is blocked and the offending queries are listed with their source locations.
Zero-Downtime Execution
Migrations classified as “safe” or “cautious” execute using PostgreSQL’s CREATE INDEX CONCURRENTLY and non-locking ALTER TABLE strategies. For changes that require table locks, the engine uses a shadow-table approach: create the new table structure, backfill data in batches, swap the table name, and drop the old table. This keeps the application serving requests throughout.
Key Design Decisions
Why a query registry instead of just schema comparison? Schema comparison can tell you that a column was dropped. It can’t tell you that several reports and an API endpoint depend on that column. The query registry closes this gap by testing actual usage patterns against the proposed change. It’s more maintenance overhead, queries must be registered, but it catches breaking changes that schema-level analysis misses.
Why Alembic as the migration engine? I evaluated Flyway and raw SQL scripts. Alembic integrates natively with SQLAlchemy (which I already use for the application ORM) and supports both auto-generated and hand-written migrations. The auto-generation catches most changes; the hand-written path handles the complex cases like data conversions and backfills. One tool for both is worth the SQLAlchemy coupling.
Why shadow-table swaps instead of online DDL tools like pg_repack? pg_repack requires superuser privileges and adds an external dependency. The shadow-table approach uses only standard PostgreSQL operations that my database user already has permission to run. It’s slower for large tables (the backfill takes time), but it works within my existing permission model without involving the hosting provider.
03: Reflection
Safety Is a Design Constraint, Not a Feature
The blocked migrations were the most valuable outcome. Each one would have broken a production query that schema-level analysis alone wouldn’t have caught. One migration renamed a column that a Grafana dashboard panel referenced directly via raw SQL, completely invisible to the ORM layer. Without the query registry, that dashboard would have silently displayed zero results until someone noticed days later.
What I’d change: the query registry is currently maintained manually. Every time someone writes a new report or API endpoint, they need to register its queries. I’d automate this by parsing the application codebase for SQLAlchemy query patterns and raw SQL strings, building the registry from source code rather than manual entries. This would eliminate the biggest maintenance burden of the system.
“The cost of a migration isn’t the time it takes to run. It’s the trust you lose when it breaks something no one expected.”
Key Outcomes
Every table under managed migrations; No breaking deployments after adoption; Migrations validated, with several blocked before execution; No application downtime during schema changes