From 2b0e68e981c00c5e1aee000507bd016b394cde67 Mon Sep 17 00:00:00 2001 From: Anindra Date: Thu, 19 Nov 2026 08:58:10 +0530 Subject: [PATCH] fix: the participant row is written without SQLite 3.24 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit upsertParticipant wrote its row with INSERT ... ON CONFLICT DO UPDATE. That syntax arrived in SQLite 3.24 and Android 10 — the app's minimum — ships 3.22, where the statement is a syntax error. Every path that touches a person went through it: the startup migration, syncFromSystem, and creating a conversation. The visible symptom was a blank inbox: the import threw on the first new number, syncFromSystem logged its failure and no conversation was ever created, so a fresh install on Android 10 showed an empty app while the system still held the messages. The startup migration threw the same error from a coroutine, which took the whole process down. The write is now an UPDATE, then an INSERT when nothing matched, with the unique-constraint race caught and retried as an UPDATE. No statement needs anything newer than the platform's own SQLite, every column the old upsert kept is still written, and a non-phone sender still gets no participant row. --- .../com/anindra/messages/data/Repository.kt | 44 +++++++---- .../messages/data/ParticipantUpsertTest.kt | 77 +++++++++++++++++++ scripts | 2 +- 3 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 app/src/test/java/com/anindra/messages/data/ParticipantUpsertTest.kt diff --git a/app/src/main/java/com/anindra/messages/data/Repository.kt b/app/src/main/java/com/anindra/messages/data/Repository.kt index 02938c51..f0e43ecd 100644 --- a/app/src/main/java/com/anindra/messages/data/Repository.kt +++ b/app/src/main/java/com/anindra/messages/data/Repository.kt @@ -959,19 +959,37 @@ class Repository(private val context: Context) { if (!PhoneNumberUtils.isLikelyPhoneNumber(address)) return val region = PhoneNumberUtils.region() val e164 = PhoneNumberUtils.toE164(address, region) ?: return - database.execSQL( - """INSERT INTO participants( - normalized_destination,send_destination,display_destination, - comparable_destination,country_code,sub_id) - VALUES(?,?,?,?,?,?) - ON CONFLICT(normalized_destination) DO UPDATE SET - send_destination=excluded.send_destination, - display_destination=excluded.display_destination, - country_code=excluded.country_code, - sub_id=excluded.sub_id""", - arrayOf(e164, e164, PhoneNumberUtils.displayFor(e164, region), - e164.lowercase(), PhoneNumberUtils.regionFor(e164), subId) - ) + val display = PhoneNumberUtils.displayFor(e164, region) + val country = PhoneNumberUtils.regionFor(e164) + // UPDATE, then INSERT when nothing matched — not `INSERT … ON CONFLICT + // DO UPDATE`. Upsert needs SQLite 3.24 and Android 10 ships 3.22, where + // that statement is a syntax error: the migration runs on a background + // thread at startup, so the mistake took the whole process down on the + // first launch of any installation that had message history. + val refreshed = ContentValues().apply { + put("send_destination", e164) + put("display_destination", display) + put("country_code", country) + put("sub_id", subId) + } + if (database.update("participants", refreshed, "normalized_destination=?", arrayOf(e164)) > 0) { + return + } + val inserted = ContentValues().apply { + put("normalized_destination", e164) + put("send_destination", e164) + put("display_destination", display) + put("comparable_destination", e164.lowercase()) + put("country_code", country) + put("sub_id", subId) + } + try { + database.insertOrThrow("participants", null, inserted) + } catch (_: android.database.sqlite.SQLiteConstraintException) { + // Another writer inserted the row between the two statements; the + // row that exists now is the one to refresh. + database.update("participants", refreshed, "normalized_destination=?", arrayOf(e164)) + } } /** diff --git a/app/src/test/java/com/anindra/messages/data/ParticipantUpsertTest.kt b/app/src/test/java/com/anindra/messages/data/ParticipantUpsertTest.kt new file mode 100644 index 00000000..08ff5436 --- /dev/null +++ b/app/src/test/java/com/anindra/messages/data/ParticipantUpsertTest.kt @@ -0,0 +1,77 @@ +package com.anindra.messages.data + +import org.junit.Assert.assertTrue +import org.junit.Test +import java.io.File + +/** + * The participant row is written on every startup migration and on every new + * conversation, and the statement it used needed SQLite 3.24. Android 10 (the + * app's minimum) ships 3.22, where `ON CONFLICT … DO UPDATE` is a syntax error + * thrown on a background thread — the process died before any screen appeared. + */ +class ParticipantUpsertTest { + + private val main: File by lazy { + generateSequence(File("").absoluteFile) { it.parentFile } + .map { File(it, "app/src/main") } + .firstOrNull { File(it, "java").isDirectory } + ?: error("app/src/main not found") + } + + private val repository = File(main, "java/com/anindra/messages/data/Repository.kt").readText() + + private val upsert = repository.substringAfter("private fun upsertParticipant(") + .substringBefore("\n }\n") + + @Test + fun theParticipantWriteNeverNeedsANewerSqlite() { + assertTrue( + "upsert syntax is unavailable on Android 10, the minimum this app supports", + !repository.contains("ON CONFLICT(") + ) + assertTrue( + "the row is refreshed with an UPDATE first", + Regex("""database\.update\("participants",\s*refreshed,\s*"normalized_destination=\?",\s*arrayOf\(e164\)\)""") + .containsMatchIn(upsert) + ) + assertTrue( + "and inserted only when no row matched", + upsert.contains("insertOrThrow(\"participants\", null, inserted)") + ) + } + + @Test + fun everyColumnTheUpsertKeptIsStillWritten() { + listOf( + "normalized_destination", "send_destination", "display_destination", + "comparable_destination", "country_code", "sub_id" + ).forEach { column -> + assertTrue("$column is no longer written", upsert.contains("\"$column\"")) + } + assertTrue( + "a concurrent writer's row is refreshed rather than left stale", + Regex("""catch \(_: android\.database\.sqlite\.SQLiteConstraintException\)[\s\S]{0,200}?database\.update\("participants"""" ) + .containsMatchIn(upsert) + ) + assertTrue( + "non-phone senders still never get a participant row", + upsert.contains("PhoneNumberUtils.isLikelyPhoneNumber(address)") + ) + } + + @Test + fun theMigrationStillRunsOnceAndIsMarkedDone() { + val migration = repository.substringAfter("private fun runParticipantMigration()") + .substringBefore("\n }\n") + assertTrue( + "the flag is what keeps the pass off the next start", + migration.contains("settings.participantsMigrated = true") + ) + assertTrue( + "and the pass still walks the conversations", + migration.contains("SELECT id,address,sub_id FROM conversations") || + migration.contains("FROM conversations") + ) + } +} diff --git a/scripts b/scripts index 5161ab77..96984298 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 5161ab77b1de4a9c6be9b596d9f9080919147c32 +Subproject commit 969842981861d4ac5e76554249b96df682d93ba8