Bird
Raised Fist0
Android Kotlinmobile~20 mins

Database migrations in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Database Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What happens after a database migration?
You have an Android app using Room database. You add a new column to a table and write a migration from version 1 to 2. What will the app do after updating if the migration is correct?
AThe app crashes because the schema version is higher than before.
BThe app deletes all existing data and creates a new database.
CThe app updates the database schema without losing existing data.
DThe app ignores the migration and uses the old schema.
Attempts:
2 left
💡 Hint
Think about what a migration is supposed to do to keep data safe.
📝 Syntax
intermediate
2:00remaining
Identify the correct Room migration code snippet
Which Kotlin code snippet correctly defines a migration from version 1 to 2 that adds a new column 'age' to the 'User' table with a default value 0?
Android Kotlin
val MIGRATION_1_2 = object : Migration(1, 2) {
  override fun migrate(database: SupportSQLiteDatabase) {
    // migration code here
  }
}
Adatabase.execSQL("DROP TABLE User")
Bdatabase.execSQL("ALTER TABLE User ADD COLUMN age INTEGER NOT NULL DEFAULT 0")
Cdatabase.execSQL("CREATE TABLE User(age INTEGER)")
Ddatabase.execSQL("ALTER TABLE User ADD COLUMN age TEXT")
Attempts:
2 left
💡 Hint
Adding a non-null column requires a default value in SQLite.
❓ lifecycle
advanced
2:00remaining
When is the migration code executed in an Android app?
At what point during the app lifecycle does Room execute the migration code when upgrading the database version?
AWhen the app is uninstalled and reinstalled.
BImmediately when the app starts, before any UI loads.
COnly when the user triggers a manual database refresh.
DWhen the database is first opened or accessed after the app update.
Attempts:
2 left
💡 Hint
Think about when the app needs to access the database after an update.
🔧 Debug
advanced
2:00remaining
Why does this migration cause a crash?
You wrote this migration code: val MIGRATION_2_3 = object : Migration(2, 3) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE User ADD COLUMN email TEXT NOT NULL") } } The app crashes on upgrade. Why?
AAdding a NOT NULL column without a default value causes a constraint error.
BThe SQL syntax for ALTER TABLE is incorrect.
CThe migration version numbers are reversed.
DThe User table does not exist in version 2.
Attempts:
2 left
💡 Hint
SQLite requires a default value when adding NOT NULL columns.
🧠 Conceptual
expert
3:00remaining
What is the best practice for handling complex schema changes in Room migrations?
You need to rename a column and change its type in a Room database migration. SQLite does not support renaming columns directly. What is the recommended approach?
ACreate a new table with the desired schema, copy data from the old table, drop the old table, then rename the new table.
BDelete the database and recreate it with the new schema.
CUse ALTER TABLE to rename the column and change its type directly.
DIgnore the change and keep the old schema.
Attempts:
2 left
💡 Hint
Think about how to preserve data when SQLite lacks direct support for renaming columns.

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

  1. Step 1: Understand database migration purpose

    Database migrations are used to change the structure of the database safely.
  2. Step 2: Identify data preservation goal

    Migrations update tables or columns without deleting existing user data.
  3. Final Answer:

    To update the database schema without losing existing data -> Option A
  4. 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

  1. Step 1: Understand Migration class usage

    Migration requires SQL commands inside migrate() to change schema.
  2. Step 2: Identify correct SQL command

    ALTER TABLE adds a new column safely; execSQL runs raw SQL.
  3. Final Answer:

    database.execSQL("ALTER TABLE users ADD COLUMN age INTEGER DEFAULT 0") -> Option A
  4. 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

  1. Step 1: Analyze migration SQL command

    CREATE TABLE IF NOT EXISTS creates the table only if it doesn't exist.
  2. Step 2: Check initial database state

    Version 1 has no 'books' table, so it will be created.
  3. Final Answer:

    A new table 'books' is created with columns 'id' and 'title' -> Option C
  4. 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?

val MIGRATION_2_3 = object : Migration(2, 3) {
    override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL("ALTER TABLE users ADD COLUMN email TEXT")
        database.execSQL("ALTER TABLE users ADD COLUMN email TEXT")
    }
}
medium
A. The migration does not update the database version
B. The same column 'email' is added twice causing an error
C. The SQL syntax for ALTER TABLE is incorrect
D. The migration should delete the 'users' table first

Solution

  1. Step 1: Review SQL commands in migration

    The code tries to add the 'email' column twice to the same table.
  2. Step 2: Understand SQLite behavior

    Adding the same column twice causes a runtime error during migration.
  3. Final Answer:

    The same column 'email' is added twice causing an error -> Option B
  4. Quick Check:

    Duplicate column addition causes migration failure [OK]
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

Solution

  1. Step 1: Understand SQLite column rename limitation

    SQLite does not support direct column renaming with ALTER TABLE.
  2. Step 2: Apply recommended migration pattern

    Create a new table with desired schema, copy data, drop old table, rename new table.
  3. Final Answer:

    Create a new table with the new column name, copy data, drop old table, rename new table -> Option D
  4. Quick Check:

    Rename column = new table + copy data [OK]
Hint: Rename columns by new table + copy data [OK]
Common Mistakes:
  • Trying to rename column with ALTER TABLE
  • Deleting old table without copying data
  • Adding new column but not removing old one
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard