Performance: Adding and removing columns
This affects page load speed and interaction responsiveness by changing database schema, which can impact server response time and frontend rendering if migrations block requests.
class AddBioToUsers < ActiveRecord::Migration[7.0] def change add_column :users, :bio, :text end end class RemoveNicknameFromUsers < ActiveRecord::Migration[7.0] def change remove_column :users, :nickname end end
class AddDetailsToUsers < ActiveRecord::Migration[7.0] def change add_column :users, :bio, :text remove_column :users, :nickname end end
| Pattern | Database Locking | Migration Duration | Page Load Impact | Verdict |
|---|---|---|---|---|
| Single migration with add and remove columns | Long table lock | Longer migration time | Blocks page load, increases LCP | [X] Bad |
| Separate migrations for add and remove columns | Shorter locks per migration | Shorter individual migrations | Improves server responsiveness, lowers LCP | [OK] Good |
age of type integer to the users table in a Rails migration?class AddNicknameToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :nickname, :string
end
endrails db:migrate?birthdate column from users:class RemoveBirthdateFromUsers < ActiveRecord::Migration[7.0]
def change
remove_column :users, :birthdate
end
endrails db:migrate, you get an error. What is the likely cause?status column to orders with default value 'pending' and then remove the old_status column safely. Which migration code correctly does this?def change
add_column :orders, :status, :string, default: 'pending'
remove_column :orders, :old_status, :string
end with default on add and type on remove for safe migration and rollback.Pass the test and this pattern moves unknown