Matthew Berman
Matthew Berman

Reputation: 8631

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?

Upvotes: 1

Views: 766

Answers (2)

gaga5lala
gaga5lala

Reputation: 1228

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

  1. In current branch, run rake db:rollback STEP=n rollback schema change done by current branch
  2. Then checkout co old branch execute rake db:rollback STEP=m to rollback schema change by old branch.
  3. Checkout back to current branch, and run rake db:migrate, and you will not see the extra changes in schema file.

reference:

Upvotes: 2

Ramy El-Farou
Ramy El-Farou

Reputation: 1

There are two possibilities:

  1. You haven't deleted the code for the previous migrations that you are trying to neglect from the schema.rb file.

  2. 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

Upvotes: 0

Related Questions