Rails - how to solve this issue of an orphaned migration? - ruby-on-rails

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.

Related

Ruby on Rails migrations run, schema.rb updated but changes not reflecting in psql database

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

RAILS: Editing old migration files to solve ActiveRecord::IrreversibleMigration

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.

Heroku - undo rake db:reset

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.

How do I undo bundle exec rake db:setup?

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.

How to fix a problematic earlier migration in Rails?

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

Resources