user3618929
user3618929

Reputation: 143

rake db:migrate ERROR for "Table already exists"

I use capistrano 3.1 to deploy to prod server.

Previous, it works fine. But today, when I change a rb file (no database migration added & changed), during the deploy procedure, when it runs to

Running /usr/local/rvm/bin/rvm default do bundle exec rake db:migrate on xxx

it show error:

[cc1734f0]  Migrating to CreateDevices (20140416233606)
DEBUG [cc1734f0]    == 20140416233606 CreateDevices: migrating ====================================
DEBUG [cc1734f0]    
DEBUG [cc1734f0]    -- create_table(:devices)
DEBUG [cc1734f0]    
DEBUG [cc1734f0]    Mysql2::Error: Table 'devices' already exists: CREATE TABLE `devices` (`id` int(11) auto_increment PRIMARY KEY, `soh_id` varchar(255), `token_id` varchar(255), `device_hw` varchar(255), `device_sw` varchar(255), `device_name` varchar(255), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
 DEBUG [cc1734f0]   rake aborted!

But in my schema.rb, I have:

ActiveRecord::Schema.define(version: 20140513035458) do

Because my last deploy already create the table, so now it reports error.

So why the rake db:migrate run the migration, which has been executed before again !

I can NOT drop the table, for it contains data already.

Upvotes: 1

Views: 3351

Answers (2)

Eyeslandic
Eyeslandic

Reputation: 14890

One way to solve this would be to add '20140416233606' to the schema_migration table. Then Rails won't run this migration.

Upvotes: 1

blotto
blotto

Reputation: 3407

Update your migration to conditionally execute if the table does not already exist

 class MyMigration < ActiveRecord::Migration
   def change
     unless table_exists? :devices
      #create the table
     end
  end
 end

Upvotes: 4

Related Questions