emeraldhieu
emeraldhieu

Reputation: 9439

Restore a migration after manually deleting it in database

I have a migration named "20120311145341_create_resource1s.rb". I accidently deleted that table in database. I ran "rake db:migrate" but the it doesn't restore that table. How to restore it?

Upvotes: 1

Views: 3145

Answers (1)

Zheileman
Zheileman

Reputation: 2559

You can't run db:migrate again because I think you're already on the latest migration.

a) If the migration you're trying to recreate is the latest one, you can do the following to rollback it, and then run it again.

rake db:rollback
rake db:migrate

b) If it's not the latest migration, you need to list the files in db/migrate directory, annotate the version of the previous migration to 20120311145341_create_resource1s.rb and then run:

rake db:migrate VERSION=20120309101821 (change the version to right one for you)
rake db:migrate

Please note, if you get an error like this one Mysql2::Error: Unknown table 'xxx': DROP TABLE 'xxx' you can workaround it making sure you check for the table existence in the migration down method:

  def self.down
    drop_table :xxx if ActiveRecord::Base.connection.table_exists? 'xxx'
  end

I hope it helps.

Upvotes: 7

Related Questions