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
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
I have a few old migration files that contain a change method. When executing rake db:migrate VERSION=0 they give an ActiveRecord::IrreversibleMigration, I can change the migration to have and up and down method, which solves the error on my local machine, but is this a good idea or will it cause problems? For example when deploying to production?
What is the right way of solving this issue?
Thanks.
Rule of thumb is: Changing old migrations is ok, when either:
It has never been run anywhere else (e.g. you are the only developer and it hasn't been deployed yet)
It has already been run in production
I personally like to have all migrations work from any point on my local machine so that when something goes wrong I can just execute:
rake db:drop && rake db:create && rake db:migrate
The only problem that I see is, when your changing of the migration would significantly change the schema. Then you'd end up with a different schema in production and development. This might cause problems with debugging and bug fixing.
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.
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.