Set up Room database with DAOs, entities, and migrations
✓Works with OpenClaudeYou are a Kotlin Android developer. The user wants to set up a Room database with entities, DAOs, and proper migration handling for Android apps.
What to check first
- Verify
build.gradle.kts(orbuild.gradle) has Room dependencies:androidx.room:room-runtime,androidx.room:room-compiler, andandroidx.room:room-ktx - Run
./gradlew buildto ensure no compilation errors before adding Room code - Check your minimum SDK level supports Room (API 14+, but use 21+ for best support)
Steps
- Create an
Entitydata class with@Entityannotation and@PrimaryKeyon the ID field - Define a
DAO(Data Access Object) interface with@Daoannotation and suspend functions for queries - Create a
RoomDatabaseabstract class extendingRoomDatabasewith@Databaseannotation - Configure the database builder in a singleton using
Room.databaseBuilder() - Implement migrations for schema changes using
Migrationclass withmigrate()function - Add migration to
addMigrations()when building the database instance - Use
withContext(Dispatchers.IO)or coroutines when calling DAO functions from UI thread - Test database operations with AndroidJUnit4 and
@get:Rule val instantTaskExecutorRule
Code
// Step 1: Entity
@Entity(tableName = "users")
data class User(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
@ColumnInfo(name = "user_name")
val name: String,
val email: String,
val age: Int
)
// Step 2: DAO
@Dao
interface UserDao {
@Insert
suspend fun insertUser(user: User): Long
@Update
suspend fun updateUser(user: User)
@Delete
suspend fun deleteUser(user: User)
@Query("SELECT * FROM users WHERE id = :userId")
suspend fun getUserById(userId: Int): User?
@Query("SELECT * FROM users ORDER BY user_name ASC")
fun getAllUsers(): Flow<List<User>>
@Query("DELETE FROM users")
suspend fun deleteAllUsers()
}
// Step 3: Database
@Database(
entities = [User::class],
version = 2,
exportSchema = true
)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
@Volatile
private var INSTANCE: AppDatabase? = null
// Step 4: Singleton builder
fun getDatabase(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"app_database"
)
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Kotlin / Android Skills
Other Claude Code skills in the same category — free to download.
Jetpack Compose
Build Jetpack Compose UIs with state and navigation
Retrofit
Configure Retrofit for API calls with coroutines
Hilt DI
Set up Hilt dependency injection in Android
Kotlin Coroutines
Write coroutines with Flow, StateFlow, and error handling
Kotlin Testing
Write Android tests with JUnit, Mockk, and Espresso
Kotlin Coroutines & Flow
Use Kotlin Coroutines and Flow for reactive async streams
Android Room Database
Set up Room (SQLite) for local data persistence in Android Kotlin apps
Want a Kotlin / Android skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.