Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions app/src/main/java/com/anindra/messages/data/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1021,19 +1021,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<Any>(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))
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
)
}
}
2 changes: 1 addition & 1 deletion scripts
Submodule scripts updated 2 files
+29 −0 TODO.md
+132 −0 test-participant-upsert.sh