Similar question here, but none of the answers actually answer the question. The accepted answer just shows how to log in to console and destroy all records. This is not what I need in this case.
I need to completely scrap a single table (it has no associations) and recreate it with an existing migration.
Is there something like?
heroku pg:destroy_table "Products"
I would then run my migration again and it would create the table with the new schema:
heroku run rake db:migrate
All Heroku Postgres databases have a corresponding Heroku application. You can find the application name on the database page at data.heroku.com. Your database is attached to the Heroku app and is accessible via an app config var containing the database URL, even if you host no code in the application itself.
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
For those arriving here like me: Strongly consider NOT RUNNING DROP TABLE products
as suggested in the accepted answer. Consider using this instead:
heroku run rake db:migrate:down VERSION=20160518643350
VERSION is the timestamp on your migration file, i.e. the prefix of:
db/migrate/20160518643350_create_products.rb
Then, modify your schema (or create a new migration) and run it:
heroku run rake db:migrate:up VERSION=20160518643350
When you create a new PG database in Rails, a schema_migrations
table is also created to keep track of the migrations you've migrations. If you drop a table from the Heroku's Postgres console (heroku pg:psql
) your migrations file won't know about it and when you try to run the migration again with the new schema rails won't create the table since it thinks it's already there.
heroku run rake db:migrate:up ...
doesn't workIf you have already run heroku pg:psql
and dropped your table (DROP TABLE products;
) you may have issues creating the table from your migration since as explained above, Rails thinks the table is there. If that's the case, take the following steps:
products
tableheroku pg:psql
CREATE TABLE products(foo int);
Exit the psql
terminal.
heroku run rake db:migrate:down VERSION=20160518643350
This will drop your new table, and record the migration.
heroku run rake db:migrate:up VERSION=20160518643350
That should do it!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With