Skip to content

Instantly share code, notes, and snippets.

@ddeveloperr
Last active February 22, 2016 07:50
Show Gist options
  • Select an option

  • Save ddeveloperr/4354c4d22719408c5c1b to your computer and use it in GitHub Desktop.

Select an option

Save ddeveloperr/4354c4d22719408c5c1b to your computer and use it in GitHub Desktop.
Migrations in Ruby on Rails
Migration Overview
Migrations are a convenient way to alter your database schema over time in a consistent and easy way.
They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be
database independent.
db:migrate runs (single) migrations that have not run yet.
db:create creates the database
db:drop deletes the database
db:schema:load creates tables and columns within the (existing) database following schema.rb
db:setup does db:create, db:schema:load, db:seed
db:reset does db:drop, db:setup
Typically, you would use db:migrate after having made changes to the schema via new migration files
(this makes sense only if there is already data in the database). db:schema:load is used when you setup
a new instance of your app.
the source and the dependencies are:
db:create creates the database for the current env
db:create:all creates the databases for all envs
db:drop drops the database for the current env
db:drop:all drops the databases for all envs
db:migrate runs migrations for the current env that have not run yet
db:migrate:up runs one specific migration
db:migrate:down rolls back one specific migration
db:migrate:status shows current migration status
db:rollback rolls back the last migration
db:forward advances the current schema version to the next one
db:seed (only) runs the db/seed.rb file
db:schema:load loads the schema into the current env's database
db:schema:dump dumps the current env's schema (and seems to create the db as well)
db:setup runs db:schema:load, db:seed
db:reset runs db:drop db:setup
db:migrate:redo runs (db:migrate:down db:migrate:up) or (db:rollback db:migrate) depending on the specified migration
db:migrate:reset runs db:drop db:create db:migrate
For further information please have a look at
https://github.com/rails/rails/blob/v3.2.12/activerecord/lib/active_record/railties/databases.rake (for Rails 3.2.x)
and
https://github.com/rails/rails/blob/v4.0.5/activerecord/lib/active_record/railties/databases.rake (for Rails 4.0.x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment