Question

I'm trying to run rake db:migrate on a migration I made to drop a few columns and add a few others. Here is the migration I'm trying to run:

     class Demographics < ActiveRecord::Migration
        def change
    change_table :demographics do |t|
    t.remove_column :demographics, :race
    t.remove_column :demographics, :other_race
    t.integer :race_id
    t.integer :other_race_id
    t.remove :demographics, :education
    t.integer :education_id

    t.remove :other_education
    t.integer :other_education_id

end
end
end

Here is the output of: rake db:migrate status --trace

    ** Invoke db:migrate (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Invoke db:load_config (first_time)
    ** Execute db:load_config
    ** Execute db:migrate
    ** Invoke db:_dump (first_time)
    ** Execute db:_dump
    ** Invoke db:schema:dump (first_time)
    ** Invoke environment 
    ** Invoke db:load_config 
    ** Execute db:schema:dump
    rake aborted!
    Don't know how to build task 'status'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/task_manager.rb:49:in `[]'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:148:in `invoke_task'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `each'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block in top_level'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:115:in `run_with_threads'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:100:in `top_level'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:78:in `block in run'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:165:in `standard_exception_handling'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:75:in `run'
    /var/lib/gems/2.0.0/gems/rake-10.1.1/bin/rake:33:in `<top (required)>'
    /usr/local/bin/rake:23:in `load'
    /usr/local/bin/rake:23:in `<main>'`enter code here`
Was it helpful?

Solution

You don't need status in the migrate command:

rake db:migrate

should be enough.

You are actually getting an error from rake because of having status in the command:

rake aborted!
    Don't know how to build task 'status'

EDIT

Following our discussion I now understand more.

The migration Demographics has already been applied to the database in the past. This is proved by its 14 digit datetime being included in the schema_migrations table.

Migrations are designed to be run once only. They make a change to the database (schema and/or data) and you move on.

When you run rake db:migrate it finds any migration not yet applied to the database (in datetime order from the migration filename), applies it, then puts an entry for the datatime in schema_migrations. Any migration which already has an entry in schema_migrations is ignored.

If you want to make a further change to the database - even to a table created in a previous migration - you should create a new migration, and then use bin rake db:migrate to apply it.

Yes you can backout a previously applied migration - rake db:rollback. Rollbacks will apply in the reverse order the migrations were applied, ie. working backwards from the end of schema_migrations. By default it will only back out the last migration, although you can use the STEP parameter to do more (rake db:rollback STEP=3 for example).

You can also redo a previously applied migraton - rake db:migrate:redo - again with optional STEP parameter. This is really just a shortcut for rollback followed by migrate.

RECOMMENDATION

My recommendation to you would be to put the Demographics migration file back to the way it was before you made the change. This is important in case the migrations need to be all reapplied in the future.

I would now create a new migration to apply the changes to your table:

rails generate migration UseForiegnKeysInDemographics

Make the required alterations; you only need mention the changes to the table:

e.g.

class UseForiegnKeysInDemographics < ActiveRecord::Migration
  def change
    remove_column :demographics, :race
    remove_column :demographics, :other_race
    remove_column :demographics, :education
    remove_column :demographics, :other_education

    add_column :demographics, :race_id,            :integer
    add_column :demographics, :other_race_id,      :integer
    add_column :demographics, :education_id,       :integer
    add_column :demographics, :other_education_id, :integer
  end
end

And apply this new migration:

rake db:migrate

I suggest you have a read of the Rails guide on migrations; http://guides.rubyonrails.org/migrations.html as it explains everything better than my brief summary.

Footnote: As you want to add foreign keys to the demographics table, you might want to consider add_reference instead of add_column. I didn't include this in my example above because I don't know the details of your other tables, but it's well documented in the Rails guide.

OTHER TIPS

I see an issue with your command : rake db:migrate status --trace

You are trying to achieve two things with one command i.e., run the migrations as well as check the status of the migrations with the detailed steps performed.

You can do it in two ways:

1. First Way

To perform the migrations use

rake db:migrate --trace

And then check the status of all your migrations within the application, use

rake db:migrate:status

2. Alternatively

rake db:migrate db:migrate:status --trace

You almost had that but status task is within db:migrate namespace so you need to use db:migrate:status. Here, the first db:migrate will perform the pending migrations whereas db:migrate:status will give the status of all migrations and --trace will give you full backtrace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top