Skip to content

Commit

Permalink
track a primary & parallel locale for each tool
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Nov 26, 2024
1 parent 5d79459 commit b0eedf6
Show file tree
Hide file tree
Showing 9 changed files with 814 additions and 1 deletion.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface ToolsRepository {
suspend fun unpinTool(code: String)

suspend fun storeToolOrder(tools: List<String>)
suspend fun updateToolLocales(code: String, primary: Locale?, parallel: Locale?)
suspend fun updateToolViews(code: String, delta: Int)

// region Initial Content Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import org.cru.godtools.db.room.repository.UserCountersRoomRepository
import org.cru.godtools.db.room.repository.UserRoomRepository

@Database(
version = 23,
version = 24,
entities = [
AttachmentEntity::class,
LanguageEntity::class,
Expand Down Expand Up @@ -78,6 +78,7 @@ import org.cru.godtools.db.room.repository.UserRoomRepository
AutoMigration(from = 20, to = 21),
AutoMigration(from = 21, to = 22),
AutoMigration(from = 22, to = 23),
AutoMigration(from = 23, to = 24),
],
)
@TypeConverters(Java8TimeConverters::class, LocaleConverter::class)
Expand Down Expand Up @@ -140,6 +141,7 @@ internal abstract class GodToolsRoomDatabase : RoomDatabase() {
* 21: 2024-01-26
* 22: 2024-04-30
* 23: 2024-06-13
* 24: 2024-04-22
*/

internal fun RoomDatabase.Builder<GodToolsRoomDatabase>.enableMigrations() = fallbackToDestructiveMigration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ internal interface ToolsDao {
suspend fun resetToolOrder()
@Query("UPDATE tools SET `order` = :order WHERE code = :code")
suspend fun updateToolOrder(code: String, order: Int)
@Query("UPDATE tools SET primaryLocale = :primary, parallelLocale = :parallel WHERE code = :code")
suspend fun updateToolLocales(code: String, primary: Locale?, parallel: Locale?)
@Query("UPDATE tools SET pendingShares = pendingShares + :views WHERE code = :code")
suspend fun updateToolViews(code: String, views: Int)
@Delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ internal class ToolEntity(
val isHidden: Boolean = false,
@ColumnInfo(defaultValue = "false")
val isSpotlight: Boolean = false,
val primaryLocale: Locale? = null,
val parallelLocale: Locale? = null,
@ColumnInfo(defaultValue = "")
val changedFields: String = "",
val apiId: Long? = null,
Expand All @@ -63,6 +65,8 @@ internal class ToolEntity(
isFavorite = tool.isFavorite,
isHidden = tool.isHidden,
isSpotlight = tool.isSpotlight,
primaryLocale = tool.primaryLocale,
parallelLocale = tool.parallelLocale,
apiId = tool.apiId,
changedFields = tool.changedFieldsStr,
)
Expand All @@ -88,6 +92,8 @@ internal class ToolEntity(
pendingShares = pendingShares,
metatoolCode = metatoolCode,
defaultVariantCode = defaultVariantCode,
primaryLocale = primaryLocale,
parallelLocale = parallelLocale,
apiId = apiId,
changedFieldsStr = changedFields,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ internal abstract class ToolsRoomRepository(private val db: GodToolsRoomDatabase
}
}

override suspend fun updateToolLocales(code: String, primary: Locale?, parallel: Locale?) =
dao.updateToolLocales(code, primary, parallel)

override suspend fun updateToolViews(code: String, delta: Int) = dao.updateToolViews(code, delta)

override suspend fun storeInitialTools(tools: Collection<Tool>) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ abstract class ToolsRepositoryIT {
}
// endregion storeToolOrder()

// region updateToolLocales()
@Test
fun `updateToolLocales()`() = testScope.runTest {
val tool = randomTool("tool")
repository.storeInitialTools(listOf(tool))

assertNotNull(repository.findTool("tool")) {
assertNull(it.primaryLocale)
assertNull(it.parallelLocale)
}
repository.updateToolLocales("tool", Locale.ENGLISH, Locale.FRENCH)
assertNotNull(repository.findTool("tool")) {
assertEquals(Locale.ENGLISH, it.primaryLocale)
assertEquals(Locale.FRENCH, it.parallelLocale)
}
}
// endregion updateToolLocales()

// region updateToolShares()
@Test
fun `updateToolViews()`() = testScope.runTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,28 @@ class GodToolsRoomDatabaseMigrationIT {
}
}

@Test
fun testMigrate23To24() {
val localesQuery = "SELECT code, primaryLocale, parallelLocale FROM tools WHERE code = 'kgp'"

// create v23 database
helper.createDatabase(GodToolsRoomDatabase.DATABASE_NAME, 23).use { db ->
db.execSQL("INSERT INTO tools (code, type) VALUES (?, ?)", arrayOf("kgp", Tool.Type.TRACT))
assertFailsWith<SQLException> { db.query(localesQuery) }
}

// run migration
helper.runMigrationsAndValidate(GodToolsRoomDatabase.DATABASE_NAME, 24, true, *MIGRATIONS).use { db ->
db.query(localesQuery).use {
assertEquals(1, it.count)
it.moveToFirst()
assertEquals("kgp", it.getStringOrNull(0))
assertEquals(null, it.getStringOrNull(1))
assertEquals(null, it.getStringOrNull(2))
}
}
}

private fun SupportSQLiteDatabase.dumpIndices(table: String) = query("PRAGMA index_list($table)").use { it ->
it.map { it.getString(1) }.associateWith { name ->
query("PRAGMA index_info($name)").use { it.map { it.getString(2) }.toSet() }
Expand Down
4 changes: 4 additions & 0 deletions library/model/src/main/kotlin/org/cru/godtools/model/Tool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class Tool(
val shares: Int = 0,
@JsonApiIgnore
val pendingShares: Int = 0,
@JsonApiIgnore
val primaryLocale: Locale? = null,
@JsonApiIgnore
val parallelLocale: Locale? = null,
metatoolCode: String? = null,
defaultVariantCode: String? = null,
@JsonApiId
Expand Down

0 comments on commit b0eedf6

Please sign in to comment.