-
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sync progress not compatible between Cantook and web reader
Closes: #1477
- Loading branch information
Showing
4 changed files
with
62 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
komga/src/flyway/kotlin/db/migration/sqlite/V20240422132621__fix_read_progress_locators.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package db.migration.sqlite | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.fasterxml.jackson.databind.node.TextNode | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.flywaydb.core.api.migration.BaseJavaMigration | ||
import org.flywaydb.core.api.migration.Context | ||
import org.springframework.jdbc.core.JdbcTemplate | ||
import org.springframework.jdbc.datasource.SingleConnectionDataSource | ||
import java.io.ByteArrayOutputStream | ||
import java.util.zip.GZIPInputStream | ||
import java.util.zip.GZIPOutputStream | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
class V20240422132621__fix_read_progress_locators : BaseJavaMigration() { | ||
override fun migrate(context: Context) { | ||
val jdbcTemplate = JdbcTemplate(SingleConnectionDataSource(context.connection, true)) | ||
|
||
val readProgressList = jdbcTemplate.queryForList( | ||
"""select r.BOOK_ID, r.USER_ID, r.locator from READ_PROGRESS r where locator is not null""", | ||
) | ||
|
||
if (readProgressList.isNotEmpty()) { | ||
val mapper = ObjectMapper() | ||
|
||
readProgressList.mapNotNull { | ||
try { | ||
val locator = GZIPInputStream((it["LOCATOR"] as ByteArray).inputStream()).use { gz -> mapper.readTree(gz) } | ||
val href = locator["href"]?.asText() | ||
if (href == null) null | ||
else { | ||
val correctHref = href.replaceBefore("/resource/", "").removePrefix("/resource/") | ||
(locator as ObjectNode).replace("href", TextNode(correctHref)) | ||
val gzLocator = ByteArrayOutputStream().use { baos -> | ||
GZIPOutputStream(baos).use { gz -> | ||
mapper.writeValue(gz, locator) | ||
baos.toByteArray() | ||
} | ||
} | ||
arrayOf(gzLocator, it["BOOK_ID"], it["USER_ID"]) | ||
} | ||
} catch (e: Exception) { | ||
null | ||
} | ||
}.let { params -> | ||
logger.info { "Updating ${params.size} incorrect read progress locators" } | ||
jdbcTemplate.batchUpdate( | ||
"""update READ_PROGRESS set locator = ? where BOOK_ID = ? and USER_ID = ?""", | ||
params, | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters