Update database structure

pull/26/head
Koitharu 5 years ago
parent 5b9922d509
commit fbb92005a1

@ -83,6 +83,7 @@ class BackupRepository(private val db: MangaDatabase) {
jo.put("url", url) jo.put("url", url)
jo.put("public_url", publicUrl) jo.put("public_url", publicUrl)
jo.put("rating", rating) jo.put("rating", rating)
jo.put("nsfw", isNsfw)
jo.put("cover_url", coverUrl) jo.put("cover_url", coverUrl)
jo.put("large_cover_url", largeCoverUrl) jo.put("large_cover_url", largeCoverUrl)
jo.put("state", state) jo.put("state", state)

@ -8,6 +8,7 @@ import org.koitharu.kotatsu.core.db.entity.TagEntity
import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity
import org.koitharu.kotatsu.favourites.data.FavouriteEntity import org.koitharu.kotatsu.favourites.data.FavouriteEntity
import org.koitharu.kotatsu.history.data.HistoryEntity import org.koitharu.kotatsu.history.data.HistoryEntity
import org.koitharu.kotatsu.utils.ext.getBooleanOrDefault
import org.koitharu.kotatsu.utils.ext.getStringOrNull import org.koitharu.kotatsu.utils.ext.getStringOrNull
import org.koitharu.kotatsu.utils.ext.iterator import org.koitharu.kotatsu.utils.ext.iterator
import org.koitharu.kotatsu.utils.ext.map import org.koitharu.kotatsu.utils.ext.map
@ -72,6 +73,7 @@ class RestoreRepository(private val db: MangaDatabase) {
url = json.getString("url"), url = json.getString("url"),
publicUrl = json.getStringOrNull("public_url").orEmpty(), publicUrl = json.getStringOrNull("public_url").orEmpty(),
rating = json.getDouble("rating").toFloat(), rating = json.getDouble("rating").toFloat(),
isNsfw = json.getBooleanOrDefault("nsfw", false),
coverUrl = json.getString("cover_url"), coverUrl = json.getString("cover_url"),
largeCoverUrl = json.getStringOrNull("large_cover_url"), largeCoverUrl = json.getStringOrNull("large_cover_url"),
state = json.getStringOrNull("state"), state = json.getStringOrNull("state"),

@ -18,7 +18,8 @@ val databaseModule
Migration3To4(), Migration3To4(),
Migration4To5(), Migration4To5(),
Migration5To6(), Migration5To6(),
Migration6To7() Migration6To7(),
Migration7To8(),
).addCallback( ).addCallback(
DatabasePrePopulateCallback(androidContext().resources) DatabasePrePopulateCallback(androidContext().resources)
).build() ).build()

