Hint: Version numbers track applied migrations, not data or speed [OK]
Common Mistakes:
Thinking version numbers speed up queries
Confusing versioning with data storage
Assuming version numbers create backups
2. Which of the following is the correct way Rails names migration version numbers?
easy
A. A user-defined name without numbers
B. A random string of letters
C. A timestamp prefix like 20240612123456_create_users.rb
D. A hash of the migration content
Solution
Step 1: Identify Rails migration naming
Rails uses timestamps to order migrations chronologically.
Step 2: Confirm correct format
Migration files start with a timestamp like 20240612123456 followed by a descriptive name.
Final Answer:
A timestamp prefix like 20240612123456_create_users.rb -> Option C
Quick Check:
Migration version = timestamp prefix [OK]
Hint: Look for timestamp prefix in migration filenames [OK]
Common Mistakes:
Thinking version is random letters
Ignoring the timestamp format
Assuming user names migrations without numbers
3. Given these migrations applied in order: 20240601010101_create_users.rb 20240602020202_add_email_to_users.rb What will Rails do if you run rails db:migrate again without new migrations?
medium
A. It will apply all migrations again
B. It will skip migrations because they are already applied
C. It will delete the database
D. It will raise an error about duplicate migrations
Solution
Step 1: Understand migration tracking
Rails records applied migrations by their version numbers in a special table.
Step 2: Behavior on re-running migrations
If no new migrations exist, Rails skips already applied ones to avoid duplicate changes.
Final Answer:
It will skip migrations because they are already applied -> Option B
Quick Check:
Applied migrations are skipped on rerun [OK]
Hint: Rails skips migrations already recorded as applied [OK]
Common Mistakes:
Thinking migrations run twice
Expecting database deletion
Assuming errors on duplicate versions
4. You created a migration file but forgot to update its version timestamp. What problem will this cause when running rails db:migrate?
medium
A. Rails will ignore the migration because the version conflicts
B. Rails will apply the migration twice
C. Rails will raise a syntax error
D. Rails will rename the migration automatically
Solution
Step 1: Understand version uniqueness
Each migration version must be unique to track changes properly.
Step 2: Effect of duplicate version
If two migrations share the same version, Rails ignores the second to avoid conflicts.
Final Answer:
Rails will ignore the migration because the version conflicts -> Option A
Quick Check:
Duplicate versions cause ignored migrations [OK]
Hint: Duplicate timestamps cause migrations to be ignored [OK]
Common Mistakes:
Expecting automatic renaming
Thinking Rails applies duplicates
Assuming syntax errors from version conflicts
5. You have two developers creating migrations simultaneously. Developer A creates 20240612120000_add_age_to_users.rb and Developer B creates 20240612120000_add_status_to_users.rb. What issue arises and how should it be resolved?
hard
A. Rails will run only the first migration and ignore the second permanently
B. Rails merges both migrations automatically without issues
C. One migration overwrites the other silently
D. Both migrations have the same version causing conflicts; rename one with a unique timestamp
Solution
Step 1: Identify version conflict
Both migrations share the exact same timestamp version, which must be unique.
Step 2: Resolve conflict
To fix, rename one migration file with a different timestamp so Rails can track both separately.
Final Answer:
Both migrations have the same version causing conflicts; rename one with a unique timestamp -> Option D