I was having issues deploying my Heroku Rails app - and came across another SO post that suggested using:
heroku run rake db:reset
Which I ran without thinking, and of course my database was dropped and setup... and I no longer have any data on my heroku app.
Is there an easy way to get my database info back? I tried running
heroku run rake db:rollback
but haven't had any luck yet.
Anyone know of any easy solution to this? I kind of need this data, and most definitely cannot lose it! Thanks for any help you can offer!
rake db:reset
Can not be undone unless you have a database backup. If you found a database backup use the following to restore your database.
psql dbname < infile
Tips:
1. rake db:rollback does not get your database back. Instead, it rollbacks the last migration in your schema_migrations table.
2. Always create database backups for your production applications.
3. Read this answer it may come in handy.
Related
I have a ruby on rails application already running in production. The database has records which I do not want to loose. I had to add and run new migrations to add some new columns to existing tables. The migrations run successfully and the schema.rb file reflects the changes but the changes do not appear in the database or existing table structure.
Based on research online, rake db:schema:load updates the db based on the schema.rb file. But this resets the database.
It is crucial that I do not loose the data in the tables.Is there any way to solve this? I am fairly new to ruby on rails.
I was able to fix it. I checked the status of migrations in the production environment rake db:migrate:status RAILS_ENV=production and realised they were down. I then run rake db:migrate RAILS_ENV=production and that did it. Changes now reflect in the db. Thanks #muistooshort for the nudge in the right direction
How do I undo bundle exec rake db:setup?
I ran it in the wrong rails app. I ran it in the blogger when I should have ran it in the blogger_advanced app.
You can do rake db:drop
.
It will drop all tables (thats any tables created by setup, any migrations run by setup and any seeds created by setup)
You may use rake db:rollback and it'll rollback migrations one by one, but if yours apps have same named models (User for example) it'll be lost. So you need to dump this tables first and recreate them later using database administrator tool.
And also it'll broke your schema_migrations table and you'll need to refill it by hands from your migration file names.
So if there is not much useful info, it's better to use #Alexphys approach.
I wrote a Rails app locally and have maybe 10-15 migrations written. This all works fine locally.
When I wanted to deploy on heroku, I ran into a problem because they are using a earlier version of PostGreSQL than what I was using locally. One of my earliest migrations is failing because of a missing DB function in one of my database views.
I found out a way to hack around the DB function issue, but now I'm stuck because I can't write a new migration that changes the view to use the hack, since the rake db:migrate will abort after it hits the original problematic view creation.
What can I do to solve this?
First of all drop your db:
heroku pg:reset
Then run your new migrations:
heroku rake db:migrate
We need constantly update our database schema in production for rails 3.1.3 app. The first db schema was created with the following rails command:
$rake RAILS_ENV=production db:schema:load
Question is: can we still use the command above to update db schema in production while safely retaining all current data?
Thanks so much.
I never used rake db:schema:load in production, but according to this answer to another question here on SO, I don't think you want to do that.
On the other hand, I have used RAILS_ENV=production rake db:migrate several times on the server with data already in the database and never experienced any problems.
After rake db:migrate:rollback STEP=1, rake db:migrate:reset, rake db:migrate:setup, rake db:migrate:up VERSION=XXXXXXXXX I get the following entry:
Status Migration ID Migration Name
------------------------------------------------------
up 0 *********NO FILE**********
up 20120209023430 Create tasks
How can I get rid of the orphaned entry? I have encountered this problem a few times after raking the db similar to above. Could somebody please explain what exactly causes this.
Thx in advance.
Shahram
You could use rake db:migrate:reset db:seed. It's a little less verbose and will completely wipe your database, re-run all migrations, and then seed your database from the db/seeds.rb file.
I've just released this gem that can solve this issue for good.
The idea of that gem is simple. It keeps all migrated migrations inside tmp folder so that Git ignores them. It's just only your local story. These files are needed to roll back the "unknown" migrations being in another branch. Now, whenever you have an inconsistent DB schema due to running migrations in some other branch just run rails db:migrate inside the current branch and it will fix the issue automatically. The gem does all this magic automatically for you.