Migrate saved filters in place on their own key (#172)

This commit is contained in:
makiolaj committed 2026-11-26 16:51:57 +01:00
1 parent f9926a80b3
commit f745698b5b
3 files changed
+35 -22

No files matched your search

@@ -19,8 +19,9 @@ import java.net.URLEncoder
* URL-encoded, and list ids are qualified by the store they belong to
* (`OWN:3`), since each store numbers its lists on its own.
*
* The flat format of 1.4 and earlier, `id|name|due|lists|tags|match|priority`
* under another key, is read only by [migrate], which rewrites it.
* Lines without the `v2|` mark are the flat format of 1.4 and earlier,
* `id|name|due|lists|tags|match|priority`. They are still read, and [migrate]
* rewrites them once.
*
* Fixed once released — don't change without a data migration.
*/
@@ -59,6 +60,9 @@ internal object SavedFilterCodec {
fun remove(raw: String?, id: Long): String = lines(raw).filterNot { idOf(it) == id }.joinToString("\n")
/** Whether [raw] still holds lines of the flat format. */
fun needsMigration(raw: String?): Boolean = lines(raw).any { !isV2(it) }
/**
* [raw] with every flat line rewritten as a rule, lists of every store kept
* as they are. Lines that never read as a filter are dropped.
@@ -77,7 +81,7 @@ internal object SavedFilterCodec {
private fun isV2(line: String): Boolean = line.startsWith("$V2|")
private fun idOf(line: String): Long? = fields(line).takeIf { isV2(line) }?.getOrNull(1)?.toLongOrNull()
private fun idOf(line: String): Long? = fields(line).getOrNull(if (isV2(line)) 1 else 0)?.toLongOrNull()
private fun Rule.elsewhereRefs(): Set<String> = when (this) {
is Rule.InList -> elsewhere
@@ -101,7 +105,7 @@ internal object SavedFilterCodec {
listOf(V2, filter.id.toString(), encode(filter.name), encodeRule(filter.rule, mode)).joinToString("|")
private fun decodeLine(line: String, mode: String): SavedFilter? {
if (!isV2(line)) return null
if (!isV2(line)) return decodeFlatLine(line, mode)
val f = fields(line)
if (f.size != 4) return null
val id = f[1].toLongOrNull() ?: return null
@@ -4,21 +4,16 @@ import androidx.datastore.core.DataMigration
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
/**
* Copies the saved filters of 1.4 and earlier into [KEY] as rules, once (see
* [SavedFilterCodec.migrate]). The old key is left as it was, so going back to
* an older version still finds the filters it knew.
*/
/** Rewrites the saved filters of 1.4 and earlier as rules, in place and once (see [SavedFilterCodec.migrate]). */
internal object SavedFilterMigration : DataMigration<Preferences> {
val KEY = stringPreferencesKey("saved_filters_v2")
val LEGACY_KEY = stringPreferencesKey("saved_filters")
val KEY = stringPreferencesKey("saved_filters")
override suspend fun shouldMigrate(currentData: Preferences): Boolean =
currentData[KEY] == null && currentData[LEGACY_KEY] != null
SavedFilterCodec.needsMigration(currentData[KEY])
override suspend fun migrate(currentData: Preferences): Preferences =
currentData.toMutablePreferences().apply { this[KEY] = SavedFilterCodec.migrate(this[LEGACY_KEY]) }
currentData.toMutablePreferences().apply { this[KEY] = SavedFilterCodec.migrate(this[KEY]) }
override suspend fun cleanUp() = Unit
}
@@ -137,8 +137,13 @@ class SavedFilterCodecTest {
}
@Test
fun `flat lines are only read by the migration`() {
assertThat(SavedFilterCodec.decode(legacyLine(id = 1), "OWN")).isEmpty()
fun `flat and v2 lines are told apart per line`() {
val raw = legacyLine(id = 7, refs = listOf("OWN:2")) + "\n" + SavedFilterCodec.upsert(null, filter, "OWN")
assertThat(SavedFilterCodec.decode(raw, "OWN").map { it.id }).containsExactly(7L, 3L).inOrder()
assertThat(SavedFilterCodec.decode(raw, "OWN").first().criteria!!.listIds).containsExactly(2L)
assertThat(SavedFilterCodec.decode(SavedFilterCodec.remove(raw, 7), "OWN").map { it.id }).containsExactly(3L)
val renamed = SavedFilterCodec.upsert(raw, SavedFilter(7, "renamed", FilterCriteria(listIds = setOf(2L))), "OWN")
assertThat(renamed.lines().first()).startsWith("v2|7|renamed|")
}
@Test
@@ -183,7 +188,12 @@ class SavedFilterCodecTest {
"garbage",
legacyLine(id = 2, refs = listOf("OWN:nope")),
).joinToString("\n")
assertThat(SavedFilterCodec.needsMigration(raw)).isTrue()
val migrated = SavedFilterCodec.migrate(raw)
assertThat(SavedFilterCodec.needsMigration(migrated)).isFalse()
for (mode in listOf("OWN", "EXTERNAL")) {
assertThat(SavedFilterCodec.decode(migrated, mode)).isEqualTo(SavedFilterCodec.decode(raw, mode))
}
assertThat(migrated.lines()).hasSize(3)
assertThat(migrated.lines().all { it.startsWith("v2|") }).isTrue()
assertThat(SavedFilterCodec.migrate(migrated)).isEqualTo(migrated)
@@ -203,14 +213,18 @@ class SavedFilterCodecTest {
}
@Test
fun `the datastore migration runs once and keeps the old filters for older versions`() = runBlocking<Unit> {
val legacy = legacyLine(id = 1, refs = listOf("OWN:1"))
val prefs = preferencesOf(SavedFilterMigration.LEGACY_KEY to legacy)
fun `the datastore migration rewrites the key in place and is idempotent`() = runBlocking<Unit> {
val raw = legacyLine(id = 1, refs = listOf("OWN:1")) + "\n" + SavedFilterCodec.upsert(null, filter, "OWN")
val prefs = preferencesOf(SavedFilterMigration.KEY to raw)
assertThat(SavedFilterMigration.shouldMigrate(prefs)).isTrue()
val migrated = SavedFilterMigration.migrate(prefs)
assertThat(migrated[SavedFilterMigration.KEY]).startsWith("v2|1|")
assertThat(migrated[SavedFilterMigration.LEGACY_KEY]).isEqualTo(legacy)
assertThat(SavedFilterMigration.shouldMigrate(migrated)).isFalse()
val once = SavedFilterMigration.migrate(prefs)
assertThat(once[SavedFilterMigration.KEY]!!.lines().all { it.startsWith("v2|") }).isTrue()
assertThat(once.asMap().keys).containsExactly(SavedFilterMigration.KEY)
assertThat(SavedFilterMigration.shouldMigrate(once)).isFalse()
val twice = SavedFilterMigration.migrate(once)
assertThat(twice).isEqualTo(once)
assertThat(SavedFilterCodec.decode(twice[SavedFilterMigration.KEY], "OWN"))
.isEqualTo(SavedFilterCodec.decode(raw, "OWN"))
assertThat(SavedFilterMigration.shouldMigrate(preferencesOf())).isFalse()
}