CtrlK
BlogDocsLog inGet started
Tessl Logo

igmarin/rails-agent-skills

Curated library of 28 atomic skills and 9 personas for Ruby on Rails development. Organized by category: testing, code-quality, engines, infrastructure, api, context, and personas. Covers code review, architecture, security, testing (RSpec), engines, Hotwire, and TDD automation. Shared Ruby skills (YARD docs, DDD, service objects) have moved to ruby-core-skills.

93

1.16x
Quality

95%

Does it follow best practices?

Impact

93%

1.16x

Average score across 28 eval scenarios

SecuritybySnyk

Advisory

Suggest reviewing before use

Overview
Quality
Evals
Security
Files

PATTERNS.mdskills/infrastructure/review-migration/

Advanced Migration Patterns

Reference for complex schema operations beyond add-column and add-index.

Type Changes

Never change a column type in a single migration on a busy table. Use the add-copy-migrate-drop approach:

# Step 1: add new column with new type
add_column :orders, :amount_cents, :bigint

# Step 2: backfill (separate deploy, in batches)
# Order.in_batches.update_all('amount_cents = CAST(amount * 100 AS BIGINT)')

# Step 3: migrate code references to new column, then drop old column
remove_column :orders, :amount

Multi-Step Unique Constraints

Adding a unique index on data that may have duplicates will fail at the database level:

  1. Query for duplicates: SELECT col, COUNT(*) FROM table GROUP BY col HAVING COUNT(*) > 1
  2. Resolve duplicates in application code or a data migration
  3. Add unique index concurrently after data is clean:
disable_ddl_transaction!
add_index :users, :email, unique: true, algorithm: :concurrent

Foreign Keys on Large Tables

Adding a foreign key validates all existing rows by default, which can lock the table:

# Step 1: add without validation (fast, no lock)
add_foreign_key :orders, :users, validate: false

# Step 2 (separate migration): validate after cleaning orphans
validate_foreign_key :orders, :users

Removing a Column Safely

Rails caches column information. Removing a column without first telling Active Record to ignore it causes ActiveModel::MissingAttributeError during the deploy window.

# Step 1: add to ignored_columns in the model (deploy this first)
class Order < ApplicationRecord
  self.ignored_columns += %w[legacy_field]
end

# Step 2 (next deploy): drop the column
remove_column :orders, :legacy_field

Multi-Database Migrations

When running migrations against a non-primary database, use connects_to in the migration class:

class AddIndexToAnalyticsEvents < ActiveRecord::Migration[7.2]
  def connection
    AnalyticsEvent.connection
  end

  def change
    disable_ddl_transaction!
    add_index :analytics_events, :occurred_at, algorithm: :concurrent
  end
end

PostgreSQL Lock Behavior Reference

Use this table when stating lock/rewrite risk for each migration step.

OperationPG LockBlocksDurationNotes
add_column (nullable, no default)ACCESS EXCLUSIVEAll~msFast metadata-only change
add_column (with default, PG 11+)ACCESS EXCLUSIVEAll~msNo rewrite since PG 11
add_column (with default, PG < 11)ACCESS EXCLUSIVEAllFull table rewriteAvoid on large tables
add_index (standard)SHAREWritesDuration of index buildBlocks INSERT/UPDATE/DELETE
add_index (concurrent)SHARE UPDATE EXCLUSIVEDDL onlyDuration of index buildDoes not block writes
add_foreign_key (validate: true, default)SHARE ROW EXCLUSIVEWritesFull row scanValidates all existing rows
add_foreign_key (validate: false)ACCESS EXCLUSIVEAll~msMetadata-only, no row scan
validate_foreign_keySHARE UPDATE EXCLUSIVEDDL onlyFull row scanDoes not block writes
change_column_null (to NOT NULL)ACCESS EXCLUSIVEAllFull row scanScans every row to verify no NULLs
change_column (type change)ACCESS EXCLUSIVEAllFull table rewriteAlways rewrite unless cast is binary-coercible
remove_columnACCESS EXCLUSIVEAll~msFast metadata-only change
rename_columnACCESS EXCLUSIVEAll~msFast but breaks running app code

skills

infrastructure

review-migration

README.md

tile.json