1 year ago

#101192

test-img

ColoniusRex

After rails migration, resulting schema does not match migrations. Lingering database state?

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?

ruby-on-rails

ruby

postgresql

database-schema

rails-migrations

0 Answers

Your Answer

Accepted video resources