3

Rails 6.1.4
Ruby 2.7
Postgresql 14
A dozen or so migrations, one schema.rb file.

I edited a migration, but did not change the migration id. The result is super weird behavior and I wanted to get input on the best approach.

After I incorrectly edited my migrations, I commited and pushed my feature. A team member pulled the feature and ran the migration on their machine. After they did, no matter the branch, the schema would include the changes I added when I originally modified it. But if they were on a different branch than mine, the actual migration files did not have those changes!!

I tried reverting my commit history to pre-migration editing with no luck. This is how I know it's a db issue, albeit caused from git.

So basically, after every migration, a specific model in the schema gets 4 added columns. No matter what, and it's not in a migration file on rails.

And thats the issue.

My question:

How would you go about solving this without resetting the db?

My current approach/best guess:

  • Lingering state in the db gets generated in the schema.rb file.
  • If its not in a migration, the only place schema.rb can get the info is from db.

How do I reset the state on stuff in general?

Either rebuild from scratch, or 'install a copy'. From scratch is not an option :)

If I wanted to install a copy, would it be a wise path to:

  • Revert changes from any migrations after pulling, delete branch.
  • Pull down fresh copy of branch
  • DO NOT MIGRATE - Instead, rails db:schema:load
  • This should copy over the db structure and effectively overwrite any lingering ghost state.
  • Rails db:migrate -> this will update migrations,
    • if you did everything right only the schema version number should change
  • Now things are synced, continue to db:migrate as normal moving forward.

I did this on my local machine and was successful, but I am curious.. Am I understanding this process correctly? Is there an easier way?

Holger Just
  • 52,918
  • 14
  • 115
  • 123
  • 1
    Welcome! Not a rails expert but interested. Do not run `rails db:schema:load` on production, see [this old post](https://stackoverflow.com/a/5905958/3842598). Maybe you have old (pending) migrations in the schema_migrations table, check with `rake db:migrate:status`, see [this old post](https://stackoverflow.com/questions/37697938/is-there-a-way-to-resolve-rails-deleted-migrations). You can learn more about [old migrations](https://edgeguides.rubyonrails.org/active_record_migrations.html#old-migrations) in the docs. Don't risk to loose production data. Wait for some experts. – Christian Jan 26 '22 at 22:40

0 Answers0