Rails Git Migration Issue With Branches - ruby-on-rails

Still a newbie. I'm working on a new feature for a RoR app. I created a local branch and generated a migration. Unfortunately I didn't save my changes to the migration file and then ran db:migrate. Wanting to start over, I switched back to master and repulled from my git and did a hard reset with the following commands (I never committed the files in the branch either locally or remotely):
git fetch --all
git reset --hard origin/master
I then remade a local branch, recreated the migration (correctly this time) and ran db:migrate. I get an error that the table already exists in the database, however, when I look in schema.db the table isn't there.
All I want is to go back to where I was based on the remote git. For what it's worth, I'm using Cloud9 on AWS for development. Thanks!

There is nothing to do with database when you make changes regarding git. Once you run rake task like rake db:migrate to make database changes, it will get reverted automatically once you change branch, You have to prepare rollback steps. (As down methods in migrations are run conventionally)
Your old migration version was different than new recreated migration so application tried to run migration file without checking whether table exists.
Whenever you run rake db:migrate in for particular database, it store migration version in your schema_migrations table in db. So calling again and again same rake will not try to create table with same name. In above case you have different migration files to create same table and schema_migration table do not know whether you deleted branch with old migration file or whether table already exists
So run following in your rails console,
ActiveRecord::Migration.drop_table :table_name
And then run your rake db:migrate

Related

Heroku missing last 4 migrations. Migration files do exist in Git

Heroku is missing the last 4 migrations for a RoR app. The files exist in the git repository and are named/numbered accordingly. I tried manually forcing that version to run, but it simply can't find it. I tried this:
heroku run rake db:migrate:up VERSION=20190328183515
and the result was
ActiveRecord::UnknownMigrationVersionError: No migration with version number 20190328183515
Trying to redeploy from local to heroku stage does nothing since it states everything is up to date. I tried touching the files in case they weren't included in the deployed files, but looking at the git repository confirmed that's not the case.
Any ideas on what's going on here and how to get heroku to recognize that it is missing 4 migration files that still need to be processed? Resetting the database is not an option. I tried rolling back the database and running the migrations again but it stops on the migration file for 3/27/2019

Why does schema.rb update after I run db:migrate for Rails?

My understanding is that whoever created the migrations should've also updated schema.rb. Since I've pulled the migrations, I should've also pulled the updated schema.rb. However, once in a while, schema.rb updates after I run bundle exec rake db:migrate.
My current workflow is:
git pull --rebase origin master --prune
rails s
Rails tells me to migrate
bundle exec rake db:migrate
Realize schema.rb updated
At this point, I'm pretty sure I'm not supposed to check in the updated schema.rb. I'd manually revert it through git checkout origin/master db/schema.rb.
So what went wrong in this case? Did a co-worker forget to run migrations after creating them? Did I do something wrong?
As far as I know schema can change after running rails db:migrate because of:
A co-worker did not commit the schema.rb so when you fetched and run the migrations you get the diff
A different DB version is running on your local machine. Based on db configuration schema may be changed accordingly.
Running git diff will help you to understand what is going.
schema.rb retains two key sets of data:
a description of all the tables in your app database structure
a list of all the migrations that have been applied.
if a new developer were to join your team, they should be able to run rake db:schema:load and get an up-to-date database structure straight away. That's far more efficient and reliable than expecting them to run through all the migrations manually.
Running rake db:migrate, even if there are no outstanding migrations that need running, will always regenerate db/schema.rb. Most of the time you won't notice because the file will be the same – but you may get differences in whitespace formatting or column order.
The best practice (IMHO) should always be to check in the updated db/schema.rb in the same commit as any migrations you've added.
When fetching or pulling branches to your local machine, running rake db:migrate will apply whatever new migrations need to be run based on the records in your local database's schema_migrations table. After that, your new db/schema.rb should be the same as the one you pulled down – but if it isn't, git diff will show you what the difference is.
You can then make a judgement call as to what the best course of action is. If the only difference is cosmetic, I personally tend to revert the unstaged changes and leave the committed version untouched until the next migration.
All the above also applies if you have switched to a SQL-based structure file (db/structure.sql) by specifying config.active_record.schema_format = :sql in config/application.rb.

Unintended Schema changes every time I commit

Every time I commit code that has a migration, for some reason, I get a bunch of schema changes that I didn't write, that came from previous PRs.
For example, i'll write a migration to add a column on User...but after running the migration, the schema file will include 10 changes from previous old code that isn't in the current branch at all.
How do I fix this?
The schema file reflect to the database schema. I think you had changed the schema at previous old code but didn't recover(rollback) it, deleted it and start coding for new migration.
The thing you shloud do is eliminating diff between code and datebase.
Solution:
Checkout to your old branch and rollback the schema change by runningrake db:migrate:down VERSION=20161106xxxxxx.
or
In current branch, run rake db:rollback STEP=n rollback schema change done by current branch
Then checkout co old branch execute rake db:rollback STEP=m to rollback schema change by old branch.
Checkout back to current branch, and run rake db:migrate, and you will not see the extra changes in schema file.
reference:
http://edgeguides.rubyonrails.org/active_record_migrations.html#rolling-back
http://edgeguides.rubyonrails.org/active_record_migrations.html#running-specific-migrations
There are two possibilities:
You haven't deleted the code for the previous migrations that you are trying to neglect from the schema.rb file.
You're very new to rails and you tried deleting fields from the schema.rb file manually, thinking it would synchronize with your database.
Either way: delete all the migration files you don't want if you haven't already, then simply rollback your database to the original empty version using the command:
rake db:rollback VERSION=0
Then: Now that you have the right migration files, migrate to your databse using the command:
rake db:migrate
This should give you an accurate schema.rb file

Why 'git checkout .' doesn't undo the changes made by rake 'db:rollback'?

I created a rails application using scaffolding and migrated the database.
and I committed a local repository by git commit -m "First commit"
then I unrolled the database using rake db:rollback and the application stopped working.
I tried to undo using git checkout . but the application wasn't still working till I migrated the database again using rake db:migrate.
Why is this happening?
Rails' migration mechanism checks for a specific table in your db which shows which migrations are applied to your db and which are pending migrations (from the files that are present but without an entry).
When you perform a db:migrate or a db:rollback this table is also updated.
The db files are not inside your repository (and shouldn't be), so you can not undo these changes by git.
You need to use the tools provided by the rake tasks.
Running a rake -T db will give you a full list of the tools you have to manipulate your migrations and db status.
If you want to redo or change the migration you need to create another one,
for more info check this.

rails database migration - multiple migrations have the version number x

Ok, I have stuffed up my migrations. I tried to sort it by deleting duplicates, sorting the schema.rb etc but I don't think I have done it properly.
When I try to deploy to heroku, or rather heroku run rake db:migrate, I get
Multiple migrations have the version number 20130307005437
The migrate works fine on localhost but not heroku.
Unfortunately when I look for migration no 20130307005437, it's not there in my db/migrate.
How can I find it to sort the problem?
While this file might not be visible within your directory listing, I suspect that there might already be a file within your Git repository, which is what is causing this error from appearing on Heroku and not locally.
Please ensure that you've only got one migration inside that Git repo with that number.

Resources