What is a database migration in Android development?
A database migration is the process of updating the database schema when the app's data structure changes, without losing existing user data.
Click to reveal answer
beginner
Why are database migrations important?
They ensure that users keep their data intact when the app updates its database schema, preventing crashes and data loss.
Click to reveal answer
intermediate
In Room database, what class helps define migrations?
The Migration class helps define how to convert the database from one version to another by overriding the migrate() method.
Click to reveal answer
beginner
What is the role of the version number in database migrations?
The version number tells the app which schema version the database currently has and which version it should migrate to.
Click to reveal answer
intermediate
How do you apply multiple migrations in Room?
You provide all Migration objects in the Room.databaseBuilder using addMigrations() so Room can apply them in order.
Click to reveal answer
What happens if you change the database schema without a migration?
AThe app may crash or lose data
BThe app updates automatically without issues
CThe database version stays the same
DThe app ignores the schema change
✗ Incorrect
Without a migration, the app cannot handle the schema change and may crash or lose user data.
Which method do you override to define migration steps in Room?
AonUpgrade()
BonCreate()
CupdateSchema()
Dmigrate()
✗ Incorrect
The migrate() method in the Migration class defines how to update the database schema.
What does the version number in Room database represent?
AThe number of migrations
BThe schema version of the database
CThe number of tables
DThe app version
✗ Incorrect
The version number represents the current schema version of the database.
How do you tell Room to use migrations?
ABy changing the app version
BBy deleting the old database
CBy calling addMigrations() on the database builder
DBy calling migrateDatabase()
✗ Incorrect
You use addMigrations() to provide migration steps to Room.
If you skip a migration version, what happens?
ARoom throws an exception at runtime
BRoom automatically fixes it
CThe app ignores the missing migration
DThe database resets to empty
✗ Incorrect
Room requires all migration steps; skipping causes a runtime error.
Explain how to create and apply a database migration in an Android app using Room.
Think about how you tell Room to move from one schema version to another safely.
You got /4 concepts.
Why is it important to handle database migrations carefully in mobile apps?
Consider what happens if the app updates but the database does not.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of a database migration in Android apps?
easy
A. To update the database schema without losing existing data
B. To delete all data and recreate the database from scratch
C. To backup the database to external storage
D. To optimize database queries for faster performance
Solution
Step 1: Understand database migration purpose
Database migrations are used to change the structure of the database safely.
Step 2: Identify data preservation goal
Migrations update tables or columns without deleting existing user data.
Final Answer:
To update the database schema without losing existing data -> Option A
Quick Check:
Migration = Safe schema update [OK]
Hint: Migrations keep data safe while changing schema [OK]
Common Mistakes:
Thinking migrations delete all data
Confusing migrations with backups
Believing migrations only optimize queries
2.
Which of the following is the correct way to define a migration from version 1 to 2 in Room database?
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
// ?
}
}
easy
A. database.execSQL("ALTER TABLE users ADD COLUMN age INTEGER DEFAULT 0")
B. database.update("users", "age", 0)
C. database.insert("users", ContentValues())
D. database.delete("users")
Solution
Step 1: Understand Migration class usage
Migration requires SQL commands inside migrate() to change schema.
Step 2: Identify correct SQL command
ALTER TABLE adds a new column safely; execSQL runs raw SQL.
Final Answer:
database.execSQL("ALTER TABLE users ADD COLUMN age INTEGER DEFAULT 0") -> Option A
Quick Check:
Migration uses execSQL with ALTER TABLE [OK]
Hint: Use execSQL with ALTER TABLE to add columns [OK]
Common Mistakes:
Using update() instead of execSQL
Trying to insert or delete rows in migration
Not providing default value for new column
3.
Given this migration code, what will be the result after migrating from version 1 to 2?
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY, title TEXT)")
}
}
Assuming version 1 had no books table.
medium
A. No changes happen because CREATE TABLE is ignored
B. Migration fails because table 'books' already exists
C. A new table 'books' is created with columns 'id' and 'title'
D. Existing tables are deleted and only 'books' remains
Solution
Step 1: Analyze migration SQL command
CREATE TABLE IF NOT EXISTS creates the table only if it doesn't exist.
Step 2: Check initial database state
Version 1 has no 'books' table, so it will be created.
Final Answer:
A new table 'books' is created with columns 'id' and 'title' -> Option C
Quick Check:
CREATE TABLE IF NOT EXISTS creates missing table [OK]
Hint: IF NOT EXISTS avoids errors if table exists [OK]
Common Mistakes:
Assuming migration fails if table exists
Thinking CREATE TABLE is ignored
Believing existing tables get deleted
4.
What is wrong with this migration code from version 2 to 3?
Hint: Avoid adding same column twice in one migration [OK]
Common Mistakes:
Ignoring duplicate column errors
Thinking migration version is updated automatically
Believing ALTER TABLE syntax is wrong here
5.
You want to rename a column username to user_name in your Room database from version 3 to 4. SQLite does not support renaming columns directly. Which migration strategy is correct?
hard
A. Add a new column and leave the old column as is
B. Use ALTER TABLE to rename the column directly
C. Delete the old table and create a new one with the new column name
D. Create a new table with the new column name, copy data, drop old table, rename new table