Pregunta

I have screwed up my migrations/schema/db to the point that I can't seem to figure this out.

Locally, I had a comments table. I needed to restructure this table completely, so I opened up my initial "create comments table" migration and altered it manually. I then altered my schema.rb file to match the migration. I then ran rake db:reset and rebuilt my database, and rake db:migrate to finish it off.

This worked seemingly well on my local dev environment. When I pushed to heroku, however, I ran heroku pg:psql and dropped my comments table. I then ran heroku run rake db:migrate thinking it would rebuild the comments table - It did not. The comments table no longer exists, and I can't figure out how to recreate it. I don't have anything important in my heroku database EXCEPT for the users table, so I can't reset the entire thing.

How can I recreate that table, and make sure it matches the migration file I pushed to heroku?

¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top