package eu.friedcutlet.yokan.database.room import androidx.room.Room import androidx.sqlite.driver.bundled.BundledSQLiteDriver import androidx.sqlite.execSQL import eu.friedcutlet.yokan.database.* import java.io.File import java.nio.file.Files import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.* import kotlin.test.* class LibraryDatesMigrationTest { @Test fun version12UpgradesWithoutChangingProgressAndNewDatesSurviveReopen() { val directory = Files.createTempDirectory("yokan-library-dates-migration") val path = directory.resolve("yokan.db").toString() try { val schema = DatabaseJson.parseToJsonElement(File("schemas/eu.friedcutlet.yokan.database.room.YokanRoomDatabase/12.json").readText()) .jsonObject.getValue("database").jsonObject BundledSQLiteDriver().open(path).use { connection -> schema.getValue("entities").jsonArray.forEach { value -> val entity = value.jsonObject val table = entity.getValue("tableName").jsonPrimitive.content connection.execSQL(entity.getValue("createSql").jsonPrimitive.content.replace("\${TABLE_NAME}", table)) entity["indices"]?.jsonArray.orEmpty().forEach { index -> connection.execSQL(index.jsonObject.getValue("createSql").jsonPrimitive.content.replace("\${TABLE_NAME}", table)) } } schema.getValue("setupQueries").jsonArray.forEach { connection.execSQL(it.jsonPrimitive.content) } 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')") 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)") connection.execSQL("INSERT INTO settings(id,json) VALUES(1,'{\"mangaGridSize\":4}')") connection.execSQL("PRAGMA user_version = 12") } val room = Room.databaseBuilder(name = path).buildYokanRoom() try { runBlocking(roomPersistenceDispatcher) { val chapter = room.chapterDao().all().single().toDb() val title = room.mangaDao().all().single().toDb() assertEquals(0L, chapter.fetchedAt) assertEquals(0L, title.nextEpisodeAiringAt) assertNull(title.nextEpisodeNumber) assertEquals("7", chapter.lastPageRead) assertTrue(chapter.isRead) assertTrue(chapter.isBookmarked) assertEquals("/offline/file", chapter.archivePath) assertEquals("Saved note", title.privateNote) assertEquals(4, room.settingsDao().get()?.toDb()?.mangaGridSize) room.chapterDao().upsert(chapter.copy(fetchedAt = 1_800_000_000_000).toRow()) room.mangaDao().upsert(title.copy(nextEpisodeAiringAt = 1_800_000_100_000, nextEpisodeNumber = 12, airingScheduleUpdatedAt = 1_800_000_000_000).toRow()) } } finally { room.close() } val reopened = Room.databaseBuilder(name = path).buildYokanRoom() try { runBlocking(roomPersistenceDispatcher) { assertEquals(1_800_000_000_000, reopened.chapterDao().all().single().toDb().fetchedAt) val title = reopened.mangaDao().all().single().toDb() assertEquals(1_800_000_100_000, title.nextEpisodeAiringAt) assertEquals(12, title.nextEpisodeNumber) assertEquals(1_800_000_000_000, title.airingScheduleUpdatedAt) assertEquals("Saved note", title.privateNote) } } finally { reopened.close() } } finally { directory.toFile().deleteRecursively() } } }