LibraryDatesMigrationTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/database/room/LibraryDatesMigrationTest.kt
1package eu.friedcutlet.yokan.database.room23import androidx.room.Room4import androidx.sqlite.driver.bundled.BundledSQLiteDriver5import androidx.sqlite.execSQL6import eu.friedcutlet.yokan.database.*7import java.io.File8import java.nio.file.Files9import kotlinx.coroutines.runBlocking10import kotlinx.serialization.json.*11import kotlin.test.*1213class LibraryDatesMigrationTest {14 @Test fun version12UpgradesWithoutChangingProgressAndNewDatesSurviveReopen() {15 val directory = Files.createTempDirectory("yokan-library-dates-migration")16 val path = directory.resolve("yokan.db").toString()17 try {18 val schema = DatabaseJson.parseToJsonElement(File("schemas/eu.friedcutlet.yokan.database.room.YokanRoomDatabase/12.json").readText())19 .jsonObject.getValue("database").jsonObject20 BundledSQLiteDriver().open(path).use { connection ->21 schema.getValue("entities").jsonArray.forEach { value ->22 val entity = value.jsonObject23 val table = entity.getValue("tableName").jsonPrimitive.content24 connection.execSQL(entity.getValue("createSql").jsonPrimitive.content.replace("\${TABLE_NAME}", table))25 entity["indices"]?.jsonArray.orEmpty().forEach { index ->26 connection.execSQL(index.jsonObject.getValue("createSql").jsonPrimitive.content.replace("\${TABLE_NAME}", table))27 }28 }29 schema.getValue("setupQueries").jsonArray.forEach { connection.execSQL(it.jsonPrimitive.content) }30 connection.execSQL("INSERT INTO manga(id,name,link,status,itemType,favorite,lastRead,isLocalArchive,updatedAt,privateNote) VALUES(1,'Saved title','/title','Ongoing','Anime',1,123,0,321,'Saved note')")31 connection.execSQL("INSERT INTO chapter(id,mangaId,name,url,dateUpload,scanlator,isBookmarked,isRead,lastPageRead,archivePath,isFiller,updatedAt) VALUES(2,1,'Episode 1','/episode','2026-01-01','Group',1,1,'7','/offline/file',0,456)")32 connection.execSQL("INSERT INTO settings(id,json) VALUES(1,'{\"mangaGridSize\":4}')")33 connection.execSQL("PRAGMA user_version = 12")34 }35 val room = Room.databaseBuilder<YokanRoomDatabase>(name = path).buildYokanRoom()36 try {37 runBlocking(roomPersistenceDispatcher) {38 val chapter = room.chapterDao().all().single().toDb()39 val title = room.mangaDao().all().single().toDb()40 assertEquals(0L, chapter.fetchedAt)41 assertEquals(0L, title.nextEpisodeAiringAt)42 assertNull(title.nextEpisodeNumber)43 assertEquals("7", chapter.lastPageRead)44 assertTrue(chapter.isRead)45 assertTrue(chapter.isBookmarked)46 assertEquals("/offline/file", chapter.archivePath)47 assertEquals("Saved note", title.privateNote)48 assertEquals(4, room.settingsDao().get()?.toDb()?.mangaGridSize)49 room.chapterDao().upsert(chapter.copy(fetchedAt = 1_800_000_000_000).toRow())50 room.mangaDao().upsert(title.copy(nextEpisodeAiringAt = 1_800_000_100_000,51 nextEpisodeNumber = 12, airingScheduleUpdatedAt = 1_800_000_000_000).toRow())52 }53 } finally { room.close() }54 val reopened = Room.databaseBuilder<YokanRoomDatabase>(name = path).buildYokanRoom()55 try {56 runBlocking(roomPersistenceDispatcher) {57 assertEquals(1_800_000_000_000, reopened.chapterDao().all().single().toDb().fetchedAt)58 val title = reopened.mangaDao().all().single().toDb()59 assertEquals(1_800_000_100_000, title.nextEpisodeAiringAt)60 assertEquals(12, title.nextEpisodeNumber)61 assertEquals(1_800_000_000_000, title.airingScheduleUpdatedAt)62 assertEquals("Saved note", title.privateNote)63 }64 } finally { reopened.close() }65 } finally { directory.toFile().deleteRecursively() }66 }67}68