Performance: Database folder and migrations
This concept affects the initial page load speed and backend response time by managing how database schema changes are applied and how the app interacts with the database.
class AddDetailsToUsers < ActiveRecord::Migration[6.0] def change add_column :users, :details, :string end end
class AddDetailsToUsers < ActiveRecord::Migration[6.0] def change add_column :users, :details, :text add_column :users, :details, :string end end
| Pattern | Database Locks | Migration Time | Backend Delay | Verdict |
|---|---|---|---|---|
| Redundant migrations with conflicting changes | High - multiple locks | Long - repeated schema updates | High - blocks server response | [X] Bad |
| Single clear migration per change | Low - minimal locking | Short - one schema update | Low - faster server response | [OK] Good |
db/migrate folder in a Rails project?db/migrate folderapp/models, configs in config, and assets in app/assets.db/migrate folder [OK]rails generate migration followed by the migration name and fields.rails generate migration to create migrations [OK]class CreateBooks < ActiveRecord::Migration[7.0]
def change
create_table :books do |t|
t.string :title
t.integer :pages
t.timestamps
end
end
endrails db:migrate?rails db:migrate doesclass AddEmailToUsers < ActiveRecord::Migration
def change
add_column :users, :email, :string
end
endrails db:migrate gives an error: undefined method 'add_column'. What is the likely cause?ActiveRecord::Migration[7.0].add_column, causing the error.username to login in the users table using a migration. Which migration code correctly does this?rename_column changes a column's name safely without losing data.rename_column :users, :username, :login, which is correct. def change
change_column :users, :username, :login
end wrongly uses change_column which changes type, not name. def change
remove_column :users, :username
add_column :users, :login, :string
end removes and adds columns, which is risky. def change
rename_table :users, :login
end renames the whole table, not a column.Pass the test and this pattern moves unknown