From 13144ae9b1cb7a725dc846179cd8498058bafad2 Mon Sep 17 00:00:00 2001 From: Keworker <79995640807la@gmail.com> Date: Fri, 4 Aug 2023 22:55:17 +0500 Subject: [PATCH] Replace Insert with Upsert at PlantDao. (#910) * Update packaging.yaml This project doesn't use Jetpack DataStore. * Update Room version. Replace Insert with Upsert at PlantDao. --- .../samples/apps/sunflower/data/GardenPlantingDaoTest.kt | 2 +- .../com/google/samples/apps/sunflower/data/PlantDaoTest.kt | 2 +- .../com/google/samples/apps/sunflower/data/PlantDao.kt | 7 +++---- .../samples/apps/sunflower/workers/SeedDatabaseWorker.kt | 2 +- gradle/libs.versions.toml | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt index 6c9734ced..ade47cbaf 100644 --- a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt +++ b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt @@ -47,7 +47,7 @@ class GardenPlantingDaoTest { database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build() gardenPlantingDao = database.gardenPlantingDao() - database.plantDao().insertAll(testPlants) + database.plantDao().upsertAll(testPlants) testGardenPlantingId = gardenPlantingDao.insertGardenPlanting(testGardenPlanting) } diff --git a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt index 709604cae..8f06f12a5 100644 --- a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt +++ b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt @@ -47,7 +47,7 @@ class PlantDaoTest { plantDao = database.plantDao() // Insert plants in non-alphabetical order to test that results are sorted by name - plantDao.insertAll(listOf(plantB, plantC, plantA)) + plantDao.upsertAll(listOf(plantB, plantC, plantA)) } @After fun closeDb() { diff --git a/app/src/main/java/com/google/samples/apps/sunflower/data/PlantDao.kt b/app/src/main/java/com/google/samples/apps/sunflower/data/PlantDao.kt index 1abdca465..e6825fd92 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/data/PlantDao.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/data/PlantDao.kt @@ -17,9 +17,8 @@ package com.google.samples.apps.sunflower.data import androidx.room.Dao -import androidx.room.Insert -import androidx.room.OnConflictStrategy import androidx.room.Query +import androidx.room.Upsert import kotlinx.coroutines.flow.Flow /** @@ -36,6 +35,6 @@ interface PlantDao { @Query("SELECT * FROM plants WHERE id = :plantId") fun getPlant(plantId: String): Flow - @Insert(onConflict = OnConflictStrategy.REPLACE) - suspend fun insertAll(plants: List) + @Upsert + suspend fun upsertAll(plants: List) } diff --git a/app/src/main/java/com/google/samples/apps/sunflower/workers/SeedDatabaseWorker.kt b/app/src/main/java/com/google/samples/apps/sunflower/workers/SeedDatabaseWorker.kt index aa3277dfa..a6058f02e 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/workers/SeedDatabaseWorker.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/workers/SeedDatabaseWorker.kt @@ -42,7 +42,7 @@ class SeedDatabaseWorker( val plantList: List = Gson().fromJson(jsonReader, plantType) val database = AppDatabase.getInstance(applicationContext) - database.plantDao().insertAll(plantList) + database.plantDao().upsertAll(plantList) Result.success() } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d7645583f..3a8bd7b01 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -51,7 +51,7 @@ pagingCompose = "1.0.0-alpha18" profileInstaller = "1.2.0" recyclerView = "1.3.0-alpha02" retrofit = "2.9.0" -room = "2.4.3" +room = "2.5.2" runner = "1.0.1" # @keep targetSdk = "33"