blob: e2f6c1aa6d8d1da374d1fb489332214dac632900 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package org.terst.feedreader.data
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.room.migration.Migration
@Database(
entities = [FeedEntity::class, ArticleEntity::class, FilterRuleEntity::class],
version = 2,
exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
abstract fun feedDao(): FeedDao
abstract fun articleDao(): ArticleDao
abstract fun filterRuleDao(): FilterRuleDao
companion object {
@Volatile private var instance: AppDatabase? = null
// v2: added ArticleEntity.imageUrl (thumbnail support). Real migrations,
// not fallbackToDestructiveMigration, since this app already has real
// subscriptions/read-state on-device during active development --
// wiping the DB on every schema tweak would mean re-importing OPML
// and losing read history each time.
private val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE articles ADD COLUMN imageUrl TEXT")
}
}
fun get(context: Context): AppDatabase = instance ?: synchronized(this) {
instance ?: Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"feedreader.db"
).addMigrations(MIGRATION_1_2).build().also { instance = it }
}
}
}
|