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:
LooKeR 2025-05-26 10:47:46 +05:30
parent d1d65a3aeb
commit 5a15b7e602
No known key found for this signature in database
GPG Key ID: 6B59369FDB608FB9
2 changed files with 10 additions and 10 deletions

View File

@ -2,20 +2,19 @@ package com.looker.droidify.data.local.dao
import androidx.room.Dao
import androidx.room.Query
import androidx.room.Transaction
import com.looker.droidify.data.local.model.RepoEntity
import kotlinx.coroutines.flow.Flow
@Dao
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")
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)
}
}

View File

@ -2,6 +2,7 @@ package com.looker.droidify.data.local.model
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.ForeignKey.Companion.CASCADE
import androidx.room.PrimaryKey
import com.looker.droidify.data.encryption.Encrypted
@ -13,7 +14,7 @@ import com.looker.droidify.data.encryption.Encrypted
childColumns = ["repoId"],
parentColumns = ["id"],
// Handles in dao
// onDelete = CASCADE,
onDelete = CASCADE,
),
],
)