Be Careful With Your Database Migration
In the early stage as a Start-Up, we do many experiments to prove our assumption to reach product market fit. Updating database schema could be a daily or weekly task, and it should be done correctly. Especially when we have reached product market fit, many users are using our product.
Example scenario: We do this when deleting a column in Rails
class RemoveColumn < ActiveRecord::Migration
def change
remove_column :users, :promo_code
end
end
When we deploy the code into production and execute the migration command (rails db:migrate), any running servers will crash if there is a process that reads the promo_code
column.
The safe way to do that is to deploy a new version to ignore the promo_code column.
class User < ApplicationRecord
self.ignored_columns = ["promo_code"]
end
Once the deployment is done, then delete the column.
class RemoveSomeColumnFromUsers < ActiveRecord::Migration
def change
remove_column :users, :promo_code
end
end
Fortunately, there is a RubyGem to prevent bad things happened. It’s called Strong Migrations
Leave a comment