31
votes

I made a new Rails 3 app today, added a simple migration, and for some reason, nothing happens when I do rake db:migrate. It simply pauses a few seconds, then returns to the command prompt, with no errors or anything. Schema.rb and the database stay empty.

Any ideas what could be going on? I've made many apps and never had this problem. Everything is a totally standard setup too.

5
Have you tried rake db:migrate --trace? What was the output? Also have you tried doing rake db:rollback; rake db:migrate?Jakub Hampl
What Jakub is getting at is that perhaps it was already run. Following his instructions should at least give you an indication that your migration was properly run. Also, you could search for your migration inside your file structure as well. It would be inside application_name/db/migrate.Tass
The output of rake db:migrate --trace was: ** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:migrate ** Invoke db:schema:dump (first_time) ** Invoke environment ** Execute db:schema:dump and rollback, then migrate do nothing either. When I try to migrate with an explicit version, it says the migration doesn't exist. But it's definitely in the standard location, db/migrate.Jack Hoge

5 Answers

67
votes

There's a few reasons why your migrations won't run, but the most common is that the system is already under the impression that all the migrations you've defined have already run.

Each migration creates an entry in the schema_migrations table with the version column corresponding to the identifier number. If you want to force a migration to re-run you can usually back it out and retry it. For example, if you had 20100421175455_create_things.rb then you would re-run it using:

rake db:migrate:redo VERSION=20100421175455

A common situation is that your migration has failed to run in the first place, that it generated an exception for instance, and yet Rails still considers it complete. To forcibly re-run a migration, delete the corresponding record from the schema_migrations table and run rake db:migrate again.

One way to avoid this kind of problem in the future is to define your migrations with an automatic back-out procedure:

class CreateThings < ActiveRecord::Migration
  def self.up
    # ... (migration) ...

  rescue
    # If an exception occurs, back out of this migration, but ignore any
    # exceptions generated there. Do the best you can.
    self.down rescue nil

    # Re-raise this exception for diagnostic purposes.
    raise
  end
end

If you have a mistake in your migration you will see the exception listed on the console. Since the migration has automatically been rolled back you should be able to run it again and again until you get it right.

1
votes

Calling spring stop might solve your problems.

0
votes

Well, I found out what was causing my problem. I'm using the slim_scrooge gem and commenting it out makes everything proceed normally. Don't know why though...

0
votes

I faced the same problem. I did a kind of a short hack that helped me. I am posting it just in case anyone wants a short and sweet solution. I agree with what Tadman is saying

"the system is already under the impression that all the migrations you've defined have already run"

What I did was to change the name of the migrate file in the /app_folder/db/migrate folder. I think the numeric part in the name of the ruby migrate file is the time at which the file was created.

You can add say 1 , to the filename, every time you want to re-run the migrate. After changing the name drop/delete the table (I used mysql command line tool for deleting) and then run rake db:migrate and the migrations should be done.

0
votes

I faced similar problem today while migrating plugin for Redmine using

rake redmine:plugins:migrate RAILS_ENV=production NAME=plugin_name

where plugin_name is actually plugin name defined in init.rb of the plugin.

I struggled for 4 hours and finally figured out that my plugin directory name was not the same as the plugin name (note redmine_ prefix):

~/redmine/plugins/redmine_plugin_name

So, make sure your plugin is placed in folder named after plugin name. I believe it applies to other rails apps as well.