agentsclimarketplace

Rails db

Skill Tyr0/agent-skills/plugins/rails-expert/skills/rails-db

A collection of skills, plugins, and agents for AI workflows.

Install
npx -y skills add Tyr0/agent-skills --skill rails-db

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its author says it does

Copied from the file, not written here

Use this skill whenever the user asks about Rails database management, including running or rolling back migrations, checking migration status, seeding the database, resetting or dropping the database, schema loading, or any `bin/rails db:*` task. Also use it for questions about the schema.rb vs structure.sql choice, multi-database setups, annotating models with schema, or troubleshooting common migration errors.

SKILL.md

6.0 KB, ~1.6k tokens by cl100k_base, as published. Nobody here has run it

Rails Database Management Reference

A dense reference for all bin/rails db:* tasks and database lifecycle management.


Task Quick Reference

TaskWhat it does
db:createCreate the database(s) defined in database.yml
db:dropDrop the database(s)
db:migrateRun pending migrations
db:rollbackRevert the last migration (or last N with STEP=N)
db:migrate:statusShow each migration and whether it has run
db:versionPrint the current schema version (timestamp of last migration)
db:seedRun db/seeds.rb
db:schema:loadLoad db/schema.rb into the database (skips migrations)
db:schema:dumpRegenerate db/schema.rb from the live database
db:structure:loadLoad db/structure.sql (for config.active_record.schema_format = :sql)
db:structure:dumpRegenerate db/structure.sql
db:resetdb:drop + db:setup
db:setupdb:create + db:schema:load + db:seed
db:prepareCreate if missing + db:migrate (idempotent; use in CI and release commands)
db:purgeTruncate all tables without dropping the database
db:environment:setWrite ar_internal_metadata to mark the current Rails env

Migrations

Run pending migrations

bin/rails db:migrate

Run for a specific environment:

RAILS_ENV=production bin/rails db:migrate

Migrate to a specific version (timestamp):

bin/rails db:migrate VERSION=20240101120000

Roll back

bin/rails db:rollback          # revert last migration
bin/rails db:rollback STEP=3   # revert last 3 migrations

Roll back to a specific version:

bin/rails db:migrate:down VERSION=20240101120000

Re-run a specific migration (down then up):

bin/rails db:migrate:redo VERSION=20240101120000
bin/rails db:migrate:redo STEP=2   # redo last 2

Check status

bin/rails db:migrate:status

Output:

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20240101000001  Create users
   up     20240215000001  Add email to users
  down    20240310000001  Add index on users email

Migration file anatomy

class AddIndexOnUsersEmail < ActiveRecord::Migration[7.2]
  def change
    add_index :users, :email, unique: true
  end
end
  • Use change when the migration is reversible (Rails auto-generates down).
  • Use up / down explicitly for irreversible operations (e.g., execute, data transforms).

Safe migration practices

PatternNotes
add_column with a defaultIn Postgres 11+, safe — no table rewrite. In older versions, consider add_column then change_column_default separately
remove_columnDeploy code that no longer references the column first; then remove
Adding a NOT NULL constraintAdd column as nullable, backfill, then add constraint — never one-step on large tables
add_indexUse algorithm: :concurrently in Postgres to avoid table lock. Requires disable_ddl_transaction!
class AddIndexConcurrently < ActiveRecord::Migration[7.2]
  disable_ddl_transaction!

  def change
    add_index :users, :email, algorithm: :concurrently
  end
end

Schema Format

db/schema.rb (default) — Ruby DSL, database-agnostic, loads fast.

db/structure.sql — Raw SQL, preserves triggers, custom types, views, functions.

To switch to SQL format, in config/application.rb:

config.active_record.schema_format = :sql

Commit schema files to version control. When in doubt, commit the regenerated file after every migration run.


Seeding

bin/rails db:seed

db/seeds.rb is plain Ruby. Use find_or_create_by to make seeds idempotent:

# db/seeds.rb
User.find_or_create_by(email: "[email protected]") do |u|
  u.password = "changeme"
  u.role = :admin
end

Run seed after schema load:

bin/rails db:setup   # create + schema:load + seed

Only seed (no create/migrate):

bin/rails db:seed

Database Lifecycle

Development reset (destructive)

bin/rails db:reset   # drop + create + schema:load + seed

Purge without dropping (faster for large schemas)

bin/rails db:purge db:schema:load db:seed

CI setup (idempotent)

bin/rails db:prepare   # creates if needed, then migrates

Multi-Database

Rails 6+ supports multiple databases. Tasks accept a :<db> suffix:

bin/rails db:migrate                    # all databases
bin/rails db:migrate:primary            # primary only
bin/rails db:migrate:animals            # named db only
bin/rails db:rollback:primary STEP=1
bin/rails db:migrate:status:animals

database.yml for multiple databases:

development:
  primary:
    adapter: postgresql
    database: myapp_development
  animals:
    adapter: postgresql
    database: myapp_animals_development
    migrations_paths: db/animals_migrate

Troubleshooting

ErrorCauseFix
PendingMigrationErrorMigrations haven't been runbin/rails db:migrate
ActiveRecord::NoDatabaseErrorDatabase doesn't existbin/rails db:create db:migrate
already exists on db:createDatabase was already createdSafe to ignore; use db:prepare to avoid
Migration stuck (lock)Another process holds a DB lockKill the other process; check pg_stat_activity
Schema mismatch in test envdb/schema.rb not loadedbin/rails db:test:prepare or RAILS_ENV=test bin/rails db:schema:load
down migration failschange method not reversibleImplement explicit up/down methods

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 327,069. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.