Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Created September 16, 2017 18:53
Show Gist options
  • Save AdamMc331/5d48220875ce6b8bf0a0c0396ec8a0c0 to your computer and use it in GitHub Desktop.
Save AdamMc331/5d48220875ce6b8bf0a0c0396ec8a0c0 to your computer and use it in GitHub Desktop.
Shows how a RoomDatabase.Callback can be used to add database triggers when a database is created.
@Database(...)
abstract class AppDatabase : RoomDatabase() {
abstract fun appDao(): AppDao
companion object {
private var INSTANCE: AppDatabase? = null
private set
fun getInMemoryDatabase(context: Context): CCDatabase {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context,
AppDatabase::class.java, "app_database.db")
.addCallback(CALLBACK)
.build()
}
return INSTANCE!!
}
private val CALLBACK = object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
db.execSQL("CREATE TRIGGER ...")
}
}
}
}
@WicherW
Copy link

WicherW commented Jul 23, 2022

Thanks a lot! i was looking for that solution :D

private val CALLBACK = object : RoomDatabase.Callback() {
            override fun onCreate(db: SupportSQLiteDatabase) {
                super.onCreate(db)

                db.execSQL("CREATE TRIGGER ...")
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment