deleted migration file keeps coming back Rails 3 - ruby-on-rails

I have 2 versions of the same migration (bad habit, I know). I deleted old version many times, but after restarting my project (or do something with it, like rake db:test:prepare), the file shows up in my migrate folder. When I run rake db:migrate, it will complain about multiple migrations with the same name.
How can I delete a migration file completely? Is there a registry that I need to remove to prevent it from coming back?

Are you updating from a repo? I don't see how the original file could be restored otherwise.
You can also delete the entry from the schema_migration table, but that just tracks if it's been run or not (IIRC).

git add only adds new and changed files, it doesn't remove deleted ones. To delete:
git rm db/migrate/<filename>
or if you have already deleted it from the filesystem:
git add -u

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

Rails Git Migration Issue With Branches

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

Protection against git removal of untracked .gitignored files in rails

I managed to delete to following files with git clean in my rails app:
Removing .DS_Store
Removing .bundle/
Removing config/application.yml
Removing db/development.sqlite3
Removing log/development.log
Removing public/system/profiles/avatars/000/000/011/
Removing public/system/profiles/avatars/000/000/012/
Removing tmp/
Are there any chances of getting back these files somehow or being able to copy from somewhere else? (Git deletes files permanently, so trash is empty. I also tried w/ disk drill but id didn't work out either.)
How can I protect my gitignored files properly to avoid these kinda situations in the future? Should I copy them sometimes or there are other programs to handle this?
Do I have to recreate my rails app or somebody knows a better option?
I think you can try to find these files in previous commits.
Next time remove using command git rm --cached file_name
I was able to pull it off. I ran bundle install (creates the files within .bundle) and reinstalled sqlite and figaro. After that I ran rake db:migrate which created the development.sqlite3 file. I lost the development data, but it is not a huge problem at all. For figaro I had to retype the keys and passwords (Sendgrid, Stripe, etc.) into the new application.yml file. Temporary and log files don't seem to be important. Thanks for the answers. This $git clean is pretty dangerous, watch out!

Multiple Migrations whit same Version Number on Deployng Rails to Heroku

Im have this problem many times, i search much and not found any solution to solve, my problem is this:
After the run
git push heroku push master
When i run
heroku run rake db:migrate
i get this error:
Multiple migrations have the version number 20130615132808
im search by this problem and found this:
rails database migration - multiple migrations have the version number x
but when execute git rm appear some options i dont understand much about git so i need solve this problem, in localhost im delete the archives but the problem persists, thanks very much by the help.
Just rename the files with duplicate timestamps (add 1 to the last digit) and then add, commit and push files. When you run heroku run rake db:migrate again all will be dandy.
And for the future remember to not copy and rename migrations by hand (so you don't get repeated version numbers)
This might occur when you copy-paste multiple "rails generate" commands to generate migrations. The migrations generated may have the same time stamp. If you type them in (or copy-paste them) separately, they will have different timestamps.
When this happens, you can simply rename the migration files under db/migrate to contain different timestamps.

What is the preferred way to manage schema.rb in git?

I don't want to add schema.rb to .gitignore, because I want to be able to load a new database schema from that file. However, keeping it checked in is causing all sorts of spurious conflicts that are easily resolved by a fresh db:migrate:reset.
Basically I want a way to:
Keep schema.rb in the repository for deploy-time database setup
Keep schema.rb in '.gitignore' for general development
There would be one or two people responsible for updating schema.rb and knowing that it was correct.
Is there a way I can have my cake and eat it, too?
I'm afraid the magic solution you're looking for does not exist. This file is normally managed in version control, then for any conflicts on the version line just choose the later of the two dates. As long as you're also running all of the associated migrations nothing should get out of sync this way. If two developers have caused modifications to a similar area of schema.rb and you get conflicts in addition to the version then you are faced with a normal merge conflict resolution, but in my opinion these are normally easy to understand and resolve. I hope this helps some!
One other thing you can do is use:
git update-index --assume-unchanged /path/schema.rb
This will keep the file in the repository but won't track changes. you can switch the tracking anytime by using:
git update-index --no-assume-unchanged /path/schema.rb
What has worked really well for me is to delete and .gitignore schema.rb and then have it regenerated for each developer when they rake db:migrate.
You can still achieve what you wanted without migrating from 0 and risking broken migrations from years ago by simply doing a "roll-up" of the migrations periodically. You can do this by:
Run all outstanding migrations with rake db:migrate
Taking the contents of your schema.rb in the ActiveRecord::Schema.define block
Paste it into your initial_schema migration inside def up (overwriting what's already there)
Delete all other migrations
Now your initial_schema migration is your starting point for new systems and you don't have to worry about conflicts in schema.rb that may not be resolved correctly. It's not magical, but it works.
Would it be sufficient to do a rake db:dump in a pre-commit git hook?
The following won't necessarily fix (1) or (2), but it might take care of the merging issue, and then maybe (1) and (2) go away.
Instead of using .gitignore, use separate branches: Develop which omits schema.rb and Test and Deploy which include schema.rb. Only make code changes in the Develop branches and never merge from Test into Develop. Keep schema.rb in a separate branch:
Developer A
Develop --------
Local Schema \ Your Repo
Test ---------> Dev A
---------> Dev B
Developer B / Master
Develop -------- Schema
Local Schema Test
Test Deploy
In Git, branches are pointers to collections of file contents, so they can include or exclude particular files as well as track file versions. This makes them flexible tools for building your particular workflow.
You could define a merge strategy.
I've found this solution, but dont remember the source
[merge "railsschema"]
name = newer Rails schema version
driver = "ruby -e '\n\
system %(git), %(merge-file), %(--marker-size=%L), %(%A), %(%O), %(%B)\n\
b = File.read(%(%A))\n\
b.sub!(/^<+ .*\\nActiveRecord::Schema\\.define.:version => (\\d+). do\\n=+\\nActiveRecord::Schema\\.define.:version => (\\d+). do\\n>+ .*/) do\n\
%(ActiveRecord::Schema.define(:version => #{[$1, $2].max}) do)\n\
end\n\
File.open(%(%A), %(w)) {|f| f.write(b)}\n\
exit 1 if b.include?(%(<)*%L)'"
put this "somewhere" and
git-config --global core.attributesfile "somewhere"
I built a gem to solve this problem.
It sorts columns, index names and foreign keys, removes excess whitespace and runs Rubocop for some formatting to unify the output of your schema.rb file.
https://github.com/jakeonrails/fix-db-schema-conflicts
After you add it to your Gemfile you just run rake db:migrate or rake db:schema:dump like normal.
Commit schema.rb file.
Run git pull (or continue with what you're doing)
Every time you migrate the database, the schema.rb file updates and appears in git status. When working on something and occasionally doing git pull, this can be annoying because you have to commit schema.rb file before pulling to resolve conflict. This means that every time you migrate the database, you need to commit schema.rb file.
schema.rb should be tracked Git, of course.
I've just released this gem that can solve an issue with "conflicts" between branches 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.

Resources