3

I suddenly met a strange error. When i try to run rake spec i recieve:

You have 2 pending migrations:
  20130405105004 CreateReports
  20130405113839 AddDocumentToReports

I don't know the reason for that (i run migrations in the past so i have data in the database and schema.rb).

Here is rake spec --trace:

** Invoke spec (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
You have 2 pending migrations:
  20130405105004 CreateReports
  20130405113839 AddDocumentToReports
Run "rake db:migrate" to update your database then try again.

Here is rake db:migrate:status:

   ...
   up     20121210112419  Create simple captcha data
   up     20130214110545  Add weeknum to alerts
  down    20130405105004  Create reports
  down    20130405113839  Add document to reports
   up     20121018133601  *** NO FILE ***
   up     20121018163051  *** NO FILE ***
   up     20121024124111  *** NO FILE ***

And here is rake db:migrate

==  CreateReports: migrating ==================================================
-- create_table(:reports)
rake aborted!
An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'reports' already exists: CREATE TABLE `reports` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `user_id` int(11), `ready_status` tinyint(1) DEFAULT 0, `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB

Tasks: TOP => db:migrate

How can i fix that?

ExiRe
  • 4,727
  • 7
  • 47
  • 92
  • just tricky but definitely work you can first go to previous version where you added this table by 'rake db:migrate version=xxx' and then again migrate it. – shrikant1712 Apr 09 '13 at 13:29

4 Answers4

10

If this is on a dev environment and you have no important data in the db, then run

bundle exec rake db:migrate:reset

This will rebuild the schema, but at the cost of nuking all your data.

Reck
  • 7,966
  • 2
  • 20
  • 24
0

I was able to get everything working by rebuilding the schema with Recks answer and with the code

bundle exec rake db:migrate:reset
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Igor
  • 3
  • 2
0

There is a schema_migrations table created w/I your postgres database, you can reset the migration status by dropping that table (drop table schema_migrations;).

Peter O.
  • 32,158
  • 14
  • 82
  • 96
unboundev
  • 357
  • 1
  • 11
-1

I had a similar problem with * No File *. I did a rake db:migrate:status and found that rake was looking for some old migrations that I had saved in another folder under the migrate folder. So, I deleted the old migrations and schema.rb. Then I did rake db:drop, rake db:create, and rake db:migrate. That solved the problem.