Friday, February 3, 2017

A warning about schema_cache.clear! and reset_column_information in some versions of ActiveRecord

A tip today about reloading column information and schema cache in Rails:

Be careful relying on YourModel.connection.schema_cache.clear! and YourModel.reset_column_information. We noticed some strange errors while calling it a lot under load. Instead, if you can avoid using it (best option) or do a rolling restart of Puma via its tmp_restart plugin (works great and handles requests in process like a champ, but delays requests and could be a horrible idea under load), either of those options might be better choices.

Other options/suggestions:

  • Try not to change column names (and possibly table names) in any DB referred to directly by Rails.
  • Be careful with any gem or code that will search through all columns in an ActiveRecord model.
  • In Rails 5, you could be diligent about adding ignored_columns to models any columns you plan to rename or delete.
  • You could patch your model/define it's columns so that it accurately reflects columns you plan to delete or want to ignore, such that it disregards them.

No comments: