Ruby on Rails Database Migration

Implementing Database Migration on Rails

Ruby on Rails is often perceived as a framework for developing RIA (Rich Internet Applications). Because of its ability to harness Ajax and properly implementing in different browsers, online applications built with the help of Ruby in Rails are common.

However, Rails offers more than just ease of developing an application. One of the main reasons why Rails is very popular among developers is its highly efficient ability in dealing with database management.

The database management of Rails is run through one of its forms – ActiveRecord. Inside this framework are the functions that you would need to effectively run database management in Rails.

When working with ActiveRecord, you will most likely be dealing with migration. In fact, this might be the only function you will be only working with if your database is already set and configured.

When you are actively working on your website with Rails, you will need to work with migration if you are application is data intensive.

Migration is actually one of the most popular features of Rails in relation to database management. It is simple enough to be understood and easy enough to work with so that database will be properly updated.

Rails Migration Features

Ease of Configuration – True to the design philosophy of Rails, migration will not require a lot of manual coding. Developers just have to work with an abstract and everything will have to be in place when you run migration.

Of course, before you fully migrate data from one source, you would need to create a migration instance wherein the data would be transferred. But this is actually a lot easier compared to other database application with migration features. Again, it bases itself on abstraction rather than strict coding.

Full Environment Included – Aside from implementation of ActiveRecord, Rails will be fully executed when you try to run migration. This means that everything that will happen in your application will be fully integrated with the data you just migrated. With the help of their scaffolding method, you could even see the actual changes implemented in your application.

Autonomy from Database – Instead of tying itself to a single database, migration on rails could work effectively in most database applications. The same behavior of the application should be expected in most database management applications.

The only exception is that when you want to fully integrated MySQL in migration which will require more configuration and non-abstract coding.

    rake db:migrate

The command rake db:migrate is the most popular command in database migration for Rails. This command would mean you will update the database as you have new data to upload. When everything has been set up, this command should immediately synch files.

However, getting to this command is not as easy as it looks. Although the main advantage of migration in Rails is that it can easily work without extensive configuration with another database, developers still needs to set up the tables, rows and other database functions where the new data will be transferred.

Of course this will only be set up for the first time and no changes will be made unless you make some changes in the application or the database itself.

When you do make some changes, you just have to update your database in Rails before you can fully establish another migration.

Establishing Changes in Rails

Rails understand that making a single mistake in migration should never jeopardize the entire operation. For this reason, Rails could implement two functions: up and down. The former function refers to upgrading your database based on the changes you want to make while the latter will help you revert or go back to the original configuration before you opted to make some changes.

Aside from simple changes; remove, change, add, rename, drop and create are additional functions that you could implement in your database. When this is integrated with the up and down functions, you should be able to actively implement any changes at all. For example, you can create a table with this command:

    create_table

However, after establishing this command you realize that you do not actually need to have another table but just make some changes on certain commands. You just have to implement the “down” command to restart. These are very simple commands that will enable efficient implementation of migration.

Editorial Team at Geekinterview is a team of HR and Career Advice members led by Chandra Vennapoosa.

Editorial Team – who has written posts on Online Learning.


Pin It