Rails Goodness : How to Test/Update Your Migration, Add a forgotten index

Once you have create a migration say,

class CreateEmployees < ActiveRecord::Migration
def change
create_table :employees do |t|
t.string :first_name
t.string :last_name
t.string :icon
t.integer :emp_id
t.timestamps
end
end
end



And now lets say you need to add an index to it, below are the steps

Step1. Rollback the current migration

 RAILS_ENV=development rake db:rollback            

Step2 Add the Index to the migration manually

class CreateEmployees < ActiveRecord::Migration
def change
create_table :employees do |t|
t.string :first_name
t.string :last_name
t.string :icon
t.integer :emp_id

t.timestamps
end
add_index :employees, [:first_name]
end
end

Step3. Test the migration by moving 10 steps behind in schema.rb and then redoing till current




 RAILS_ENV=development rake db:migrate:redo STEP=10

The Steps can be any number, just to ensure robustness that schema.rb retains its current state

Thank you

Comments

Popular posts from this blog

Apache Airflow Wait Between Tasks

Hibernate Interview Questions

Java Spring Interview Questions