Heroku Drop Table Rails Help

How to drop a single Postgres table in a Ruby on Rails Heroku app?

You could try to use heroku pg:psql. This should open a console to your database where you can execute arbitrary SQL:

DROP TABLE products;

You can find more about pg:psql in the heroku docs: https://devcenter.heroku.com/articles/heroku-postgresql#pgpsql

PostgreSQL docs for the same: http://www.postgresql.org/docs/9.1/static/sql-droptable.html

Deal with a drop_table migration for nonexistent table on heroku

Just delete the offending migration and get on with more interesting things. Clearly there's no "create table" migration for that table so the "drop table" migration has no right to exist. If you really don't want to delete the migration, you can rewrite it using raw SQL:

def up
connection.execute 'drop table if exists campaigns'
end

The if exists does exactly what you'd think: it only tries to drop the table if it is there.

About this stuff:

I thought you weren't supposed to delete migrations so that anyone else that works on the project in the future can run them and have an accurate DB.

Migrations are meant to get an existing instance of an application from state-A to state-B. If you're starting a fresh instance, you'll be using schema.rb (or structure.sql) to initialize the application and no migrations should be needed. Also, once a migration has been applied to every instance of an application, that migration really isn't needed anymore unless you want to try and go backwards. I tend to clear out old migrations a couple weeks after a release to keep the clutter down.

Rails/Heroku - Dropped table and can't get it back

First off, you shouldn't go back into old migrations and manually change them. Just do a new migration with the changes defined there. Read up on migrations here: http://guides.rubyonrails.org/migrations.html

This is done so you don't run into the problem you are currently having. It's also bad practice and if you were working in a group there would be complete mayhem. It's hard to tell exactly what the differences are between your local machine and heroku and what you've changed. My suggestion would be to roll back both your local machine and heroku and do a new migration with your changes.

I'm assuming you are using git so you can go back to a previous version before the change to the create_comments_table on your local machine. You can do the same in Heroku at

https://dashboard.heroku.com/apps/nameofyourapp/activity

In your dashboard you can click on your app and go to the activity tab. There heroku maintains a record of the changes you've pushed. You can rollback to another previous change by hovering over the version.

Now that you have basically restart your app locally and on heroku, you can define a migration with what you want to change in the comments table.

You also might be interested in the PG Backup add-on. Then you won't be as worried about losing data. https://devcenter.heroku.com/articles/pgbackups

Drop and Recreate a single table (on Heroku)

To empty a table on Heroku without changing the schema, in your application's directory:

$ heroku run console
Ruby console for myap.heroku.com
>> ModelName.delete_all
>> exit

Heroku - how to remove table and run migration again?

you can redo a single migration file like this:

heroku run rake db:migrate:redo VERSION=20130311054546

where 20130311054546 is the timestamp of your migration.

To do different database modifications, such as drop table etc., I usually use David Dollar's heroku SQL console

What is the best way to drop a table & remove a model in Rails 3?

The second method is the ideal way to handle this: your migration files are meant to represent how your database has changed over time. The older migration files will remain in your project (in case, hypothetically, you wanted to roll back to an older version), but Rails will not run them when you rake db:migrate because it knows they've already been run (based on data in the database's schema_migrations table).

Your schema.rb will just be updated once to reflect that your database no longer contains that table.

One minor tweak to your code: your migration file should drop the table in the up method, and ideally recreate it in the down method. The "up" signifies that your migration drops the table to move forward in time, and if the migration is rolled back, the down method will be run.

Heroku/Rails: PG:: Undefined Table: error [tablename] does not exist on heroku rails migration

There might be a problem in your migration files. If you deleted one but another corresponding file remains, it can cause this error. I would have commented but I don't have the reputation yet.

Rails DB Migration - How To Drop a Table?

You won't always be able to simply generate the migration to already have the code you want. You can create an empty migration and then populate it with the code you need.

You can find information about how to accomplish different tasks in a migration here:

http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

More specifically, you can see how to drop a table using the following approach:

drop_table :table_name


Related Topics



Leave a reply



Submit