blob: 4cf0ecd52508251fc2078d9caedd658d217f7807 (
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
43
44
45
46
47
48
49
|
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 = 3,
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")
}
}
// v3: added FeedEntity.readerModeDisabled (per-feed reader-mode opt-out).
private val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE feeds ADD COLUMN readerModeDisabled INTEGER NOT NULL DEFAULT 0")
}
}
fun get(context: Context): AppDatabase = instance ?: synchronized(this) {
instance ?: Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"feedreader.db"
).addMigrations(MIGRATION_1_2, MIGRATION_2_3).build().also { instance = it }
}
}
}
|