ORM Lens

SQL migration risk rules (preview)

orm-lens migration-risk --path . also reads migrations kept as plain SQL, so Node, Go, Java and Supabase projects get the same review as Django and Alembic ones. Like the other rules it is static: the files are read as text, never run, and no database is touched. Usage, output formats and CI semantics are those of the Django migration rules: exit code 1 on critical findings, --severity critical|warning|info|all (default critical), --format text|json|sarif|github|gitlab. Findings carry the tool in app, the migration in migration, the table in model and the column in field.

Tool What is read Order
Prisma migrations/*/migration.sql next to migration_lock.toml folder name
Drizzle drizzle-kit 0.x: the .sql files listed in meta/_journal.json; 1.x: <out>/*/migration.sql beside a snapshot.json journal idx, or folder name
Supabase supabase/migrations/*.sql file name
Flyway V<version>__<name>.sql under a migration or migrations directory, subdirectories included (repeatable R__ files are skipped) version
golang-migrate, and tools with the same naming (sqlx, morph) *.up.sql (*.down.sql is skipped) leading number
goose .sql files with -- +goose Up, up to -- +goose Down leading number
dbmate .sql files with -- migrate:up, up to -- migrate:down leading number

Other .sql files, such as schema.sql or seed data, are not migrations and are not read, except in supabase/migrations, where every .sql file is a migration. Neither are node_modules, vendor, dist, build, target and hidden directories.

The MCP server accepts a workspace with SQL migrations and no Django or SQLAlchemy project when one of these files is at most four directories down: migration_lock.toml, meta/_journal.json, a migration.sql beside a snapshot.json, a Flyway or *.up.sql file, .sql files in a migration or migrations directory, or a numbered .sql file with a goose (-- +goose Up) or dbmate (-- migrate:up) marker. For that marker only the first 20 numbered files of a directory are opened, and only their first 64 KiB. If yours is deeper, or its marker is further in, point workspace_root at the migrations directory or a directory named migrations.

Dialect

Prisma records the database in migration_lock.toml and Drizzle in its journal (0.x) or the newest snapshot (1.x). For the other tools the scanned root or a directory below it named mysql, mariadb, sqlite or postgres decides, as in Mattermost’s db/migrations/mysql/; the directories a project is checked out under don’t count. Otherwise the SQL decides, outside comments and strings: backquoted names, ENGINE=, AUTO_INCREMENT or MODIFY COLUMN mean MySQL, AUTOINCREMENT or PRAGMA mean SQLite, and the dialect with the most of its own constructs wins. A Flyway folder named for a database inside a location (db/migration/mysql) gives its dialect to the history it joins, or stays a history of its own when that one has another. PostgreSQL is assumed when nothing tells.

Populated tables

Checked against 2539 real migrations: Prisma in Cal.com, Documenso, Umami, Langfuse and Trigger.dev, Drizzle in lobe-chat (PostgreSQL) and openstatus (SQLite). Another 425 drizzle-kit 1.x migrations: hollo, Hackers’ Pub and Zipline (PostgreSQL), Zerobyte, Headplane, LNReader, holocron and a Cloudflare Workers SaaS template (SQLite).

sql_add_column_not_null_no_default

Operation: ADD COLUMN · Severity: critical · Confidence: high · populated tables, PostgreSQL

Adds a NOT NULL column (or a primary key) with no DEFAULT. Existing rows get NULL, so the ALTER TABLE fails. Identity, generated and serial columns are filled by the database and are exempt.

-- Prisma generates this, with a warning that it fails on a table that has rows
ALTER TABLE "Booking" ADD COLUMN "uid" TEXT NOT NULL;

Prisma itself warns about this case in the migration it generates. When the authors edit the migration to add the column nullable, backfill it and then set NOT NULL, or to give it a default, this rule stays silent and the SET NOT NULL step is reported instead. It also catches a case Prisma words differently: a column dropped and added again as NOT NULL because its type changed.

Mitigation: Add the column nullable, backfill it, then set NOT NULL in a later migration. Or give it a DEFAULT.

sql_create_index_locks_table

Operation: CREATE INDEX · Severity: warning · Confidence: medium · populated tables, PostgreSQL

CREATE INDEX without CONCURRENTLY blocks writes to the table for the whole build.

Mitigation: CREATE INDEX CONCURRENTLY, in a migration of its own: it cannot run inside a transaction.

sql_set_not_null_scans_table

Operation: ALTER COLUMN ... SET NOT NULL · Severity: warning · Confidence: medium · populated tables, PostgreSQL

PostgreSQL scans the whole table under an exclusive lock to verify there are no NULLs, and the ALTER fails if any row still has one.

Mitigation: Backfill first. On large tables add CHECK (col IS NOT NULL) NOT VALID, VALIDATE it, then set NOT NULL.

sql_alter_column_type_rewrites

Operation: ALTER COLUMN ... TYPE, MODIFY, CHANGE · Severity: warning · Confidence: medium · populated tables, not SQLite

Most type changes rewrite the table under an exclusive lock. Some don’t (a longer varchar, varchar to text), but the migration alone doesn’t say what the old type was.

Mitigation: Add a column of the new type and keep it in step with a trigger on the old one; backfill with batched UPDATE … SET new = old (with the cast the migration uses), which reads and writes each row under its lock so no newer write is overwritten; move reads and writes to the new column, then drop the old one once no instance of the previous release is left.

sql_rename_column_breaks_running_code

Operation: RENAME COLUMN, or CHANGE old new · Severity: warning · Confidence: medium · populated tables

Code still running from the previous release keeps using the old name until every instance is replaced. The finding’s field is the new name, and the description names the old one.

Mitigation: Add the new column and keep it in step with a trigger on the old one; backfill with batched UPDATE … SET new = old, which reads and writes each row under its lock; move reads and writes to the new column, then drop the old one once no instance of the previous release is left.

sql_rename_table_breaks_running_code

Operation: ALTER TABLE ... RENAME TO, RENAME TABLE · Severity: warning · Confidence: medium · populated tables

Code still running from the previous release keeps querying the old name.

Mitigation: Create the new table and mirror every insert, update and delete on the old one into it with a trigger, in the same transaction; backfill in batches that lock the rows they copy (SELECT … FOR SHARE) and skip rows already there, so an update or delete waits for the batch and is then mirrored; move reads and writes to the new table and point foreign keys that reference the old one at it, then drop the old one once no instance of the previous release is left.

sql_drop_column_breaks_running_code

Operation: DROP COLUMN · Severity: warning · Confidence: medium · populated tables

The previous release still selects the column until every instance is replaced.

Mitigation: Remove the column from the code and deploy first; drop it in a later release.

sql_drop_table_breaks_running_code

Operation: DROP TABLE · Severity: warning · Confidence: medium · populated tables

The previous release still queries the table, so every remaining instance fails until it is replaced.

Mitigation: Remove the table from the code and deploy first; drop it in a later release.

sql_add_unique_constraint_locks_and_may_fail

Operation: ADD CONSTRAINT ... UNIQUE / PRIMARY KEY / EXCLUDE · Severity: warning · Confidence: medium · populated tables, not SQLite

The index is built while writes are blocked, and existing duplicates make it fail. ADD CONSTRAINT ... USING INDEX reuses an index that is already built and is not reported.

Mitigation: For UNIQUE and PRIMARY KEY, CREATE UNIQUE INDEX CONCURRENTLY first, then ADD CONSTRAINT ... USING INDEX. For a primary key, set NOT NULL on its columns first (backfilled and checked as in sql_set_not_null_scans_table): otherwise attaching the index scans the table under the lock. USING INDEX does not work for EXCLUDE: its index is always built under the lock, so add it while the table is small or writes can wait.

sql_add_foreign_key_validates_rows

Operation: ADD CONSTRAINT ... FOREIGN KEY · Severity: warning · Confidence: medium · populated tables, not SQLite

Every existing row is checked while both tables are locked against writes. A constraint added NOT VALID is not reported.

Mitigation: Add it NOT VALID, then VALIDATE CONSTRAINT in a separate migration.

sql_add_check_constraint_validates_rows

Operation: ADD CONSTRAINT ... CHECK · Severity: warning · Confidence: medium · populated tables, not SQLite

Every existing row is checked under an exclusive lock. A constraint added NOT VALID is not reported.

Mitigation: Add it NOT VALID, then VALIDATE CONSTRAINT in a separate migration.