8

I am using Ruby on Rails and I no longer need my table Order so I deleted it using SQLite manager.. How can I make the table deletion take place in heroku?

EDIT I am getting the error

db/migrate/20110806052256_droptableorders.rb:10: syntax error, unexpected keyword_end, expecting $end

When i run the command

class DropTableOrder < ActiveRecord::Migration
  self.up
      drop_table :orders
  end

  self.down
      raise IrreversibleMigration
  end
end
Jake
  • 91
  • 1
  • 3

3 Answers3

22

In case you don't want to create a migration to drop table and cant rollback the previous migrations because you don't want to lose the data in the tables created after that migration, you could use following commands on heroku console to drop a table:

$ heroku console
Ruby console for heroku-project-name
>> ActiveRecord::Migration.drop_table(:orders)

Above command will drop the table from your heroku database. You can use other methods like create_table, add_column, add_index etc. in the ActiveRecord::Migration module to manipulate database without creating and running a migration. But be warned that this will leave a mess behind in the schema_migrations table created by Rails for managing migration versions.

This could only be useful if your application is still under development and you don't want to lose the data you have added on remote staging server on heroku.

Zeeshan
  • 3,462
  • 2
  • 25
  • 28
  • 1
    I found this useful when I was in dev and was pushing/pulling db and somehow futzed it up & migrations were dying. This command worked like a charm, thanks! – rtfminc Nov 27 '11 at 07:19
6

Execute following command.Here 'abc' is app name

heroku run console --app abc

Then use,

ActiveRecord::Migration.drop_table(:orders)

It will drop the table 'order'.

Thaha kp
  • 3,689
  • 1
  • 26
  • 25
3

Just create a migration like this:

def self.up
    drop_table :orders
end

def self.down
    # whatever you need to recreate the table or
    # raise IrreversibleMigration
    # if you want this to be irreversible.
end

and then do a heroku rake db:migrate the next time you push your changes.

You might want to recreate the table in SQLite so that you can run this migration locally as well.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • I'm getting an error. == DropTablesMigration: migrating ============================================ -- drop_table(:order) rake aborted! An error has occurred, this and all later migrations canceled: PGError: ERROR: table "order" does not exist : DROP TABLE "order". But my table order still does exist – Jake Aug 06 '11 at 05:18
  • @Jake: Sorry, typo, it should be `:orders` not `:order`. – mu is too short Aug 06 '11 at 05:19
  • i recreated the table `order` on my local server and tried to delete it using your command. can you please check back up at the top for the edit i made – Jake Aug 06 '11 at 05:29
  • @Jake: Sorry (again), I am full of typos tonight it seems. Missing `def` added. – mu is too short Aug 06 '11 at 05:35
  • i don't know if it matters but I'm using rails 3.1 – Jake Aug 06 '11 at 05:35
  • 2
    @Jake: No, 3.1 has nothing to do with my brain and fingers not communicating with each other :) – mu is too short Aug 06 '11 at 05:39