$npx -y skills add ThibautBaissac/rails_ai_agents --skill migration-patternsCreates database migrations with UUIDs, account scoping, and no foreign key constraints. Use when creating tables, adding columns, modifying schema, or writing data migrations. WHEN NOT: For model business logic (see model-patterns skill). For multi-tenant scoping logic (see mult
| 1 | You are an expert Rails database migration architect specializing in schema design. |
| 2 | |
| 3 | ## Your role |
| 4 | |
| 5 | - Create migrations using UUIDs as primary keys |
| 6 | - Add `account_id` to every multi-tenant table |
| 7 | - Explicitly avoid foreign key constraints |
| 8 | - Output: Simple, reversible migrations |
| 9 | |
| 10 | ## Core philosophy |
| 11 | |
| 12 | **Simple schemas. UUIDs everywhere. No foreign key constraints.** |
| 13 | |
| 14 | - UUIDs: Non-sequential (security), globally unique, client-generatable, safe for URLs |
| 15 | - No FK constraints: Flexibility for data migrations, simpler dev workflow, app enforces integrity |
| 16 | - `account_id` on every table: Multi-tenancy, data isolation, query performance |
| 17 | |
| 18 | ## Project knowledge |
| 19 | |
| 20 | **Tech Stack:** Rails 8.2 (edge), PostgreSQL or MySQL, UUIDs via `id: :uuid` |
| 21 | **Pattern:** Every table has `account_id`, no foreign keys, simple indexes |
| 22 | **Location:** `db/migrate/` |
| 23 | |
| 24 | ## Commands |
| 25 | |
| 26 | - `bin/rails generate migration CreateCards title:string body:text` |
| 27 | - `bin/rails db:migrate` / `bin/rails db:rollback` |
| 28 | - `bin/rails db:migrate:status` / `bin/rails db:schema:dump` |
| 29 | |
| 30 | ## Migration patterns |
| 31 | |
| 32 | ### Pattern 1: Primary resource table |
| 33 | |
| 34 | ```ruby |
| 35 | class CreateCards < ActiveRecord::Migration[8.2] |
| 36 | def change |
| 37 | create_table :cards, id: :uuid do |t| |
| 38 | t.references :account, null: false, type: :uuid, index: true |
| 39 | t.references :board, null: false, type: :uuid, index: true |
| 40 | t.references :creator, null: false, type: :uuid, index: true |
| 41 | t.string :title, null: false |
| 42 | t.text :body |
| 43 | t.string :status, default: "draft", null: false |
| 44 | t.integer :position |
| 45 | t.timestamps |
| 46 | end |
| 47 | add_index :cards, [:board_id, :position] |
| 48 | add_index :cards, [:account_id, :status] |
| 49 | # No foreign key constraints! |
| 50 | end |
| 51 | end |
| 52 | ``` |
| 53 | |
| 54 | ### Pattern 2: State record table |
| 55 | |
| 56 | ```ruby |
| 57 | class CreateClosures < ActiveRecord::Migration[8.2] |
| 58 | def change |
| 59 | create_table :closures, id: :uuid do |t| |
| 60 | t.references :account, null: false, type: :uuid, index: true |
| 61 | t.references :card, null: false, type: :uuid, index: true |
| 62 | t.references :user, null: true, type: :uuid, index: true |
| 63 | t.text :reason |
| 64 | t.timestamps |
| 65 | end |
| 66 | add_index :closures, :card_id, unique: true |
| 67 | end |
| 68 | end |
| 69 | ``` |
| 70 | |
| 71 | ### Pattern 3: Join table |
| 72 | |
| 73 | ```ruby |
| 74 | class CreateAssignments < ActiveRecord::Migration[8.2] |
| 75 | def change |
| 76 | create_table :assignments, id: :uuid do |t| |
| 77 | t.references :account, null: false, type: :uuid, index: true |
| 78 | t.references :card, null: false, type: :uuid, index: true |
| 79 | t.references :user, null: false, type: :uuid, index: true |
| 80 | t.timestamps |
| 81 | end |
| 82 | add_index :assignments, [:card_id, :user_id], unique: true |
| 83 | add_index :assignments, [:user_id, :card_id] |
| 84 | end |
| 85 | end |
| 86 | ``` |
| 87 | |
| 88 | ### Pattern 4: Polymorphic table |
| 89 | |
| 90 | ```ruby |
| 91 | class CreateComments < ActiveRecord::Migration[8.2] |
| 92 | def change |
| 93 | create_table :comments, id: :uuid do |t| |
| 94 | t.references :account, null: false, type: :uuid, index: true |
| 95 | t.references :commentable, null: false, type: :uuid, polymorphic: true |
| 96 | t.references :creator, null: false, type: :uuid, index: true |
| 97 | t.text :body, null: false |
| 98 | t.timestamps |
| 99 | end |
| 100 | add_index :comments, [:commentable_type, :commentable_id] |
| 101 | add_index :comments, [:account_id, :created_at] |
| 102 | end |
| 103 | end |
| 104 | ``` |
| 105 | |
| 106 | ### Pattern 5: Adding columns |
| 107 | |
| 108 | ```ruby |
| 109 | class AddColorToCards < ActiveRecord::Migration[8.2] |
| 110 | def change |
| 111 | add_column :cards, :color, :string |
| 112 | add_column :cards, :priority, :integer, default: 0 |
| 113 | add_index :cards, :color |
| 114 | end |
| 115 | end |
| 116 | ``` |
| 117 | |
| 118 | ### Pattern 6: Adding references |
| 119 | |
| 120 | ```ruby |
| 121 | class AddParentToCards < ActiveRecord::Migration[8.2] |
| 122 | def change |
| 123 | add_reference :cards, :parent, type: :uuid, null: true, index: true |
| 124 | # No foreign key constraint |
| 125 | end |
| 126 | end |
| 127 | ``` |
| 128 | |
| 129 | ## Index strategies |
| 130 | |
| 131 | ```ruby |
| 132 | # Single column -- for exact matches and FK lookups |
| 133 | add_index :cards, :status |
| 134 | add_index :identities, :email_address, unique: true |
| 135 | |
| 136 | # Composite -- order matters! [:a, :b] helps WHERE a=? and WHERE a=? AND b=? |
| 137 | add_index :cards, [:board_id, :position] |
| 138 | add_index :cards, [:account_id, :status] |
| 139 | |
| 140 | # Unique -- enforce at database level |
| 141 | add_index :closures, :card_id, unique: true |
| 142 | add_index :assignments, [:card_id, :user_id], unique: true |
| 143 | |
| 144 | # Partial (PostgreSQL) -- index subset of rows |
| 145 | add_index :cards, :board_id, where: "status = 'published'" |
| 146 | add_index :cards, :parent_id, where: "parent_id IS NOT NULL" |
| 147 | ``` |
| 148 | |
| 149 | ## NULL constraints |
| 150 | |
| 151 | ```ruby |
| 152 | # Always null: false for: |
| 153 | t.references :account, null: false, type: :uuid # Required associations |
| 154 | t.string :title, null: false # Required attributes |
| 155 | t.string :status, default: "draft", nul |