Scenario:
Let’s say we are having 10 migration files in our application. Our DB is already set as per our migrations. Now let’s say we got a requirement to add a new column to an existing table. Literally we need to update our migration file and synchronize the same with database.
Description:
We are having following migration file in project/db/migrate/20130830131215_create_tips.rb
class CreateTips < ActiveRecord::Migration def self.up create_table :tips do |t| t.string :title t.string :description t.timestamps end end def self.down drop_table :tips end
We got requirement to add a new field “Tag of String type” to our application ( add Tag to our table tips).
We can update the same in our existing migration file : (Just update the create_table section)
create table :tips do |t| t.string :title t.string :description t.string :tag # Here goes our new column t.timestamps end
Now we need to make our Synchronized with this change in Migration. So let’s run migration file specific command.
rake db:migrate:up VERSION=20130830131215
Above command will run a specific migration file and update our DB.
Points to keep in Mind : 1. DON’T EVER give any spaces between this VERSION, = & timestamp. 2. If you wish to remove table / go for the down case, then change it to db:migrate:down 3. This version is the timestamp at which this migration file was created
(this long timestamp is attached to our migration file to make it unqiue which will avoid file conflicting )