How to AI Code: Your AI is editing your migration files
Intro You’re using ChatGPT or Claude to speed up development. Good. Then suddenly: your migrations are broken. AI tools love editing files they shouldn’t touch , especially: SQL migration scripts EF Core migration files Schema snapshots This is dangerous. One wrong change can corrupt your database history. Recognize migration files EF Core migrations Typical structure: /Migrations/ 20240101120000_InitialCreate.cs 20240102130000_AddUsers.cs MyDbContextModelSnapshot.cs Example: public partial class AddUsers : Migration { protected override void Up ( MigrationBuilder migrationBuilder ) { migrationBuilder . CreateTable ( name : "Users" , columns : table => new { Id = table . Column < int >( nullable : false ) }); } } SQL migrations /db/migrations/ V001__init.sql V002__add_users.sql Example: CREATE TABLE Users ( Id INT PRIMARY KEY ); Key indicators Timestamp or version prefix Sequential naming Contains schema changes only Stored in a dedicated folder Understand the risk WRONG (AI rewriting history) AI might do this: // Modified existing migration (BAD) migrationBuilder . DropTable ( "Users" ); migrationBuilder . CreateTable ( "Customers" ); or: -- Edited old migration (BAD) ALTER TABLE Users RENAME TO Customers ; This breaks every environment except fresh ones. This is extremely dangerous if your AI does not recognize them. CORRECT (append-only) Always create a new migration : // New migration migrationBuilder . RenameTable ( name : "Users" , newName : "Customers" ); Step 3: Lock migration files from Claude AI editing Explicit blocking of editing We will create 2 new files. These files will instruct that CALUDE is not allowed to modify existing files. What we actually want to achieve is a way to keep appending the files. { "$schema": "https://json.schemastore.org/claude-code-settings.json", "hooks": { "PreToolUse": [ { "matcher": "Edit|Write|MultiEdit", "hooks": [ { "type": "command", "command": "bash \"$CLAUDE_PROJECT_DIR/.claude/hooks/block-migration-edits.sh\"" } ] }