Upgrade db, add galleries table

This commit is contained in:
FooIbar authored and tarsin committed 2023-07-20 19:00:57 +08:00
1 parent 13346f1124
commit 11426e1c4c
5 files changed
+638 -5

No files matched your search

@@ -262,6 +262,7 @@ object EhDB {
it.close()
context.deleteDatabase(ehExportName)
} use { newDb ->
db.galleryDao().list().let { newDb.galleryDao().insert(it) }
db.downloadsDao().list().forEach { newDb.downloadsDao().insert(it) }
db.downloadLabelDao().list().forEach { newDb.downloadLabelDao().insert(it) }
db.downloadDirnameDao().list().forEach { newDb.downloadDirnameDao().insert(it) }
@@ -288,6 +289,9 @@ object EhDB {
it.close()
context.deleteDatabase(tempDBName)
} use { oldDB ->
runCatching {
db.galleryDao().insert(oldDB.galleryDao().list())
}
runCatching {
val downloadLabelList = oldDB.downloadLabelDao().list()
DownloadManager.addDownloadLabel(downloadLabelList)
@@ -0,0 +1,43 @@
package com.hippo.ehviewer.dao
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.hippo.ehviewer.client.data.BaseGalleryInfo
@Entity(tableName = "GALLERIES")
data class CommonGalleryInfo(
@PrimaryKey
@ColumnInfo(name = "GID")
override var gid: Long,
@ColumnInfo(name = "TOKEN")
override var token: String?,
@ColumnInfo(name = "TITLE")
override var title: String?,
@ColumnInfo(name = "TITLE_JPN")
override var titleJpn: String?,
@ColumnInfo(name = "THUMB")
override var thumbKey: String?,
@ColumnInfo(name = "CATEGORY")
override var category: Int = 0,
@ColumnInfo(name = "POSTED")
override var posted: String?,
@ColumnInfo(name = "UPLOADER")
override var uploader: String?,
@ColumnInfo(name = "RATING")
override var rating: Float,
@ColumnInfo(name = "SIMPLE_LANGUAGE")
override var simpleLanguage: String?,
@ColumnInfo(name = "FAVORITE_SLOT")
override var favoriteSlot: Int,
) : BaseGalleryInfo()
@@ -9,6 +9,7 @@ import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.AutoMigrationSpec
import androidx.sqlite.db.SupportSQLiteDatabase
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
@DeleteTable(tableName = "BOOKMARKS")
class DropBookMarkDao : AutoMigrationSpec
@@ -22,9 +23,15 @@ class HistoryMigration : AutoMigrationSpec {
class AddPositionColumn : AutoMigrationSpec {
override fun onPostMigrate(db: SupportSQLiteDatabase) {
db.execSQL("UPDATE QUICK_SEARCH SET POSITION = (SELECT COUNT(*) FROM QUICK_SEARCH T WHERE T.TIME < QUICK_SEARCH.TIME)")
db.execSQL("UPDATE DOWNLOAD_LABELS SET POSITION = (SELECT COUNT(*) FROM DOWNLOAD_LABELS T WHERE T.TIME < DOWNLOAD_LABELS.TIME)")
db.execSQL("UPDATE DOWNLOADS SET POSITION = (SELECT COUNT(*) FROM DOWNLOADS T WHERE T.TIME < DOWNLOADS.TIME)")
val needMigrationTables = arrayOf(
"QUICK_SEARCH",
"DOWNLOAD_LABELS",
"DOWNLOADS",
)
needMigrationTables.forEach { table ->
// TODO: Rewrite this with row_number() when min sdk is 30 (SQLite 3.28.0)
db.execSQL("UPDATE $table SET POSITION = (SELECT COUNT(*) FROM $table T WHERE T.TIME < $table.TIME)")
}
}
}
@@ -32,6 +39,23 @@ class AddPositionColumn : AutoMigrationSpec {
@DeleteColumn(tableName = "DOWNLOAD_LABELS", columnName = "TIME")
class DropTimeColumn : AutoMigrationSpec
class AddGalleryTable : AutoMigrationSpec {
override fun onPostMigrate(db: SupportSQLiteDatabase) {
val needMigrationTables = arrayOf(
"HISTORY",
"DOWNLOADS",
"LOCAL_FAVORITES",
)
needMigrationTables.forEachIndexed { index, table ->
db.execSQL(
"INSERT OR IGNORE INTO GALLERIES " +
"SELECT GID, TOKEN, TITLE, TITLE_JPN, THUMB, CATEGORY, POSTED, UPLOADER, RATING, SIMPLE_LANGUAGE, " +
"${if (index == 0) "FAVORITE_SLOT" else NOT_FAVORITED} FROM $table",
)
}
}
}
class ThumbKeyMigration : AutoMigrationSpec {
override fun onPostMigrate(db: SupportSQLiteDatabase) {
val needMigrationTables = arrayOf(
@@ -53,8 +77,8 @@ class ThumbKeyMigration : AutoMigrationSpec {
}
@Database(
entities = [DownloadInfo::class, DownloadLabel::class, DownloadDirname::class, Filter::class, HistoryInfo::class, LocalFavoriteInfo::class, QuickSearch::class],
version = 12,
entities = [CommonGalleryInfo::class, DownloadInfo::class, DownloadLabel::class, DownloadDirname::class, Filter::class, HistoryInfo::class, LocalFavoriteInfo::class, QuickSearch::class],
version = 13,
exportSchema = true,
autoMigrations = [
AutoMigration(
@@ -94,10 +118,16 @@ class ThumbKeyMigration : AutoMigrationSpec {
to = 12,
spec = DropTimeColumn::class,
),
AutoMigration(
from = 12,
to = 13,
spec = AddGalleryTable::class,
),
],
)
@TypeConverters(FilterModeConverter::class)
abstract class EhDatabase : RoomDatabase() {
abstract fun galleryDao(): GalleryDao
abstract fun downloadDirnameDao(): DownloadDirnameDao
abstract fun downloadLabelDao(): DownloadLabelDao
abstract fun downloadsDao(): DownloadsDao
@@ -0,0 +1,22 @@
package com.hippo.ehviewer.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
@Dao
interface GalleryDao {
@Query("SELECT * FROM GALLERIES WHERE GID = :gid")
suspend fun load(gid: Long): CommonGalleryInfo?
@Query("SELECT * FROM GALLERIES")
suspend fun list(): List<CommonGalleryInfo>
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(galleryInfoList: List<CommonGalleryInfo>)
@Update
suspend fun update(galleryInfo: CommonGalleryInfo)
}