diff --git a/app/build.gradle b/app/build.gradle index a14eb29bb..b9e5bf162 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,7 +45,8 @@ android { annotationProcessorOptions { arguments = [ "room.incremental" : "true", - "room.expandProjection": "true" + "room.expandProjection": "true", + "room.schemaLocation" : "$projectDir/schemas".toString() ] } } diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 16faf6cad..df83c5e7c 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -19,7 +19,7 @@ import kotlinx.coroutines.launch ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, RssStar::class, TxtTocRule::class], - version = 13, + version = 14, exportSchema = true ) abstract class AppDatabase : RoomDatabase() { @@ -31,7 +31,7 @@ abstract class AppDatabase : RoomDatabase() { fun createDatabase(context: Context): AppDatabase { return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) .fallbackToDestructiveMigration() - .addMigrations(migration_10_11, migration_11_12, migration_12_13) + .addMigrations(migration_10_11, migration_11_12, migration_12_13, migration_13_14) .addCallback(object : Callback() { override fun onDestructiveMigration(db: SupportSQLiteDatabase) { GlobalScope.launch { Restore.restoreDatabase(Backup.backupPath) } @@ -73,6 +73,41 @@ abstract class AppDatabase : RoomDatabase() { ) } } + + private val migration_13_14 = object : Migration(13, 14) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL( + """ + CREATE TABLE IF NOT EXISTS `books_new` (`bookUrl` TEXT NOT NULL, `tocUrl` TEXT NOT NULL, `origin` TEXT NOT NULL, `originName` TEXT NOT NULL, + `name` TEXT NOT NULL, `author` TEXT NOT NULL, `kind` TEXT, `customTag` TEXT, `coverUrl` TEXT, `customCoverUrl` TEXT, `intro` TEXT, + `customIntro` TEXT, `charset` TEXT, `type` INTEGER NOT NULL, `group` INTEGER NOT NULL, `latestChapterTitle` TEXT, `latestChapterTime` INTEGER NOT NULL, + `lastCheckTime` INTEGER NOT NULL, `lastCheckCount` INTEGER NOT NULL, `totalChapterNum` INTEGER NOT NULL, `durChapterTitle` TEXT, + `durChapterIndex` INTEGER NOT NULL, `durChapterPos` INTEGER NOT NULL, `durChapterTime` INTEGER NOT NULL, `wordCount` TEXT, `canUpdate` INTEGER NOT NULL, + `order` INTEGER NOT NULL, `originOrder` INTEGER NOT NULL, `useReplaceRule` INTEGER NOT NULL, `variable` TEXT, PRIMARY KEY(`bookUrl`)) + """ + ) + database.execSQL( + """ + CREATE UNIQUE INDEX IF NOT EXISTS `index_books_name_author` ON `books_new` (`name`, `author`) + """ + ) + database.execSQL( + """ + INSERT INTO books_new select * from books + """ + ) + database.execSQL( + """ + DROP TABLE books + """ + ) + database.execSQL( + """ + ALTER TABLE books_new RENAME TO books + """ + ) + } + } } abstract fun bookDao(): BookDao diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index 02106085a..a05c19a86 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -4,6 +4,7 @@ import android.os.Parcelable import androidx.room.Entity import androidx.room.Ignore import androidx.room.Index +import androidx.room.PrimaryKey import io.legado.app.constant.AppPattern import io.legado.app.constant.BookType import io.legado.app.utils.GSON @@ -16,10 +17,10 @@ import kotlin.math.max @Parcelize @Entity( tableName = "books", - primaryKeys = ["name", "author"], - indices = [(Index(value = ["bookUrl"], unique = true))] + indices = [Index(value = ["name", "author"], unique = true)] ) data class Book( + @PrimaryKey override var bookUrl: String = "", // 详情页Url(本地书源存储完整文件路径) var tocUrl: String = "", // 目录页Url (toc=table of Contents) var origin: String = BookType.local, // 书源URL(默认BookType.local)