Rails shows all migrations as pending - ruby-on-rails

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.

Related

Error when running bin/rails test in Vagrant

I'm running the rails-dev-box on Vagrant, with a folder shared between the box and my Windows computer. Rails version 5.0.5. I have a very basic app using a sqlite3 database, and a basic generated scaffold for a model. When I ran bin/rails test I received this error:
ActiveRecord::Tasks::DatabaseAlreadyExists
A link in this GitHub thread pointed to this SO question, and I followed this answer - I edited database.yml to change the location of the databases to a location outside of the shared folder. I then re-migrated the databases with bin/rails db:migrate. This seemed to help a bit, because the next time I ran bin/rails test I received a different error:
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
But even after running bin/rails db:migrate RAILS_ENV=test I still receive this error each time I try to run the test.
I think all you need is bin/rails db:test:prepare before bin/rails test

Migration Errors on Heroku

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?

Rake Migrate will not work on developement

I'm following along with some tutorials and they are teaching how to create and run migrations. All has been working so far but I have run in to a problem. But now when i have come back to it the migration will not work in the development database. I used rake db:migrate RAILS_ENV=production to see if my migrations work and on that database it can go through all the migrations and back down again with no problems are errors but when I run rake db:migrate RAILS_ENV=development it pauses for a few seconds then returns me to the console awaiting a command. No errors are shown or nothing is returned to show what it has done and when checking the DB nothing has changed. What can this be?
Use --trace
rake db:migrate --trace
Show your database.yml file please.

After heroku run rake db:migrate (cedar stack) - migration not happening

I'm trying to run heroku run rake db:migrate, but I get the following:
Running rake db:migrate attached to terminal... up, run.7
And that's it - Migration is not happening!
Although I have set up everything alright - development as sqlite3 and production as pg.
And previously all the migrations worked just fine. (rails 3.1. app )
I'm not sure what is happening here.
Here are the logs:
←[35m2012-01-08T11:25:22+00:00 app[run.8]:←[0m Awaiting client
←[35m2012-01-08T11:25:22+00:00 app[run.8]:←[0m Starting process with command `bu
ndle exec rake db:migrate`
←[35m2012-01-08T11:25:22+00:00 heroku[run.8]:←[0m State changed from starting to
up
←[35m2012-01-08T11:25:35+00:00 heroku[run.8]:←[0m State changed from up to compl
ete
←[35m2012-01-08T11:25:35+00:00 heroku[run.8]:←[0m Process exited
I don't see any errors.
Has anybody came across with something similar ?
Thanks!
The reason for this was simply because the migration has already happened automatically after pushing the updated app to heroku.

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