SQL マイグレーション安全チェック(PostgreSQL / MySQL / SQLite)

SQL migration safety check for PostgreSQL, MySQL and SQLite

マイグレーションの SQL から、DROP や WHERE なしの UPDATE/DELETE、DEFAULT なしの NOT NULL 追加、長時間ロック、IF EXISTS なしなど本番で事故になりやすい文をデプロイ前に指摘します。

ClaudeHermes AgentCodexCursorMIT(改変・再配布・商用利用可)Python

できること

マイグレーションファイルを本番で実行する前に読み、危険な文を理由と安全な代替案つきで指摘します。データベースには接続しません。

  • データ消失: 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, UPDATE or DELETE without a top-level WHERE (a WHERE inside a sub-query does not count)
  • Fails on tables with data: NOT NULL column without DEFAULT, SET NOT NULL, new UNIQUE, primary or foreign keys
  • Locks and rewrites: column type changes, PostgreSQL index builds without CONCURRENTLY, CREATE INDEX CONCURRENTLY inside a transaction, constraints without NOT VALID, VACUUM FULL and 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 EXISTS or IF NOT EXISTS
  • --big-tables raises 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 --dialect when you know it.
  • It is not a full SQL parser, so unusual syntax may be split into statements incorrectly.

インストール方法

  1. ダウンロードしたZIPを展開します(中に sql-migration-safety-check/ フォルダと SKILL.md があります)。
  2. フォルダごと、使うエージェントのスキル置き場にコピーします。
    • Claude:~/.claude/skills/(プロジェクト単位なら .claude/skills/)
    • Hermes Agent:~/.hermes/skills/
    • Codex:~/.agents/skills//Cursor:~/.cursor/skills/
    • Claude アプリ(claude.ai):「カスタマイズ → スキル」からZIPをアップロード
  3. エージェントを再起動し、スキル一覧に表示されることを確認します。

Windows・Mac 別、エージェント別のインストール手順

出品者

AI Skill Market 公式

出品スキル 44 件

出品者のプロフィールとスキル一覧