@ -15,8 +15,8 @@ import org.koitharu.kotatsu.history.data.HistoryEntity
entities = [ entities = [
MangaEntity::class, TagEntity::class, HistoryEntity::class, MangaTagsEntity::class, MangaEntity::class, TagEntity::class, HistoryEntity::class, MangaTagsEntity::class,
FavouriteCategoryEntity::class, FavouriteEntity::class, MangaPrefsEntity::class, FavouriteCategoryEntity::class, FavouriteEntity::class, MangaPrefsEntity::class,
TrackEntity::class, TrackLogEntity::class TrackEntity::class, TrackLogEntity::class, SuggestionEntity::class
], version = 7 ], version = 8
) )
abstract class MangaDatabase : RoomDatabase() { abstract class MangaDatabase : RoomDatabase() {

@ -13,14 +13,15 @@ data class MangaEntity(
@PrimaryKey(autoGenerate = false) @PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "manga_id") val id: Long, @ColumnInfo(name = "manga_id") val id: Long,
@ColumnInfo(name = "title") val title: String, @ColumnInfo(name = "title") val title: String,
@ColumnInfo(name = "alt_title") val altTitle: String? = null, @ColumnInfo(name = "alt_title") val altTitle: String?,
@ColumnInfo(name = "url") val url: String, @ColumnInfo(name = "url") val url: String,
@ColumnInfo(name = "public_url") val publicUrl: String, @ColumnInfo(name = "public_url") val publicUrl: String,
@ColumnInfo(name = "rating") val rating: Float = Manga.NO_RATING, //normalized value [0..1] or -1 @ColumnInfo(name = "rating") val rating: Float, //normalized value [0..1] or -1
@ColumnInfo(name = "nsfw") val isNsfw: Boolean,
@ColumnInfo(name = "cover_url") val coverUrl: String, @ColumnInfo(name = "cover_url") val coverUrl: String,
@ColumnInfo(name = "large_cover_url") val largeCoverUrl: String? = null, @ColumnInfo(name = "large_cover_url") val largeCoverUrl: String?,
@ColumnInfo(name = "state") val state: String? = null, @ColumnInfo(name = "state") val state: String?,
@ColumnInfo(name = "author") val author: String? = null, @ColumnInfo(name = "author") val author: String?,
@ColumnInfo(name = "source") val source: String @ColumnInfo(name = "source") val source: String
) { ) {
@ -30,6 +31,7 @@ data class MangaEntity(
altTitle = this.altTitle, altTitle = this.altTitle,
state = this.state?.let { MangaState.valueOf(it) }, state = this.state?.let { MangaState.valueOf(it) },
rating = this.rating, rating = this.rating,
isNsfw = this.isNsfw,
url = this.url, url = this.url,
publicUrl = this.publicUrl, publicUrl = this.publicUrl,
coverUrl = this.coverUrl, coverUrl = this.coverUrl,
@ -50,6 +52,7 @@ data class MangaEntity(
coverUrl = manga.coverUrl, coverUrl = manga.coverUrl,
altTitle = manga.altTitle, altTitle = manga.altTitle,
rating = manga.rating, rating = manga.rating,
isNsfw = manga.isNsfw,
state = manga.state?.name, state = manga.state?.name,
title = manga.title, title = manga.title,
author = manga.author author = manga.author

@ -0,0 +1,24 @@
package org.koitharu.kotatsu.core.db.entity
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.PrimaryKey
@Entity(
tableName = "suggestions",
foreignKeys = [
ForeignKey(
entity = MangaEntity::class,
parentColumns = ["manga_id"],
childColumns = ["manga_id"],
onDelete = ForeignKey.CASCADE
)
]
)
data class SuggestionEntity(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "manga_id", index = true) val mangaId: Long,
@ColumnInfo(name = "relevance") val relevance: Float,
@ColumnInfo(name = "created_at") val createdAt: Long = System.currentTimeMillis(),
)

@ -0,0 +1,13 @@
package org.koitharu.kotatsu.core.db.migrations
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
class Migration7To8 : Migration(7, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE manga ADD COLUMN nsfw INTEGER NOT NULL DEFAULT 0")
database.execSQL("CREATE TABLE IF NOT EXISTS suggestions (manga_id INTEGER NOT NULL, relevance REAL NOT NULL, created_at INTEGER NOT NULL, PRIMARY KEY(manga_id), FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )")
database.execSQL("CREATE INDEX IF NOT EXISTS index_suggestions_manga_id ON suggestions (manga_id)")
}
}

@ -11,6 +11,7 @@ data class Manga(
val url: String, // relative url for internal use val url: String, // relative url for internal use
val publicUrl: String, val publicUrl: String,
val rating: Float = NO_RATING, //normalized value [0..1] or -1 val rating: Float = NO_RATING, //normalized value [0..1] or -1
val isNsfw: Boolean = false,
val coverUrl: String, val coverUrl: String,
val largeCoverUrl: String? = null, val largeCoverUrl: String? = null,
val description: String? = null, //HTML val description: String? = null, //HTML

@ -34,6 +34,10 @@ fun JSONObject.getStringOrNull(name: String): String? = opt(name)?.takeUnless {
it === JSONObject.NULL it === JSONObject.NULL
}?.toString() }?.toString()
fun JSONObject.getBooleanOrDefault(name: String, defaultValue: Boolean): Boolean = opt(name)?.takeUnless {
it === JSONObject.NULL
} as? Boolean ?: defaultValue
operator fun JSONArray.iterator(): Iterator<JSONObject> = JSONIterator(this) operator fun JSONArray.iterator(): Iterator<JSONObject> = JSONIterator(this)
private class JSONIterator(private val array: JSONArray) : Iterator<JSONObject> { private class JSONIterator(private val array: JSONArray) : Iterator<JSONObject> {

Loading…
Cancel
Save