Refactor: Use cascade delete for repo auth
This commit simplifies the `RepoDao` by removing the manual deletion of authentication records when a repository is deleted. Instead, it leverages Room's `onDelete = CASCADE` functionality in the `AuthenticationEntity`'s foreign key definition. This ensures that when a `RepoEntity` is deleted, its associated `AuthenticationEntity` records are automatically removed from the database. The `deleteRepo` and `deleteAuth` methods in `RepoDao` have been removed as they are no longer necessary. Additionally, `stream()` and `repo()` methods have been added to `RepoDao` for observing repository data.
This commit is contained in:
parent
d1d65a3aeb
commit
fa2ee28284
@ -2,20 +2,19 @@ package com.looker.droidify.data.local.dao
|
|||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
import androidx.room.Transaction
|
import com.looker.droidify.data.local.model.RepoEntity
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
interface RepoDao {
|
interface RepoDao {
|
||||||
|
|
||||||
|
@Query("SELECT * FROM repository")
|
||||||
|
fun stream(): Flow<List<RepoEntity>>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM repository WHERE id = :repoId")
|
||||||
|
fun repo(repoId: Int): Flow<RepoEntity>
|
||||||
|
|
||||||
@Query("DELETE FROM repository WHERE id = :id")
|
@Query("DELETE FROM repository WHERE id = :id")
|
||||||
suspend fun delete(id: Int)
|
suspend fun delete(id: Int)
|
||||||
|
|
||||||
@Query("DELETE FROM authentication WHERE repoId = :repoId")
|
|
||||||
suspend fun deleteAuth(repoId: Int)
|
|
||||||
|
|
||||||
@Transaction
|
|
||||||
suspend fun deleteRepo(repoId: Int) {
|
|
||||||
delete(repoId)
|
|
||||||
deleteAuth(repoId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.looker.droidify.data.local.model
|
|||||||
|
|
||||||
import androidx.room.Entity
|
import androidx.room.Entity
|
||||||
import androidx.room.ForeignKey
|
import androidx.room.ForeignKey
|
||||||
|
import androidx.room.ForeignKey.Companion.CASCADE
|
||||||
import androidx.room.PrimaryKey
|
import androidx.room.PrimaryKey
|
||||||
import com.looker.droidify.data.encryption.Encrypted
|
import com.looker.droidify.data.encryption.Encrypted
|
||||||
|
|
||||||
@ -12,8 +13,7 @@ import com.looker.droidify.data.encryption.Encrypted
|
|||||||
entity = RepoEntity::class,
|
entity = RepoEntity::class,
|
||||||
childColumns = ["repoId"],
|
childColumns = ["repoId"],
|
||||||
parentColumns = ["id"],
|
parentColumns = ["id"],
|
||||||
// Handles in dao
|
onDelete = CASCADE,
|
||||||
// onDelete = CASCADE,
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user