So at one point in the development process I added a migration to add a column to my users table. This column added a "dollars" attribute. After that I decided to remove the "dollars" attribute by writing a remove migration. I later accidentally deleted the initial migration to add the dollars, so to solve the errors I was getting when running db:migrate on my local SQLite database I also removed the RemoveDollarsFromUsers migration.
Seemingly that had solved the problem until I deployed to heroku. After reseting the database with heroku pg:reset DATABASE, I ran heroku run rake db:migrate and got an error saying that "dollars" was not a attribute of User. The log file showed that it had tried to run the RemoveDollarsFromUsers migration which I had previously deleted.
So I double checked that my repo was up to date and it was, and then I tried to completely reset and start a new app on heroku, thinking maybe heroku was remembering the old migrations. With a fresh heroku app I got the same error, even though the repo I am pushing should not have any reference at all to the old migration files.
Is this a git issue? the only thing I can think of is that git is pushing an old version with the RemoveDollarsFromUsers migration, otherwise how could heroku even know about it?
So migrations are recorded in the schema migrations table and that specifies which point to run from when you run rake db:migrate, and even db:reset and db:setup.
As you say, sometime it gets mixed up, especially if you change a migration LOWER down the chain. One simple way to get it to run the whole thing again is to delete the shchema_migrations table (though this can be risky if you are in a production environment and have data you want to keep). Running db:migrate will now go through all of them, step by step.
The easiest way to get back to base is to create a new migration and copy across db/schema.rb (making that it reflects what you want it to be). You can then delete the other migrations, drop the database and then run the new migration.
Make sure to backup anything you don't want to lose first though!
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