YokanRoomDatabase.kt
composeApp/src/nonWebMain/kotlin/eu/friedcutlet/yokan/database/room/YokanRoomDatabase.kt
1package eu.friedcutlet.yokan.database.room23import androidx.room.ColumnInfo4import androidx.room.ConstructedBy5import androidx.room.Dao6import androidx.room.Database7import androidx.room.Entity8import androidx.room.ForeignKey9import androidx.room.Insert10import androidx.room.OnConflictStrategy11import androidx.room.PrimaryKey12import androidx.room.Query13import androidx.room.RoomDatabase14import androidx.room.RoomDatabaseConstructor15import androidx.room.Upsert16import androidx.room.migration.Migration17import androidx.sqlite.SQLiteConnection18import androidx.sqlite.driver.bundled.BundledSQLiteDriver19import androidx.sqlite.execSQL20import eu.friedcutlet.yokan.database.DbSettings2122// ─── Entity schemas ─────────────────────────────────────────────────────────23// One Row class per logical table. Complex types (enums, List<String>,24// nested objects) are stored as JSON TEXT columns and translated in25// `RowAdapters.kt`; the schema stays plain SQL primitives so Room's KSP26// processor doesn't need TypeConverters.2728@Entity(tableName = "manga")29data class MangaRow(30 @PrimaryKey val id: Long,31 val name: String? = null,32 val link: String? = null,33 val imageUrl: String? = null,34 val description: String? = null,35 val author: String? = null,36 val artist: String? = null,37 val status: String,38 val itemType: String,39 val genre: String? = null,40 val favorite: Boolean = false,41 val source: String? = null,42 val lang: String? = null,43 val dateAdded: Long? = null,44 val lastUpdate: Long? = null,45 val lastRead: Long = 0,46 val categories: String? = null,47 val isLocalArchive: Boolean = false,48 val customCoverImage: ByteArray? = null,49 val customCoverFromTracker: String? = null,50 val smartUpdateDays: Int? = null,51 val updatedAt: Long = 0,52 val sourceId: Long? = null,53 @ColumnInfo(defaultValue = "''") val privateNote: String = "",54 @ColumnInfo(defaultValue = "0") val nextEpisodeAiringAt: Long = 0,55 @ColumnInfo(defaultValue = "NULL") val nextEpisodeNumber: Int? = null,56 @ColumnInfo(defaultValue = "0") val airingScheduleUpdatedAt: Long = 0,57 @ColumnInfo(defaultValue = "NULL") val parentId: Long? = null,58 @ColumnInfo(defaultValue = "'Episodes'") val fetchType: String = "Episodes",59 @ColumnInfo(defaultValue = "NULL") val seasonNumber: Double? = null,60)6162@Entity(63 tableName = "manga_credit",64 primaryKeys = ["mangaId", "position"],65 foreignKeys = [ForeignKey(entity = MangaRow::class, parentColumns = ["id"], childColumns = ["mangaId"], onDelete = ForeignKey.CASCADE)],66)67data class MangaCreditRow(68 val mangaId: Long,69 val position: Int,70 val name: String,71 val role: String? = null,72 val character: String? = null,73 val imageUrl: String? = null,74)7576@Dao77interface MangaCreditDao {78 @Query("SELECT * FROM manga_credit ORDER BY mangaId, position") suspend fun all(): List<MangaCreditRow>79 @Upsert suspend fun upsertAll(rows: List<MangaCreditRow>)80 @Query("DELETE FROM manga_credit WHERE mangaId = :mangaId") suspend fun deleteForManga(mangaId: Long)81 @Query("DELETE FROM manga_credit") suspend fun clear()82}8384@Entity(tableName = "chapter")85data class ChapterRow(86 @PrimaryKey val id: Long,87 val mangaId: Long? = null,88 val name: String? = null,89 val url: String = "",90 val dateUpload: String = "",91 val scanlator: String = "",92 val isBookmarked: Boolean = false,93 val isRead: Boolean = false,94 val lastPageRead: String = "",95 val archivePath: String = "",96 val isFiller: Boolean = false,97 val thumbnailUrl: String? = null,98 val description: String? = null,99 val downloadSize: String? = null,100 val duration: String? = null,101 val updatedAt: Long = 0,102 @ColumnInfo(defaultValue = "0") val fetchedAt: Long = 0,103 @ColumnInfo(defaultValue = "NULL") val number: Double? = null,104 @ColumnInfo(defaultValue = "NULL") val season: Int? = null,105 @ColumnInfo(defaultValue = "NULL") val numberingName: String? = null,106 @ColumnInfo(defaultValue = "NULL") val fillerOverride: Boolean? = null,107)108109@Entity(tableName = "category")110data class CategoryRow(111 @PrimaryKey val id: Long,112 val name: String,113 val forItemType: String,114 val position: Int? = null,115 val hidden: Boolean? = null,116 val shouldUpdate: Boolean? = null,117 val updatedAt: Long = 0,118)119120@Entity(tableName = "source")121data class SourceRow(122 @PrimaryKey val id: Long,123 val name: String = "",124 val baseUrl: String = "",125 val lang: String = "",126 val isActive: Boolean = true,127 val isAdded: Boolean = false,128 val isPinned: Boolean = false,129 val isNsfw: Boolean = false,130 val sourceCode: String = "",131 val sourceCodeUrl: String = "",132 val typeSource: String = "",133 val iconUrl: String = "",134 val isFullData: Boolean = false,135 val hasCloudflare: Boolean = false,136 val lastUsed: Boolean = false,137 val dateFormat: String = "",138 val dateFormatLocale: String = "",139 val apiUrl: String = "",140 val version: String = "0.0.1",141 val versionLast: String = "0.0.1",142 val headers: String = "",143 val supportLatest: Boolean? = null,144 val filterList: String? = null,145 val preferenceList: String? = null,146 val itemType: String,147 val appMinVerReq: String = "",148 val additionalParams: String = "",149 val isLocal: Boolean = false,150 val isObsolete: Boolean = false,151 val sourceCodeLanguage: String,152 val notes: String = "",153 val repoJson: String? = null,154 val updatedAt: Long = 0,155)156157@Entity(tableName = "history")158data class HistoryRow(159 @PrimaryKey val id: Long,160 val mangaId: Long? = null,161 val chapterId: Long? = null,162 val itemType: String,163 val date: String? = null,164 val updatedAt: Long = 0,165 val readingTimeSeconds: Int? = null,166)167168@Entity(tableName = "library_update")169data class UpdateRow(170 @PrimaryKey val id: Long,171 val mangaId: Long? = null,172 val chapterId: Long? = null,173 val chapterName: String? = null,174 val date: String? = null,175 val updatedAt: Long = 0,176)177178@Entity(tableName = "download")179data class DownloadRow(180 @PrimaryKey val id: Long,181 val chapterId: Long? = null,182 val succeeded: Int? = null,183 val failed: Int? = null,184 val total: Int? = null,185 val isDownload: Boolean? = null,186 val isStartDownload: Boolean? = null,187 @ColumnInfo(defaultValue = "0") val isPaused: Boolean = false,188 val storageLocation: String? = null,189 val requestJson: String? = null,190)191192@Entity(tableName = "track")193data class TrackRow(194 @PrimaryKey val id: Long,195 val libraryId: Long? = null,196 val mediaId: Long? = null,197 val mangaId: Long? = null,198 val syncId: Int? = null,199 val title: String? = null,200 val lastChapterRead: Int? = null,201 val totalChapter: Int? = null,202 val score: Int? = null,203 val status: String,204 val startedReadingDate: Long? = null,205 val finishedReadingDate: Long? = null,206 val trackingUrl: String? = null,207 val itemType: String,208 val updatedAt: Long = 0,209)210211@Entity(tableName = "track_preference")212data class TrackPreferenceRow(213 @PrimaryKey val syncId: Long,214 val username: String? = null,215 val oauth: String? = null,216 val prefs: String? = null,217 val refreshing: Boolean? = null,218)219220@Entity(tableName = "track_search")221data class TrackSearchRow(222 @PrimaryKey(autoGenerate = true) val rowId: Long = 0,223 val id: Long? = null,224 val libraryId: Long? = null,225 val mediaId: Long? = null,226 val syncId: Int? = null,227 val title: String? = null,228 val lastChapterRead: Int? = null,229 val totalChapter: Int? = null,230 val score: Double? = null,231 val status: String = "",232 val startedReadingDate: Long? = null,233 val finishedReadingDate: Long? = null,234 val trackingUrl: String? = null,235 val coverUrl: String = "",236 val summary: String = "",237 val publishingStatus: String = "",238 val publishingType: String = "",239 val startDate: String = "",240)241242@Entity(tableName = "sync_preference")243data class SyncPreferenceRow(244 @PrimaryKey val syncId: Long,245 val email: String? = null,246 val authToken: String? = null,247 val lastSyncManga: Long? = null,248 val lastSyncHistory: Long? = null,249 val lastSyncUpdate: Long? = null,250 val server: String? = null,251 val syncOn: Boolean = false,252 val autoSyncFrequency: Int = 0,253 val syncHistories: Boolean = false,254 val syncUpdates: Boolean = false,255 val syncSettings: Boolean = false,256 @ColumnInfo(defaultValue = "0") val hubHead: Long = 0,257 @ColumnInfo(defaultValue = "0") val hubEpoch: Int = 0,258 @ColumnInfo(defaultValue = "0") val hubRecoveryPending: Boolean = false,259 @ColumnInfo(defaultValue = "1") val hubBootstrapComplete: Boolean = false,260 @ColumnInfo(defaultValue = "0") val hubHlc: Long = 0,261 val deviceId: String? = null,262 @ColumnInfo(defaultValue = "'YokanHub'") val serverProvider: String = "YokanHub",263 val mangayomiIdMappings: String? = null,264)265266@Entity(tableName = "changed_part")267data class ChangedPartRow(268 @PrimaryKey val id: Long,269 val actionType: String,270 val isarId: Long? = null,271 val data: String,272 val clientDate: Long,273)274275/** Unpushed hub ops; see `DbOutboxOp`. */276@Entity(tableName = "outbox", primaryKeys = ["kind", "id", "path"])277data class OutboxRow(278 val kind: Int,279 val id: Long,280 val path: String,281 val value: String? = null,282 val hlc: Long,283 val seq: Long,284 @ColumnInfo(defaultValue = "0") val epoch: Int = 0,285)286287@Entity(tableName = "custom_button")288data class CustomButtonRow(289 @PrimaryKey val id: Long,290 val title: String? = null,291 val codePress: String? = null,292 val codeLongPress: String = "",293 val codeStartup: String = "",294 val isFavourite: Boolean = false,295 val position: Int,296 val updatedAt: Long = 0,297)298299@Entity(tableName = "page_cache")300data class PageCacheRow(301 @PrimaryKey val chapterId: Long,302 val chapterUrl: String? = null,303 val urls: String? = null,304 val headers: String? = null,305 val fetchedAt: Long? = null,306 val sourceId: Long? = null,307 val sourceVersion: String? = null,308)309310/** Single-row settings table; payload is the whole DbSettings serialized to JSON. */311@Entity(tableName = "settings")312data class SettingsRow(313 @PrimaryKey val id: Long = 1L,314 val json: String,315)316317/** Durable host-owned recipe catalog checkpoint. These rows are intentionally excluded from legacy snapshots. */318@Entity(tableName = "recipe_catalog_checkpoint")319data class RecipeCatalogCheckpointRow(320 @PrimaryKey val catalogId: String,321 val generation: Long,322 val replayRevision: Long? = null,323 val replayPayloadHash: ByteArray? = null,324)325326@Entity(tableName = "recipe_catalog_trust_key", primaryKeys = ["catalogId", "keyId"])327data class RecipeCatalogTrustKeyRow(328 val catalogId: String,329 val keyId: String,330 val rawEd25519PublicKey: ByteArray,331 val state: String,332 val validFromEpochSeconds: Long? = null,333 val validUntilEpochSeconds: Long? = null,334)335336@Entity(tableName = "recipe_catalog_installed", primaryKeys = ["catalogId", "stableId"])337data class RecipeCatalogInstalledRow(338 val catalogId: String,339 val stableId: String,340 val catalogRevision: Long,341 val releaseVersion: String,342 val sha256: String,343 val sizeBytes: Long,344 val artifactBytes: ByteArray,345 /** Host-owned source row identity; null is a v6 record awaiting lazy materialization. */346 val sourceId: Long? = null,347)348349// ─── DAOs ───────────────────────────────────────────────────────────────────350// Each DAO exposes only `all()` / `upsert()` / `delete()` / `clear()`. Reads351// are still served from the in-memory snapshot for synchronous access; the352// DAOs only run on startup (hydration) and on mutations (write-through).353354@Dao355interface MangaDao {356 @Query("SELECT * FROM manga") suspend fun all(): List<MangaRow>357 @Upsert suspend fun upsert(row: MangaRow)358 @Upsert suspend fun upsertAll(rows: List<MangaRow>)359 @Query("DELETE FROM manga") suspend fun clear()360 @Query("DELETE FROM manga WHERE id = :id") suspend fun delete(id: Long)361}362363@Dao364interface ChapterDao {365 @Query("SELECT * FROM chapter") suspend fun all(): List<ChapterRow>366 @Upsert suspend fun upsert(row: ChapterRow)367 @Upsert suspend fun upsertAll(rows: List<ChapterRow>)368 @Query("DELETE FROM chapter") suspend fun clear()369 @Query("DELETE FROM chapter WHERE id = :id") suspend fun delete(id: Long)370}371372@Dao373interface CategoryDao {374 @Query("SELECT * FROM category") suspend fun all(): List<CategoryRow>375 @Upsert suspend fun upsert(row: CategoryRow)376 @Upsert suspend fun upsertAll(rows: List<CategoryRow>)377 @Query("DELETE FROM category") suspend fun clear()378 @Query("DELETE FROM category WHERE id = :id") suspend fun delete(id: Long)379}380381@Dao382interface SourceDao {383 @Query("SELECT * FROM source") suspend fun all(): List<SourceRow>384 @Query("SELECT * FROM source WHERE id = :id") suspend fun source(id: Long): SourceRow?385 @Upsert suspend fun upsert(row: SourceRow)386 @Upsert suspend fun upsertAll(rows: List<SourceRow>)387 @Query("DELETE FROM source") suspend fun clear()388 @Query("DELETE FROM source WHERE id = :id") suspend fun delete(id: Long)389}390391@Dao392interface HistoryDao {393 @Query("SELECT * FROM history") suspend fun all(): List<HistoryRow>394 @Upsert suspend fun upsert(row: HistoryRow)395 @Upsert suspend fun upsertAll(rows: List<HistoryRow>)396 @Query("DELETE FROM history") suspend fun clear()397 @Query("DELETE FROM history WHERE id = :id") suspend fun delete(id: Long)398}399400@Dao401interface UpdateDao {402 @Query("SELECT * FROM library_update") suspend fun all(): List<UpdateRow>403 @Upsert suspend fun upsert(row: UpdateRow)404 @Upsert suspend fun upsertAll(rows: List<UpdateRow>)405 @Query("DELETE FROM library_update") suspend fun clear()406 @Query("DELETE FROM library_update WHERE id = :id") suspend fun delete(id: Long)407}408409@Dao410interface DownloadDao {411 @Query("SELECT * FROM download") suspend fun all(): List<DownloadRow>412 @Upsert suspend fun upsert(row: DownloadRow)413 @Upsert suspend fun upsertAll(rows: List<DownloadRow>)414 @Query("DELETE FROM download") suspend fun clear()415 @Query("DELETE FROM download WHERE id = :id") suspend fun delete(id: Long)416}417418@Dao419interface TrackDao {420 @Query("SELECT * FROM track") suspend fun all(): List<TrackRow>421 @Upsert suspend fun upsert(row: TrackRow)422 @Upsert suspend fun upsertAll(rows: List<TrackRow>)423 @Query("DELETE FROM track") suspend fun clear()424 @Query("DELETE FROM track WHERE id = :id") suspend fun delete(id: Long)425}426427@Dao428interface TrackPreferenceDao {429 @Query("SELECT * FROM track_preference") suspend fun all(): List<TrackPreferenceRow>430 @Upsert suspend fun upsert(row: TrackPreferenceRow)431 @Upsert suspend fun upsertAll(rows: List<TrackPreferenceRow>)432 @Query("DELETE FROM track_preference") suspend fun clear()433}434435@Dao436interface TrackSearchDao {437 @Query("SELECT * FROM track_search") suspend fun all(): List<TrackSearchRow>438 @Upsert suspend fun upsert(row: TrackSearchRow)439 @Upsert suspend fun upsertAll(rows: List<TrackSearchRow>)440 @Query("DELETE FROM track_search") suspend fun clear()441}442443@Dao444interface SyncPreferenceDao {445 @Query("SELECT * FROM sync_preference") suspend fun all(): List<SyncPreferenceRow>446 @Upsert suspend fun upsert(row: SyncPreferenceRow)447 @Upsert suspend fun upsertAll(rows: List<SyncPreferenceRow>)448 @Query("DELETE FROM sync_preference") suspend fun clear()449}450451@Dao452interface ChangedPartDao {453 @Query("SELECT * FROM changed_part") suspend fun all(): List<ChangedPartRow>454 @Upsert suspend fun upsert(row: ChangedPartRow)455 @Upsert suspend fun upsertAll(rows: List<ChangedPartRow>)456 @Query("DELETE FROM changed_part") suspend fun clear()457}458459@Dao460interface OutboxDao {461 @Query("SELECT * FROM outbox") suspend fun all(): List<OutboxRow>462 @Upsert suspend fun upsertAll(rows: List<OutboxRow>)463 @Query("DELETE FROM outbox WHERE kind = :kind AND id = :id AND path = :path AND seq = :seq")464 suspend fun deleteIfUnchanged(kind: Int, id: Long, path: String, seq: Long)465 @Query("DELETE FROM outbox") suspend fun clear()466 @Query("DELETE FROM outbox WHERE epoch = 0") suspend fun clearUnnegotiated()467}468469@Dao470interface CustomButtonDao {471 @Query("SELECT * FROM custom_button") suspend fun all(): List<CustomButtonRow>472 @Upsert suspend fun upsert(row: CustomButtonRow)473 @Upsert suspend fun upsertAll(rows: List<CustomButtonRow>)474 @Query("DELETE FROM custom_button") suspend fun clear()475 @Query("DELETE FROM custom_button WHERE id = :id") suspend fun delete(id: Long)476}477478@Dao479interface PageCacheDao {480 @Query("SELECT * FROM page_cache") suspend fun all(): List<PageCacheRow>481 @Upsert suspend fun upsert(row: PageCacheRow)482 @Upsert suspend fun upsertAll(rows: List<PageCacheRow>)483 @Query("DELETE FROM page_cache") suspend fun clear()484 @Query("DELETE FROM page_cache WHERE chapterId = :chapterId") suspend fun delete(chapterId: Long)485}486487@Dao488interface SettingsDao {489 @Query("SELECT * FROM settings WHERE id = 1") suspend fun get(): SettingsRow?490 @Upsert suspend fun upsert(row: SettingsRow)491}492493@Dao494interface RecipeInstallationDao {495 @Query("SELECT * FROM recipe_catalog_checkpoint WHERE catalogId = :catalogId") suspend fun checkpoint(catalogId: String): RecipeCatalogCheckpointRow?496 @Query("SELECT * FROM recipe_catalog_checkpoint") suspend fun allCheckpoints(): List<RecipeCatalogCheckpointRow>497 @Query("SELECT * FROM recipe_catalog_trust_key WHERE catalogId = :catalogId ORDER BY keyId") suspend fun trustKeys(catalogId: String): List<RecipeCatalogTrustKeyRow>498 @Query("SELECT * FROM recipe_catalog_installed WHERE catalogId = :catalogId ORDER BY stableId") suspend fun installed(catalogId: String): List<RecipeCatalogInstalledRow>499 @Query("SELECT * FROM recipe_catalog_installed ORDER BY catalogId, stableId") suspend fun allInstalled(): List<RecipeCatalogInstalledRow>500 @Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun insertCheckpoint(row: RecipeCatalogCheckpointRow): Long501 @Insert(onConflict = OnConflictStrategy.ABORT) suspend fun insertTrustKeys(rows: List<RecipeCatalogTrustKeyRow>)502 @Upsert suspend fun upsertCheckpoint(row: RecipeCatalogCheckpointRow)503 @Upsert suspend fun upsertInstalled(row: RecipeCatalogInstalledRow)504 @Query("DELETE FROM recipe_catalog_installed WHERE catalogId = :catalogId AND stableId = :stableId") suspend fun deleteInstalled(catalogId: String, stableId: String)505 @Query("DELETE FROM recipe_catalog_trust_key WHERE catalogId = :catalogId") suspend fun deleteTrustKeys(catalogId: String)506}507508@Database(509 entities = [510 MangaRow::class,511 MangaCreditRow::class,512 ChapterRow::class,513 CategoryRow::class,514 SourceRow::class,515 HistoryRow::class,516 UpdateRow::class,517 DownloadRow::class,518 TrackRow::class,519 TrackPreferenceRow::class,520 TrackSearchRow::class,521 SyncPreferenceRow::class,522 ChangedPartRow::class,523 CustomButtonRow::class,524 PageCacheRow::class,525 SettingsRow::class,526 OutboxRow::class,527 RecipeCatalogCheckpointRow::class,528 RecipeCatalogTrustKeyRow::class,529 RecipeCatalogInstalledRow::class,530 ],531 version = 16,532 exportSchema = true,533)534@ConstructedBy(YokanRoomDatabaseConstructor::class)535abstract class YokanRoomDatabase : RoomDatabase() {536 abstract fun mangaDao(): MangaDao537 abstract fun mangaCreditDao(): MangaCreditDao538 abstract fun chapterDao(): ChapterDao539 abstract fun categoryDao(): CategoryDao540 abstract fun sourceDao(): SourceDao541 abstract fun historyDao(): HistoryDao542 abstract fun updateDao(): UpdateDao543 abstract fun downloadDao(): DownloadDao544 abstract fun trackDao(): TrackDao545 abstract fun trackPreferenceDao(): TrackPreferenceDao546 abstract fun trackSearchDao(): TrackSearchDao547 abstract fun syncPreferenceDao(): SyncPreferenceDao548 abstract fun changedPartDao(): ChangedPartDao549 abstract fun customButtonDao(): CustomButtonDao550 abstract fun pageCacheDao(): PageCacheDao551 abstract fun settingsDao(): SettingsDao552 abstract fun outboxDao(): OutboxDao553 abstract fun recipeInstallationDao(): RecipeInstallationDao554}555556/** v1 → v2: hub sync. New `outbox` table, hub cursor columns on `sync_preference`. */557val MIGRATION_1_2: Migration = object : Migration(1, 2) {558 override fun migrate(connection: SQLiteConnection) {559 connection.execSQL(560 "CREATE TABLE IF NOT EXISTS `outbox` (`kind` INTEGER NOT NULL, `id` INTEGER NOT NULL, `path` TEXT NOT NULL, " +561 "`value` TEXT, `hlc` INTEGER NOT NULL, `seq` INTEGER NOT NULL, PRIMARY KEY(`kind`, `id`, `path`))",562 )563 connection.execSQL("ALTER TABLE `sync_preference` ADD COLUMN `hubHead` INTEGER NOT NULL DEFAULT 0")564 connection.execSQL("ALTER TABLE `sync_preference` ADD COLUMN `hubEpoch` INTEGER NOT NULL DEFAULT 0")565 connection.execSQL("ALTER TABLE `sync_preference` ADD COLUMN `hubHlc` INTEGER NOT NULL DEFAULT 0")566 connection.execSQL("ALTER TABLE `sync_preference` ADD COLUMN `deviceId` TEXT")567 }568}569570/** v2 → v3: tag outbox operations with the hub history epoch. */571val MIGRATION_2_3: Migration = object : Migration(2, 3) {572 override fun migrate(connection: SQLiteConnection) {573 connection.execSQL("ALTER TABLE `outbox` ADD COLUMN `epoch` INTEGER NOT NULL DEFAULT 0")574 }575}576577/** v3 → v4: persist interrupted hub rollback recovery. */578val MIGRATION_3_4: Migration = object : Migration(3, 4) {579 override fun migrate(connection: SQLiteConnection) {580 connection.execSQL("ALTER TABLE `sync_preference` ADD COLUMN `hubRecoveryPending` INTEGER NOT NULL DEFAULT 0")581 }582}583584/** v4 → v5: persist bootstrap eligibility independently of the hub cursor. */585val MIGRATION_4_5: Migration = object : Migration(4, 5) {586 override fun migrate(connection: SQLiteConnection) {587 connection.execSQL("ALTER TABLE `sync_preference` ADD COLUMN `hubBootstrapComplete` INTEGER NOT NULL DEFAULT 1")588 }589}590591/** v5 → v6: durable, host-owned recipe catalog checkpoints and artifact records. */592val MIGRATION_5_6: Migration = object : Migration(5, 6) {593 override fun migrate(connection: SQLiteConnection) {594 connection.execSQL("CREATE TABLE IF NOT EXISTS `recipe_catalog_checkpoint` (`catalogId` TEXT NOT NULL, `generation` INTEGER NOT NULL, `replayRevision` INTEGER, `replayPayloadHash` BLOB, PRIMARY KEY(`catalogId`))")595 connection.execSQL("CREATE TABLE IF NOT EXISTS `recipe_catalog_trust_key` (`catalogId` TEXT NOT NULL, `keyId` TEXT NOT NULL, `rawEd25519PublicKey` BLOB NOT NULL, `state` TEXT NOT NULL, `validFromEpochSeconds` INTEGER, `validUntilEpochSeconds` INTEGER, PRIMARY KEY(`catalogId`, `keyId`))")596 connection.execSQL("CREATE TABLE IF NOT EXISTS `recipe_catalog_installed` (`catalogId` TEXT NOT NULL, `stableId` TEXT NOT NULL, `catalogRevision` INTEGER NOT NULL, `releaseVersion` TEXT NOT NULL, `sha256` TEXT NOT NULL, `sizeBytes` INTEGER NOT NULL, `artifactBytes` BLOB NOT NULL, PRIMARY KEY(`catalogId`, `stableId`))")597 }598}599600/** v6 → v7: bind each signed recipe artifact to a durable, host-owned source row. */601val MIGRATION_6_7: Migration = object : Migration(6, 7) {602 override fun migrate(connection: SQLiteConnection) {603 connection.execSQL("ALTER TABLE `recipe_catalog_installed` ADD COLUMN `sourceId` INTEGER")604 }605}606607/** v7 → v8: page-list freshness. Existing lists get no `fetchedAt`, so they are refetched once. */608val MIGRATION_7_8: Migration = object : Migration(7, 8) {609 override fun migrate(connection: SQLiteConnection) {610 connection.execSQL("ALTER TABLE `page_cache` ADD COLUMN `fetchedAt` INTEGER")611 connection.execSQL("ALTER TABLE `page_cache` ADD COLUMN `sourceId` INTEGER")612 connection.execSQL("ALTER TABLE `page_cache` ADD COLUMN `sourceVersion` TEXT")613 }614}615616/** v8 → v9: keep the selected sync server provider independent of its configured URL. */617val MIGRATION_8_9: Migration = object : Migration(8, 9) {618 override fun migrate(connection: SQLiteConnection) {619 connection.execSQL("ALTER TABLE `sync_preference` ADD COLUMN `serverProvider` TEXT NOT NULL DEFAULT 'YokanHub'")620 }621}622623/** v9 → v10: persist stable Mangayomi-safe client IDs. */624val MIGRATION_9_10: Migration = object : Migration(9, 10) {625 override fun migrate(connection: SQLiteConnection) {626 connection.execSQL("ALTER TABLE `sync_preference` ADD COLUMN `mangayomiIdMappings` TEXT")627 }628}629630/** v10 → v11: private title notes, empty for existing libraries. */631val MIGRATION_10_11: Migration = object : Migration(10, 11) {632 override fun migrate(connection: SQLiteConnection) {633 connection.execSQL("ALTER TABLE `manga` ADD COLUMN `privateNote` TEXT NOT NULL DEFAULT ''")634 }635}636637/** v11 → v12: restore pause intent and exact download destinations/options. */638val MIGRATION_11_12: Migration = object : Migration(11, 12) {639 override fun migrate(connection: SQLiteConnection) {640 connection.execSQL("ALTER TABLE `download` ADD COLUMN `isPaused` INTEGER NOT NULL DEFAULT 0")641 connection.execSQL("ALTER TABLE `download` ADD COLUMN `storageLocation` TEXT")642 connection.execSQL("ALTER TABLE `download` ADD COLUMN `requestJson` TEXT")643 }644}645646/** v12 → v13: independent chapter discovery timestamps and provider-announced anime schedules. */647val MIGRATION_12_13: Migration = object : Migration(12, 13) {648 override fun migrate(connection: SQLiteConnection) {649 connection.execSQL("ALTER TABLE `chapter` ADD COLUMN `fetchedAt` INTEGER NOT NULL DEFAULT 0")650 connection.execSQL("ALTER TABLE `manga` ADD COLUMN `nextEpisodeAiringAt` INTEGER NOT NULL DEFAULT 0")651 connection.execSQL("ALTER TABLE `manga` ADD COLUMN `nextEpisodeNumber` INTEGER DEFAULT NULL")652 connection.execSQL("ALTER TABLE `manga` ADD COLUMN `airingScheduleUpdatedAt` INTEGER NOT NULL DEFAULT 0")653 }654}655656/** v13 → v14: source numbering independent of optional local episode display titles. */657val MIGRATION_13_14: Migration = object : Migration(13, 14) {658 override fun migrate(connection: SQLiteConnection) {659 connection.execSQL("ALTER TABLE `chapter` ADD COLUMN `number` REAL DEFAULT NULL")660 connection.execSQL("ALTER TABLE `chapter` ADD COLUMN `season` INTEGER DEFAULT NULL")661 connection.execSQL("ALTER TABLE `chapter` ADD COLUMN `numberingName` TEXT DEFAULT NULL")662 }663}664665val MIGRATION_14_15: Migration = object : Migration(14, 15) {666 override fun migrate(connection: SQLiteConnection) {667 connection.execSQL("CREATE TABLE IF NOT EXISTS `manga_credit` (`mangaId` INTEGER NOT NULL, `position` INTEGER NOT NULL, `name` TEXT NOT NULL, `role` TEXT, `character` TEXT, `imageUrl` TEXT, PRIMARY KEY(`mangaId`, `position`), FOREIGN KEY(`mangaId`) REFERENCES `manga`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE)")668 }669}670671/** v15 → v16: Animetail season records (parent link, fetch type, season number) and user filler overrides. */672val MIGRATION_15_16: Migration = object : Migration(15, 16) {673 override fun migrate(connection: SQLiteConnection) {674 connection.execSQL("ALTER TABLE `manga` ADD COLUMN `parentId` INTEGER DEFAULT NULL")675 connection.execSQL("ALTER TABLE `manga` ADD COLUMN `fetchType` TEXT NOT NULL DEFAULT 'Episodes'")676 connection.execSQL("ALTER TABLE `manga` ADD COLUMN `seasonNumber` REAL DEFAULT NULL")677 connection.execSQL("ALTER TABLE `chapter` ADD COLUMN `fillerOverride` INTEGER DEFAULT NULL")678 }679}680681@Suppress("KotlinNoActualForExpect")682expect object YokanRoomDatabaseConstructor : RoomDatabaseConstructor<YokanRoomDatabase> {683 override fun initialize(): YokanRoomDatabase684}685686expect fun createYokanRoomDatabase(profileId: eu.friedcutlet.yokan.domain.ProfileId = eu.friedcutlet.yokan.domain.ProfileId.Default): YokanRoomDatabase687688/** Driver, query lane and full migration chain shared by every [createYokanRoomDatabase] actual. */689internal fun RoomDatabase.Builder<YokanRoomDatabase>.buildYokanRoom(): YokanRoomDatabase = this690 .setDriver(BundledSQLiteDriver())691 .setQueryCoroutineContext(roomPersistenceDispatcher)692 .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9, MIGRATION_9_10, MIGRATION_10_11, MIGRATION_11_12, MIGRATION_12_13, MIGRATION_13_14, MIGRATION_14_15, MIGRATION_15_16)693 .build()694