Migration Errors on Heroku - ruby-on-rails

I have a Rails app which I have recently pushed up to Heroku. When I try to migrate the database with:
heroku run rake db:migrate
I am getting the rake aborted! message and complains about a table that doesn't exist.
Locally, when I run the migrate command there are no issues so why is Heroku different?
I did experience this issue the last time I migrated on Heroku too - I ended up using:
heroku rake db:schema:load
to overcome the problem but I really want to get to the root of the problem so it stops happening.
Any suggestions?
EDIT: I know which migration file is at fault but is it safe to remove a file from the migrations folder?

Related

Local sql database and heroku postgres database files differ, how to sync again? Ruby on Rails

I am currently doing the Ruby on Rails tutorial by Michael Hartl. Somewhere along the way I messed up my database. In my database file there is only 1 user, by the name of Bob.
Locally in cloud9 IDE, when I do 'rails console' and then do Users.first, I get a user with a name of "Bob".
However when I do 'heroku rails run console' and do Users.first, I get a user with a different name. (I probably changed the name somewhere along the way)
How do I get Heroku to see the correct local database file again? Should I clear the heroku database, then use pg:pull to pull the local sql database to heroku?
Not sure if you've gotten to Chapter 9 yet but section 9.3.2 of the current book deals with creating sample users. This is done through the db/seeds.rb file.
Running $ bundle exec rake db:reset then resets your DB followed by $ bundle exec rake db:seed to fill it with your new data.
You can run the same procedure on your production application with:
heroku pg:reset DATABASE
heroku run rake db:migrate
heroku run rake db:seed
It is, of course, also possible to transfer data between local and production databases with tools such as heroku-pg-transfer but that's a little advanced if you're only starting out, and I think somewhat unnecessary if you only have one user to transfer over.
Hope this helps.
You can use yaml_db gem to dump your local data to file and then upload it to heroku.
On your development machine:
rake db:data:dump
Then commit changes, push to heroku and run:
heroku run rake db:data:load

Rails shows all migrations as pending

I have a Ruby on Rails application that is successfully running on Heroku.
I did some experimentation with RSpec and Cucumber, but at some point running the server locally failed with the message
Migrations are pending. To resolve this issue, run: bin/rake
db:migrate RAILS_ENV=development
I ran rake db:migrate:status and literally all 50 or so of my migrations are marked as "down", even ones created months ago:
When I try to run the command it gives me, it gives me
PG::DuplicateTable: ERROR: relation "artists" already exists
which is fair enough since it does, it's an old migration. So the problem is that it's not detecting that these migrations were already run.
Here's what I've tried:
A git reset to the last working state (my heroku instance)
Restarting Postgres, the rails server and my Mac
Running rake db:reset, as well as rake db:drop db:create db:migrate
None of which worked. I'm desperate to get my app back into a state where I can continue development on it.

Heroku: How to push seeds.rb to existing rails app?

I store all my app's data in seeds.rb locally. However, after I pushing everything to Heroku, the app works well, but without any data. I do not want to re-input the mass data again, so does anyone have any ways to help me?
If you push the app to heroku, you can seed the database with the following command.
heroku run rake db:seed
If you have changed migrations then first you need to do is run migration
heroku run rake db:migrate
then
heroku run rake db:seed
If you don't have any data in database the I would suggest following is, But caution it will remove all current data from heroku database.
heroku run rake db:setup
Hope this helps you
you have to be sure no migration is pending
if no pending
just do
heroku run rake db:migrate
and it will word perfectly
I'll just add, because I haven't seen it yet.
heroku run rails db:seed
Will accomplish the same task.

Migrate Data and schema from development to production rails heroku

I have facing problems with migrating data to my heroku app which has Postgresql as database for my hosted site(Production). At my development site i have rails 3.2.13 with Sqlite3 as database. I have followed Ruby on rails Tutorial by Michael Hartl
i have used git push heroku to update my site at heroku. i also want to update database along with data. But heroku run rake db:migrate migrates schema not data. I tried db:push to push data to heroku but i get error
dependency.rb in 'to_specs' :Could not find sequel (~) 3.20.0
also i have searched and found that i should first my sqlite data to dump.sql and then run
heroku pg:psql HEROKU_POSTGRESQL_COLOR --app app_name < file.sql as answered in https://stackoverflow.com/questions/15371394/...
but it failed with
the local psql command could not be located
please tell me what i am doing wrong. or what is the right way to update heroku postgresql with my development sqlite3 data.
Thanks in Advance
It is not a good idea to fill the production database with the data that you have now in the developement database. Because, if you have problems with your production database in the future, and you need to refill it again, your development db may changed (e.g dropped), and you are not going to be able to do it again.
For this need, Rails provides seeds in db/seeds.rb file. You should create all the neccessary objects there.
Then when you push your code to Heroku, Heroku is going to prepare the database, create the schema, and seed it. If you need to seed the db manually, you can run bundle exec rake db:seed, if you want to run it in Heroku: heroku run bundle exec rake db:migrate

Rake error fixed by bundle exec, but deployment not working

I pushed an update to my Rails app production server, and in the update there was a new database migration. I ran rake db:migrate and got the common error seen here. I ran the rake again in bundle exec bash and it was successful. But after restarting my apache server, I'm now getting the 500 Error page. This update worked fine on my localhost, and was mostly this update to the db with supporting changes in the according view and controller/routing.
I don't even know why this error appeared this time, as I have pushed db updates successfully before using only rake. Nonetheless, the rake was successful. The 500 error page only shows on pages that require that specific new ActiveRecord. Any ideas on how to debug?
EDIT: My problem was an extremely simple one. I merely forgot to include the environment with the rake:
bundle exec rake db:migrate RAILS_ENV=production
Unfortunately, it took quite a while to narrow that down, as I couldn't use IRB to check the db entries until I followed these steps.
Did you run rake db:migrate on your server? Also be sure to set the RAILS_ENV flag so your production database is updated:
rake db:migrate RAILS_ENV=production

Resources