5

I've noticed this for months and I just didn't have the time to deal with it until now. Whenever my CI server does an automatic git pull and restarts the rails servers, the schema.rb gets randomly modified. As the example shows below, the api_name column of a certain table got dropped. I dropped this column about 3 months ago. Same with transportation_charges. And very often, the spacing in this file changes: see created_at and updated_at.

It's especially annoying since on the next run, when my CI does an initial git pull, it complains about changes to schema.rb and stops execution until they get pushed or reverted. And it's not just the CI server. I've seen this on other developer machines as well. Did anyone run into this before?

diff --git a/db/schema.rb b/db/schema.rb
index 470d3bf..166e3ee 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -883,7 +883,6 @@ ActiveRecord::Schema.define(version: 20170720211740) do

   create_table "ups_package_service_options", force: :cascade do |t|
     t.string   "name"
-    t.string   "api_name"
     t.string   "type"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
@@ -910,9 +909,8 @@ ActiveRecord::Schema.define(version: 20170720211740) do
     t.string   "code"
     t.string   "name"
     t.string   "api_name"
-    t.decimal  "transportation_charges"
-    t.datetime "created_at",             null: false
-    t.datetime "updated_at",             null: false
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
     t.boolean  "domestic"
   end
Abdullah
  • 2,015
  • 2
  • 20
  • 29
Jenna S
  • 616
  • 1
  • 6
  • 24
  • Jenna, is the `schema.rb` in the `.gitignore` file? – MatayoshiMariano Aug 03 '17 at 20:52
  • No. Should it be ignored? I read that it's a good idea to have it under version control. Especially, since I always have new server I need to deploy it under, so it's useful to do a rake db:setup. I do have hundreds of migrations. – Jenna S Aug 04 '17 at 00:21
  • Jenna I have been ignoring the `schema.rb` because it avoids this kind of conflicts and more important, if I run `rake db:migrate` from scracth I can guarantee that all the migrations works. However I have been looking some info about ignoring or not: [Link1](https://stackoverflow.com/questions/6450263/what-is-the-right-approach-to-deal-with-rails-db-schema-rb-file-in-git), [Link2](https://stackoverflow.com/questions/737854/what-is-the-preferred-way-to-manage-schema-rb-in-git), [Link3](http://thelazylog.com/you-should-always-commit-gemfile-lock-and-schema-rb/) – MatayoshiMariano Aug 04 '17 at 13:08

1 Answers1

4

When you run a migration, the schema gets updated not just by the migration, but also the current database. I'm guessing some of your developers are using databases inconsistent with the schema. Then you'll get unexpected changes every time they run a migration.

EJAg
  • 3,210
  • 2
  • 14
  • 22
  • It's not a big team. Sometimes I'll make 2 deployments a night when everyone else is sleeping, so I'm pretty sure that it's just myself - and it still happens. I also read that inconsistencies between the OS and/or database might cause this. But we are using Docker, so the OS and database remain constant. – Jenna S Aug 04 '17 at 00:23
  • @JennaS I've seen this behavior before myself. Try jumping into the rails console in your server and checking if the model has the "api_name" attribute. It's a mismatch between your development database schema and your production schema. As EJ2015 says, after migrations, a `schema.rb` is generated using the current state of your database, and not the state of your migrations. – Amin Shah Gilani Aug 04 '17 at 01:19
  • Thanks for the tips. There might be something wrong with my deployment & build scripts. Will investigate further and post the findings. – Jenna S Aug 09 '17 at 04:36
  • I still don't understand what's going on. I was able to identify that git is not at fault. It's the rake db:migrate command that runs on the build machine, that for some reason, it makes these random changes - that make no sense. I think my database is simply out of sync with my database.rb file. I will drop the database, recreate it and keep it under watch. – Jenna S Aug 15 '17 at 00:51
  • 1
    So, after more testing, I concluded that my actual database schema had changed at some point and every time rake db:migrate would run, it would simply generate these changes. After dropping and re-creating the schema, rake db:migrate doesn't the strange changes in schema.rb. I'm not sure how it happened in the first place. Thanks for the help. – Jenna S Aug 15 '17 at 01:26