Don't manage ID manually, but use autoGenerate to ensure uniqueness

(across the lifetime of the db)
This commit is contained in:
tzugen 2022-03-27 12:03:59 +02:00
parent f121e297df
commit 8a5dbe491d
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
4 changed files with 7 additions and 14 deletions

View File

@ -10,9 +10,9 @@ import androidx.sqlite.db.SupportSQLiteDatabase
* This could be settings or data that are not specific to any remote music database * This could be settings or data that are not specific to any remote music database
*/ */
@Database( @Database(
entities = [ServerSetting::class], entities = [ServerSetting::class],
version = 4, version = 5,
exportSchema = true exportSchema = true
) )
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {

View File

@ -19,7 +19,8 @@ import androidx.room.PrimaryKey
*/ */
@Entity @Entity
data class ServerSetting( data class ServerSetting(
@PrimaryKey var id: Int, // Default ID is 0, which will trigger SQLite to generate a unique ID.
@PrimaryKey(autoGenerate = true) var id: Int = 0,
@ColumnInfo(name = "index") var index: Int, @ColumnInfo(name = "index") var index: Int,
@ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "url") var url: String, @ColumnInfo(name = "url") var url: String,
@ -37,6 +38,6 @@ data class ServerSetting(
@ColumnInfo(name = "podcastSupport") var podcastSupport: Boolean? = null @ColumnInfo(name = "podcastSupport") var podcastSupport: Boolean? = null
) { ) {
constructor() : this ( constructor() : this (
-1, 0, "", "", null, "", "", false, false, false, null, null 0, 0, "", "", null, "", "", false, false, false, null, null
) )
} }

View File

@ -69,12 +69,6 @@ interface ServerSettingDao {
@Query("SELECT COUNT(*) FROM serverSetting") @Query("SELECT COUNT(*) FROM serverSetting")
fun liveServerCount(): LiveData<Int?> fun liveServerCount(): LiveData<Int?>
/**
* Retrieves the greatest value of the Id column in the table
*/
@Query("SELECT MAX([id]) FROM serverSetting")
suspend fun getMaxId(): Int?
/** /**
* Retrieves the greatest value of the Index column in the table * Retrieves the greatest value of the Index column in the table
*/ */

View File

@ -127,7 +127,6 @@ class ServerSettingsModel(
appScope.launch { appScope.launch {
serverSetting.index = (repository.count() ?: 0) + 1 serverSetting.index = (repository.count() ?: 0) + 1
serverSetting.id = (repository.getMaxId() ?: 0) + 1
repository.insert(serverSetting) repository.insert(serverSetting)
Timber.d("saveNewItem saved server setting: $serverSetting") Timber.d("saveNewItem saved server setting: $serverSetting")
} }
@ -142,12 +141,11 @@ class ServerSettingsModel(
runBlocking { runBlocking {
demo.index = (repository.count() ?: 0) + 1 demo.index = (repository.count() ?: 0) + 1
demo.id = (repository.getMaxId() ?: 0) + 1
repository.insert(demo) repository.insert(demo)
Timber.d("Added demo server") Timber.d("Added demo server")
} }
return demo.id return demo.index
} }
/** /**