Skip to content

Commit b0eedf6

Browse files
committed
track a primary & parallel locale for each tool
1 parent 5d79459 commit b0eedf6

9 files changed

Lines changed: 814 additions & 1 deletion

File tree

library/db/room-schemas/org.cru.godtools.db.room.GodToolsRoomDatabase/24.json

Lines changed: 755 additions & 0 deletions
Large diffs are not rendered by default.

library/db/src/main/kotlin/org/cru/godtools/db/repository/ToolsRepository.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface ToolsRepository {
3535
suspend fun unpinTool(code: String)
3636

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

4041
// region Initial Content Methods

library/db/src/main/kotlin/org/cru/godtools/db/room/GodToolsRoomDatabase.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import org.cru.godtools.db.room.repository.UserCountersRoomRepository
4545
import org.cru.godtools.db.room.repository.UserRoomRepository
4646

4747
@Database(
48-
version = 23,
48+
version = 24,
4949
entities = [
5050
AttachmentEntity::class,
5151
LanguageEntity::class,
@@ -78,6 +78,7 @@ import org.cru.godtools.db.room.repository.UserRoomRepository
7878
AutoMigration(from = 20, to = 21),
7979
AutoMigration(from = 21, to = 22),
8080
AutoMigration(from = 22, to = 23),
81+
AutoMigration(from = 23, to = 24),
8182
],
8283
)
8384
@TypeConverters(Java8TimeConverters::class, LocaleConverter::class)
@@ -140,6 +141,7 @@ internal abstract class GodToolsRoomDatabase : RoomDatabase() {
140141
* 21: 2024-01-26
141142
* 22: 2024-04-30
142143
* 23: 2024-06-13
144+
* 24: 2024-04-22
143145
*/
144146

145147
internal fun RoomDatabase.Builder<GodToolsRoomDatabase>.enableMigrations() = fallbackToDestructiveMigration()

library/db/src/main/kotlin/org/cru/godtools/db/room/dao/ToolsDao.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ internal interface ToolsDao {
6464
suspend fun resetToolOrder()
6565
@Query("UPDATE tools SET `order` = :order WHERE code = :code")
6666
suspend fun updateToolOrder(code: String, order: Int)
67+
@Query("UPDATE tools SET primaryLocale = :primary, parallelLocale = :parallel WHERE code = :code")
68+
suspend fun updateToolLocales(code: String, primary: Locale?, parallel: Locale?)
6769
@Query("UPDATE tools SET pendingShares = pendingShares + :views WHERE code = :code")
6870
suspend fun updateToolViews(code: String, views: Int)
6971
@Delete

library/db/src/main/kotlin/org/cru/godtools/db/room/entity/ToolEntity.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ internal class ToolEntity(
3838
val isHidden: Boolean = false,
3939
@ColumnInfo(defaultValue = "false")
4040
val isSpotlight: Boolean = false,
41+
val primaryLocale: Locale? = null,
42+
val parallelLocale: Locale? = null,
4143
@ColumnInfo(defaultValue = "")
4244
val changedFields: String = "",
4345
val apiId: Long? = null,
@@ -63,6 +65,8 @@ internal class ToolEntity(
6365
isFavorite = tool.isFavorite,
6466
isHidden = tool.isHidden,
6567
isSpotlight = tool.isSpotlight,
68+
primaryLocale = tool.primaryLocale,
69+
parallelLocale = tool.parallelLocale,
6670
apiId = tool.apiId,
6771
changedFields = tool.changedFieldsStr,
6872
)
@@ -88,6 +92,8 @@ internal class ToolEntity(
8892
pendingShares = pendingShares,
8993
metatoolCode = metatoolCode,
9094
defaultVariantCode = defaultVariantCode,
95+
primaryLocale = primaryLocale,
96+
parallelLocale = parallelLocale,
9197
apiId = apiId,
9298
changedFieldsStr = changedFields,
9399
)

library/db/src/main/kotlin/org/cru/godtools/db/room/repository/ToolsRoomRepository.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ internal abstract class ToolsRoomRepository(private val db: GodToolsRoomDatabase
5656
}
5757
}
5858

59+
override suspend fun updateToolLocales(code: String, primary: Locale?, parallel: Locale?) =
60+
dao.updateToolLocales(code, primary, parallel)
61+
5962
override suspend fun updateToolViews(code: String, delta: Int) = dao.updateToolViews(code, delta)
6063

6164
override suspend fun storeInitialTools(tools: Collection<Tool>) =

library/db/src/test/kotlin/org/cru/godtools/db/repository/ToolsRepositoryIT.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,24 @@ abstract class ToolsRepositoryIT {
388388
}
389389
// endregion storeToolOrder()
390390

391+
// region updateToolLocales()
392+
@Test
393+
fun `updateToolLocales()`() = testScope.runTest {
394+
val tool = randomTool("tool")
395+
repository.storeInitialTools(listOf(tool))
396+
397+
assertNotNull(repository.findTool("tool")) {
398+
assertNull(it.primaryLocale)
399+
assertNull(it.parallelLocale)
400+
}
401+
repository.updateToolLocales("tool", Locale.ENGLISH, Locale.FRENCH)
402+
assertNotNull(repository.findTool("tool")) {
403+
assertEquals(Locale.ENGLISH, it.primaryLocale)
404+
assertEquals(Locale.FRENCH, it.parallelLocale)
405+
}
406+
}
407+
// endregion updateToolLocales()
408+
391409
// region updateToolShares()
392410
@Test
393411
fun `updateToolViews()`() = testScope.runTest {

library/db/src/test/kotlin/org/cru/godtools/db/room/GodToolsRoomDatabaseMigrationIT.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,28 @@ class GodToolsRoomDatabaseMigrationIT {
376376
}
377377
}
378378

379+
@Test
380+
fun testMigrate23To24() {
381+
val localesQuery = "SELECT code, primaryLocale, parallelLocale FROM tools WHERE code = 'kgp'"
382+
383+
// create v23 database
384+
helper.createDatabase(GodToolsRoomDatabase.DATABASE_NAME, 23).use { db ->
385+
db.execSQL("INSERT INTO tools (code, type) VALUES (?, ?)", arrayOf("kgp", Tool.Type.TRACT))
386+
assertFailsWith<SQLException> { db.query(localesQuery) }
387+
}
388+
389+
// run migration
390+
helper.runMigrationsAndValidate(GodToolsRoomDatabase.DATABASE_NAME, 24, true, *MIGRATIONS).use { db ->
391+
db.query(localesQuery).use {
392+
assertEquals(1, it.count)
393+
it.moveToFirst()
394+
assertEquals("kgp", it.getStringOrNull(0))
395+
assertEquals(null, it.getStringOrNull(1))
396+
assertEquals(null, it.getStringOrNull(2))
397+
}
398+
}
399+
}
400+
379401
private fun SupportSQLiteDatabase.dumpIndices(table: String) = query("PRAGMA index_list($table)").use { it ->
380402
it.map { it.getString(1) }.associateWith { name ->
381403
query("PRAGMA index_info($name)").use { it.map { it.getString(2) }.toSet() }

library/model/src/main/kotlin/org/cru/godtools/model/Tool.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class Tool(
7575
val shares: Int = 0,
7676
@JsonApiIgnore
7777
val pendingShares: Int = 0,
78+
@JsonApiIgnore
79+
val primaryLocale: Locale? = null,
80+
@JsonApiIgnore
81+
val parallelLocale: Locale? = null,
7882
metatoolCode: String? = null,
7983
defaultVariantCode: String? = null,
8084
@JsonApiId

0 commit comments

Comments
 (0)