Rails - Migrations

rails

What is a migration?

A migration is a Ruby class. As we've just seen, rails generate model creates a database migration file inside the db/migrate directory. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it's possible to undo a migration after it's been applied to your database. Migration filenames include a timestamp to ensure that they're processed in the order that they were created.

If you look in the db/migrate/20140120191729_create_articles.rb file (remember, yours will have a slightly different name), here's what you'll find:

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :title
      t.text :text

      t.timestamps
    end
  end
end

The above migration creates a method named change which will be called when you run this migration.

When you run this migration it will create an articles table with one string column and a text column. It also creates two timestamp fields to allow Rails to track article creation and update times.

How to run a migration?

bin/rake db:migrate

Because you're working in the development environment by default, this command will apply to the database defined in the development section of your config/database.yml file. If you would like to execute migrations in another environment, for instance in production, you must explicitly pass it when invoking the command: rake db:migrate RAILS_ENV=production.

How to run a migration against a particular environment?

rake db:migrate RAILS_ENV=production

What does the content of a migration file look like?

In addition to the model, Rails has also made a migration to create the corresponding database table:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.string :commenter
      t.text :body

      # this line adds an integer column called `article_id`.
      t.references :article, index: true

      t.timestamps
    end
  end
end

In a migration file, what is the purpose of t.references?

The t.references line sets up a foreign key column for the association between the two models. An index for this association is also created on this column.

Why does the migration file names have the numbers that look like a time stamp?

  1. This gives developer an idea about when this migration file was generated.
  2. If we was to capture all the changes to the database in a single file, it is difficult for software to keep track of which changes were migrated. As this file grows over time, it may become hard for developers to work with this file because it becomes large and developers will have to scroll, or developers may wish to delete previous changes that were migrated. If two developers happen to delete previous changes that had been migrated at the same time, source code control system may produce a conflict, and the developers will have to resolve this useless conflict. So, when it comes to migration files, it is a good idea to keep each migration in its own file. Using a time stamp as part of the file name is a good way to generate the file (so that developers do not have to think about the name of the file).
  3. Rails may use this information, and possibly the timestamp of the file and other information to determine if this particular change was migrated. Rails also keep track of which migration were migrated in the database, and therefore is smart enough to only execute the migrations that have not already been run against the current database.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License