DEV Community

Cover image for Ruby Migration.
Mazin Idris
Mazin Idris

Posted on

Ruby Migration.

What’s a Rails migration?
A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.

Migrations are additive. Each one represents a new version of your database schema. Rails applications can evolve, and publishing a new migration with a new release of your application isn’t unusual.

Migrations provide lot of benefits, including:

  • When working on a large project with different team members, it gets easier to keep track of all the changes that the schema goes through.
  • Migrations make it easier to rollback to an older schema version, undoing specific migrations.
  • Rails migrations make changes more readable and easier to understand.
  • Each developer will know whenever a schema is updated, and they can use migrations to speed up to the latest version.

How to create a migration:

rake db:create_migration NAME=maz
Enter fullscreen mode Exit fullscreen mode

or

bundle exec rake db:create_migration NAME=maz
Enter fullscreen mode Exit fullscreen mode

Migration table:

class CreateMaz < ActiveRecord::Migration[6.1]
  def change
    create_table :Maz do |t|
      t.string :name
      t.string :address
      t.string :img_url
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Rolling back migrations

In case a migration has been executed and is now a part of the application’s database schema, it can always be rolled back or reverted to a state prior to when that particular migration altered the schema.

rails db:rollback STEP=1
Enter fullscreen mode Exit fullscreen mode

The command shown above will undo the most recently executed migration. The STEP=1 portion of the command makes sure that only the most recent migration needs to be rolled back. In case we want more than one migration to be rolled back, we can change the number of the STEP flag. For example, STEP=3 would roll back the three most recent executed migrations.

Add a column
One of the most common changes in an app is adding a field to an existing object. So adding a column to a database is a typical migration. If you’re working with Active Records, Rails will create the migration for you.

You can use all of the Rails basic data types with migrations, and it’ll be matched to the corresponding type in the database you migrate to. Here’s a list of data types:

  • string
  • text
  • integer
  • bigint
  • float
  • decimal
  • numeric
  • datetime
  • time
  • date
  • timestamp
  • binary
  • primary_key
  • boolean

You can also specify a database-specific data type for a column, but this may cause problems if you try to migrate your application to a new platform.

Conclusion:

Rails migration helps interacting with the Database easier.

By taking advantages of this layer, the app can perform very fast, hence we may consider to understand Rails migration and Database well.

No SQL Database and reactive programming also open new ways to fix some missing features of SQL database. With new technologies, it may outdate? the migration layer in Rails.

Top comments (0)