Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed May 6, 2024
1 parent 4e9c30b commit 9cbb477
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface HistoryRepository {

suspend fun clearHistory()

suspend fun clearEntriesOlderThan(minusDays: LocalDateTime)
suspend fun clearEntriesOlderThan(dateTime: LocalDateTime)
}

class RealHistoryRepository(
Expand Down Expand Up @@ -97,9 +97,9 @@ class RealHistoryRepository(
.also { cachedHistoryEntries = it }
}

override suspend fun clearEntriesOlderThan(minusDays: LocalDateTime) {
override suspend fun clearEntriesOlderThan(dateTime: LocalDateTime) {
cachedHistoryEntries = null
historyDao.deleteEntriesOlderThan(minusDays)
historyDao.deleteEntriesOlderThan(dateTime)
fetchAndCacheHistoryEntries()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,15 @@ interface HistoryDao {
@Query("DELETE FROM visits_list WHERE timestamp < :timestamp")
suspend fun deleteOldVisitsByTimestamp(timestamp: String)

@Query(
"SELECT * FROM history_entries WHERE id NOT IN (SELECT DISTINCT historyEntryId FROM visits_list) " +
"OR id IN (SELECT historyEntryId FROM visits_list WHERE timestamp < :timestamp)",
)
suspend fun getEntriesWithNoVisitsOrOlderThanTimestamp(timestamp: String): List<HistoryEntryWithVisits>
@Query("DELETE FROM history_entries WHERE id NOT IN (SELECT DISTINCT historyEntryId FROM visits_list)")
suspend fun deleteEntriesWithNoVisits()

@Transaction
suspend fun deleteEntriesOlderThan(dateTime: LocalDateTime) {
val timestamp = DatabaseDateFormatter.timestamp(dateTime)

deleteOldVisitsByTimestamp(timestamp)

delete(getEntriesWithNoVisitsOrOlderThanTimestamp(timestamp).map { it.historyEntry })
deleteEntriesWithNoVisits()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class HistoryDaoTest {
}

@Test
fun whenDeleteOldItemsWithNoOldEnoughItemsThenTheyAreDeleted() {
fun whenDeleteOldItemsWithOldEnoughItemsThenTheyAreDeleted() {
runTest {
val insertDate = LocalDateTime.of(2000, JANUARY, 1, 0, 0)
historyDao.updateOrInsertVisit("url", "title", "query", false, insertDate)
Expand Down

0 comments on commit 9cbb477

Please sign in to comment.