How to delete a column without downtime using ActiveRecord 3.1?

Removing columns from tables when starting an application using ActiveRecord causes errors because ActiveRecord caches column names.

A workaround for other versions of ActiveRecord is to override the #columns method in the model and filter out the obsolete column names before migration (basically, hide these columns from AR). This worked because all the methods associated with the column name were based on calling #columns

In ActiveRecord 3.1, table structure caching is moved to ConnectionPool, and all values โ€‹โ€‹associated with the column name (e.g. coluumns_hash) are cached independently (3.2 uses ModelSchema.columns, which make it work again)

Is there a way (other than deeply cracking concrete adapters) to achieve a safe column fall in ActiveRecord 3.1?

+7
source share
2 answers

Luke Ludwig from TST Media offers a solution here . Essentially, they "override the ActiveRecord :: Base.columns method for the class whose column is being deleted."

(solution applies to all but 3.1)

+3
source

If you have multiple application servers, so you can take one snapshot at a time without downtime, the following will work:

  • Remove the ability for the user to modify the columns that you are deleting.
  • Restart applications one at a time.
  • Run the new master master database into the existing database.
  • Stop replication from your new database.
  • Drop your columns on the new database server.
  • Reconfigure each application server at the same time to use the new database server and restart.
  • Enable replication for the new database.
  • Once the source server has caught the backup, reconfigure the applications to use this database server again (with corresponding reboots).
  • Stop replication and disconnect the new database server.
+1
source

All Articles