Ruby on Rails Rails Migration - Ruby on Rails

What is Rail Migrations and what are its uses?

The next step in creating Rails Applications is to generate Migrations that simplify the creating and maintaining of database tables and columns. Rails Migration allows you to use Ruby to define changes to your database schema, making it possible to use a version control system to keep things synchronized with the actual code.

The following are some of its many uses−

  • Teams of developers − If one person makes a schema change, the other developers just need to update, and run "rake migrate".
  • Production servers − Run "rake migrate" when you roll out a new release to bring the database up to date as well.

  • Multiple machines − If you develop on both a desktop and a laptop, or in more than one location, migrations can help you keep them all synchronized.

What Can Rails Migration Do?

  • create_table(name, options)
  • drop_table(name)
  • rename_table(old_name, new_name)
  • add_column(table_name, column_name, type, options)
  • rename_column(table_name, column_name, new_column_name)
  • change_column(table_name, column_name, type, options)
  • remove_column(table_name, column_name)
  • add_index(table_name, column_name, index_type)
  • remove_index(table_name, column_name)

What are the data types that migration supports?

  • string − for small data types such as a title.

  • text − for longer pieces of textual data, such as the description.

  • integer − for whole numbers.

  • float − for decimals.

  • datetime and timestamp − store the date and time into a column.

  • date and time − store either the date only or time only.

  • binary − for storing data such as images, audio, or movies.

  • Boolean − for storing true or false values.

List of valid column options
  • limit ( :limit => “50” )

  • default (:default => “blah” )

  • null (:null => false implies NOT NULL)

It is to be noted that the activities done by Rails Migration can be done using any front-end GUI or directly on SQL prompt, but Rails Migration makes all those activities very easy.

How to create the Migrations

The generic syntax for creating a migration is −

This syntax will create the file db/migrate/001_table_name.rb. A migration file contains the basic Ruby syntax that describes the data structure of a database table.

Make sure to clean the existing migrations generated by model generators before running the migration generator.

Now, let us create two migrations corresponding to our three tables − books and subjects.

Books migration should be as follows −

This generates the following code.

Book migration code

subject migration should be as follows −

Above command generates the following code.
Subject migration code

It is important to note that you are using lower case for book and subject and plural form while creating migrations. This is a Rails paradigm that you should follow each time you create a Migration.

How to edit the Code

Next, go to db/migrate subdirectory of your application and edit each file one by one using any simple text editor.

Here is how to modify 001_books.rb. The ID column will be created automatically, so don't do it here as well.

The self.up method is used when migrating to a new version and self.down for vice versa. That is, to roll back any changes if needed. At this moment, the above script will be used to create books table.

Modify 002_subjects.rb as follows −

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Ruby on Rails Topics