Skip to content

Commit ab47dfe

Browse files
committed
Corrected fusion which didn't increment the ids
1 parent ea3cc39 commit ab47dfe

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ android {
1919
minSdk = 26
2020
targetSdk = 36
2121
versionCode = 1
22-
versionName = "1.0"
22+
versionName = "1.0.5"
2323

2424
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2525
}

app/src/main/java/com/mouton/openwinemer/data/local/WineDao.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ interface WineDao {
5151
@Query("DELETE FROM wines")
5252
suspend fun clearAll()
5353

54-
@Insert(onConflict = OnConflictStrategy.REPLACE)
54+
@Insert(onConflict = OnConflictStrategy.IGNORE)
5555
suspend fun insertAll(wines: List<WineEntity>)
5656

5757
@Insert(onConflict = OnConflictStrategy.REPLACE)
5858
suspend fun insertOrUpdate(wines: List<WineEntity>)
5959

60+
@Query("SELECT MAX(id) FROM wines")
61+
suspend fun getLastId(): Long?
62+
6063
}

app/src/main/java/com/mouton/openwinemer/domain/usecase/BackupUseCase.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,19 @@ class BackupUseCase(
101101

102102
// 4) Remplacer ou fusionner
103103
if (replace) {
104+
// 🔥 MODE REMPLACER : on efface tout et on laisse Room recréer les IDs
104105
wineDao.clearAll()
105-
wineDao.insertAll(wines)
106+
val winesWithoutId = wines.map { it.copy(id = 0) }
107+
wineDao.insertAll(winesWithoutId)
106108
} else {
107-
wineDao.insertOrUpdate(wines)
109+
// 🔥 MODE FUSIONNER : on récupère le dernier ID existant
110+
val lastId = wineDao.getLastId() ?: 0L
111+
// On renumérote les vins importés pour éviter les collisions
112+
var nextId = lastId + 1
113+
val winesWithNewIds = wines.map { wine ->
114+
wine.copy(id = nextId++)
115+
}
116+
wineDao.insertOrUpdate(winesWithNewIds)
108117
}
109118
}
110119

0 commit comments

Comments
 (0)