SQL マイグレーション安全チェック(PostgreSQL / MySQL / SQLite)
SQL migration safety check for PostgreSQL, MySQL and SQLite
マイグレーションの SQL から、DROP や WHERE なしの UPDATE/DELETE、DEFAULT なしの NOT NULL 追加、長時間ロック、IF EXISTS なしなど本番で事故になりやすい文をデプロイ前に指摘します。
できること
マイグレーションファイルを本番で実行する前に読み、危険な文を理由と安全な代替案つきで指摘します。データベースには接続しません。
- データ消失:
DROP TABLE/DROP COLUMN/DROP DATABASE/TRUNCATE、WHERE なしのUPDATE・DELETE(サブクエリ内の WHERE は数えません) - 既存データがあると失敗する文: DEFAULT なしの
NOT NULL列追加、SET NOT NULL、UNIQUE・主キー・外部キーの追加 - ロックや書き換え: 列の型変更、PostgreSQL の
CONCURRENTLYなしのインデックス作成、トランザクション内のCREATE INDEX CONCURRENTLY、NOT VALIDなしの制約追加、VACUUM FULLなど - 稼働中のコードを壊す変更: テーブル名・列名の変更
- SQLite 特有: 非対応の ALTER、定数でない DEFAULT、テーブル作り直しパターンの認識、
PRAGMA foreign_keys - 再実行時の失敗:
IF EXISTS/IF NOT EXISTSなし - 大きいテーブルを
--big-tablesで指定すると重要度を引き上げ、同じファイルで作ったばかりのテーブルや down マイグレーションは重要度を下げます
使い方
python scripts/migration_check.py migrations/ --big-tables users,orders --json migration-review.json
エージェントはテーブルの規模や DB のバージョン、コード側の参照を確認したうえで、expand/contract などの安全な手順を提案します。
必要な環境
- Python 3(標準ライブラリのみ)
注意点
- 静的チェックです。重複や NULL の有無などデータ次第の失敗は判定できません。
- 方言の自動判定は推定なので、分かっている場合は
--dialectを指定してください。 - 完全な SQL パーサーではないため、特殊な書き方では文の区切りを誤ることがあります。
English description
Flags risky statements in SQL migrations before deploy: drops, UPDATE or DELETE without WHERE, NOT NULL without default, long locks and rewrites, missing IF EXISTS.
What it does
Reads migration files before they run in production and flags risky statements with the reason and a safer alternative. It never connects to a database.
- Data loss:
DROP TABLE,DROP COLUMN,DROP DATABASE,TRUNCATE,UPDATEorDELETEwithout a top-level WHERE (a WHERE inside a sub-query does not count) - Fails on tables with data:
NOT NULLcolumn without DEFAULT,SET NOT NULL, new UNIQUE, primary or foreign keys - Locks and rewrites: column type changes, PostgreSQL index builds without
CONCURRENTLY,CREATE INDEX CONCURRENTLYinside a transaction, constraints withoutNOT VALID,VACUUM FULLand more - Breaks running code: table and column renames
- SQLite specifics: unsupported ALTER forms, non-constant defaults, table-rebuild pattern detection,
PRAGMA foreign_keys - Re-run failures: missing
IF EXISTSorIF NOT EXISTS --big-tablesraises severity for large tables; tables created in the same file and down migrations are lowered
How to use
python scripts/migration_check.py migrations/ --big-tables users,orders --json migration-review.json
The agent then checks table sizes, database version and code references, and proposes safe sequences such as expand and contract.
Requirements
- Python 3 (standard library only)
Caveats
- Static review: data-dependent failures (duplicates, NULLs) cannot be detected.
- Dialect detection is a guess; pass
--dialectwhen you know it. - It is not a full SQL parser, so unusual syntax may be split into statements incorrectly.
インストール方法
- ダウンロードしたZIPを展開します(中に
sql-migration-safety-check/フォルダとSKILL.mdがあります)。 - フォルダごと、使うエージェントのスキル置き場にコピーします。
- Claude:
~/.claude/skills/(プロジェクト単位なら.claude/skills/) - Hermes Agent:
~/.hermes/skills/ - Codex:
~/.agents/skills//Cursor:~/.cursor/skills/ - Claude アプリ(claude.ai):「カスタマイズ → スキル」からZIPをアップロード
- Claude:
- エージェントを再起動し、スキル一覧に表示されることを確認します。
出品者
出品スキル 44 件