Rails Goodness : How to Test/Update Your Migration, Add a forgotten index
Once you have create a migration say,
And now lets say you need to add an index to it, below are the steps
Step1. Rollback the current migration
Step2 Add the Index to the migration manually
Step3. Test the migration by moving 10 steps behind in schema.rb and then redoing till current
The Steps can be any number, just to ensure robustness that schema.rb retains its current state
Thank you
class CreateEmployees < ActiveRecord::Migrationdef changecreate_table :employees do |t|t.string :first_namet.string :last_namet.string :icont.integer :emp_idt.timestampsendendend
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
Post a Comment