Skip to content

Commit

Permalink
Add fallback destructive migration. Up version to 2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Garneg committed Dec 17, 2023
1 parent 393274d commit 545e1f3
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 181 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ android {
applicationId "com.garnegsoft.hubs"
minSdk 23
targetSdk 34
versionCode 10
versionName "2.3.2"
versionCode 13
versionName "2.4.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package com.garnegsoft.hubs.api.article.offline

import android.content.Context
import androidx.room.*
import androidx.room.migration.Migration
import kotlinx.coroutines.flow.Flow


private const val SNIPPETS_TABLE_NAME = "offline_articles_snippets"
private const val ARTICLES_TABLE_NAME = "offline_articles"
private const val DATABASE_NAME = "offline_db"

@Entity(
tableName = SNIPPETS_TABLE_NAME,
indices = [Index("article_id", unique = true)]
)
data class OfflineArticleSnippet(

@ColumnInfo("article_id")
val articleId: Int,

@ColumnInfo("author_name")
val authorName: String?,

@ColumnInfo("author_avatar_url")
val authorAvatarUrl: String?,

@ColumnInfo("time_published")
val timePublished: String,

val title: String,

@ColumnInfo("reading_time")
val readingTime: Int,

@ColumnInfo("is_translation")
val isTranslation: Boolean,

@ColumnInfo("text_snippet")
val textSnippet: String,

@ColumnInfo("thumbnail_url")
val thumbnailUrl: String?,

@TypeConverters(HubsConverter::class)
val hubs: HubsList,

@ColumnInfo
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
)

@Entity(
tableName = ARTICLES_TABLE_NAME,
)
data class OfflineArticle(
@ColumnInfo("article_id", index = true)
val articleId: Int,

@ColumnInfo("author_name")
val authorName: String?,

@ColumnInfo("author_avatar_url")
val authorAvatarUrl: String?,

@ColumnInfo("time_published")
val timePublished: String,

val title: String,

@ColumnInfo("reading_time")
val readingTime: Int,

@ColumnInfo("is_translation")
val isTranslation: Boolean,


@ColumnInfo("content_html")
val contentHtml: String,

@TypeConverters(HubsConverter::class)
val hubs: HubsList,

@PrimaryKey(autoGenerate = true)
val id: Int = 0,

)

@ProvidedTypeConverter
class HubsConverter {

@TypeConverter
fun fromHubs(hubs: HubsList): String {
return hubs.hubsList.joinToString(",")
}

@TypeConverter
fun fromString(hubs: String): HubsList {
return HubsList(hubs.split(","))
}


}

class HubsList(
val hubsList: List<String>
)

val Context.offlineArticlesDatabase: OfflineArticlesDatabase
get() = OfflineArticlesDatabase.getDb(this)


@Dao
interface OfflineArticlesDao {

/**
* @return article entity by **article_id**, not by room table id
*/
@Query("SELECT * FROM $ARTICLES_TABLE_NAME WHERE article_id = :articleId")
fun getArticleById(articleId: Int): OfflineArticle

@Query("SELECT EXISTS (SELECT * FROM $ARTICLES_TABLE_NAME WHERE article_id = :articleId)")
fun exists(articleId: Int): Boolean

@Query("SELECT EXISTS (SELECT * FROM $ARTICLES_TABLE_NAME WHERE article_id = :articleId)")
fun existsFlow(articleId: Int): Flow<Boolean>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertSnippet(entity: OfflineArticleSnippet)

@Delete
fun deleteSnippet(entity: OfflineArticleSnippet)

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(entity: OfflineArticle)

@Query("DELETE FROM $ARTICLES_TABLE_NAME WHERE article_id = :id")
fun delete(id: Int)

@Query("DELETE FROM $SNIPPETS_TABLE_NAME WHERE article_id = :id")
fun deleteSnippet(id: Int)

/**
* @return list of article entities from older to newer
*/
@Query("SELECT * FROM $SNIPPETS_TABLE_NAME ORDER BY id ASC")
fun getAllSnippetsSortedByIdAsc(): Flow<List<OfflineArticleSnippet>>

/**
* @return list of article entities from newer to older
*/
@Query("SELECT * FROM $SNIPPETS_TABLE_NAME ORDER BY id DESC")
fun getAllSnippetsSortedByIdDesc(): Flow<List<OfflineArticleSnippet>>

}

@TypeConverters(HubsConverter::class)
@Database(
entities = [OfflineArticleSnippet::class, OfflineArticle::class],
version = 2
)
abstract class OfflineArticlesDatabase : RoomDatabase() {

abstract fun articlesDao(): OfflineArticlesDao

companion object {
@Volatile
private var instance: OfflineArticlesDatabase? = null
fun getDb(context: Context): OfflineArticlesDatabase {
return instance ?: synchronized(this) {
val inst = Room.databaseBuilder(
context = context,
klass = OfflineArticlesDatabase::class.java,
name = DATABASE_NAME
)
.addTypeConverter(HubsConverter())
.fallbackToDestructiveMigration()
.build()
instance = inst
inst
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ internal fun UserProfile(
) {
whoIs?.let { whoIs ->
whoIs.badges.let {

TitledColumn(title = "Значки") {
FlowRow(
horizontalArrangement = Arrangement.spacedBy(8.dp),
Expand Down

0 comments on commit 545e1f3

Please sign in to comment.