Jason
Jason

Reputation: 3360

Deleted migrations and Heroku

A few days ago I created a migration for rails to add Paperclip avatars, but ended up going a different direction. Being new to Rails, I didn't know how bad of an idea it was to delete the migration file like I did.

My app works fine locally, but when run heroku run rake db:migrate I get this:

undefined method `attachment' for #<ActiveRecord::ConnectionAdapters::Table:0x000000046092e0>

It is because it is trying to run a migration called AddAttachmentAvatarToVenues, which is the migration I stupidly deleted.

It was also adding columns for the avatars that were specified in the deleted migration to the schema.rb, but I created a new migration to get rid of these. The new migration got rid of them but didn't change the heroku migration error.

Any idea how to fix this? I've done lots of googling and looking around SO and while there a lot of people with similar errors they are mostly problems with the commands that they're using.

Here is the output after the deleted migration attempted in my heroku migration.

==  AddAttachmentAvatarToVenues: migrating ====================================
-- change_table(:venues)
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `attachment' for #<ActiveRecord::ConnectionAdapters::Table:0x00000003bdb7c8>
/app/db/migrate/20130206222434_add_attachment_avatar_to_venues.rb:4:in `block in up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/schema_statements.rb:243:in `change_table'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:466:in `block in method_missing'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:438:in `block in say_with_time'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:438:in `say_with_time'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:458:in `method_missing'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:334:in `method_missing'
/app/db/migrate/20130206222434_add_attachment_avatar_to_venues.rb:3:in `up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:370:in `up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:410:in `block in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:389:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:528:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `call'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `ddl_transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:719:in `block in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `each'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:570:in `up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:551:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

I do see what's wrong with the output, I'm just not sure how to fix it without messing it up more.

EDIT: Here's some screenshots of file structure:

git

(The two similarly named ones are because there was a column I forgot to remove, but this was after I was having this problem and didn't affect it either way)

structure

EDIT2:

Here's the deleted migration from my git history. I had added some more channels after this. They were just a couple of strings, but if that could make a difference I'll find a newer version.

class AddAttachmentAvatarToVenues < ActiveRecord::Migration
  def self.up
    change_table :venues do |t|
      t.attachment :avatar
    end
  end

  def self.down
    drop_attached_file :venues, :avatar
  end
end

Thanks in advance!

Upvotes: 1

Views: 2225

Answers (1)

Peter de Ridder
Peter de Ridder

Reputation: 2399

Perhaps you should look into this: How to empty DB in heroku

All your normal commands are also available in heroku, the only difference is that you have to put heroku run in front of it.

If your application hasn't gone live yet you could simply reset the database:

heroku pg:reset SHARED_DATABASE --confirm NAME_OF_THE_APP

And recreate it, using:

heroku run rake db:migrate

To seed the database:

heroku run rake db:seed

And lastly, to restart Heroku:

heroku restart

P.S. If these steps don't help, you could try running "heroku run rake db:setup" after dropping the database

Upvotes: 2

Related Questions