Move to FK pragma and prep other code points in light of that

This commit is contained in:
Jack Kingsman
2026-04-01 21:22:01 -07:00
parent 47c4f038fe
commit 4a2d7ed100
6 changed files with 238 additions and 34 deletions
+28 -5
View File
@@ -1167,7 +1167,14 @@ class TestRawPacketRepository:
await RawPacketRepository.create(b"\x04\x05\x06", recent_timestamp)
# Insert old but decrypted packet (should NOT be deleted)
old_id, _ = await RawPacketRepository.create(b"\x07\x08\x09", old_timestamp)
await RawPacketRepository.mark_decrypted(old_id, 1)
msg_id = await MessageRepository.create(
msg_type="PRIV",
conversation_key="test_key",
text="test",
sender_timestamp=old_timestamp,
received_at=old_timestamp,
)
await RawPacketRepository.mark_decrypted(old_id, msg_id)
# Prune packets older than 10 days
deleted = await RawPacketRepository.prune_old_undecrypted(10)
@@ -1191,10 +1198,18 @@ class TestRawPacketRepository:
async def test_purge_linked_to_messages_deletes_only_linked_packets(self, test_db):
"""Purge linked raw packets removes only rows with a message_id."""
ts = int(time.time())
msg_id_1 = await MessageRepository.create(
msg_type="PRIV", conversation_key="k1", text="t1",
sender_timestamp=ts, received_at=ts,
)
msg_id_2 = await MessageRepository.create(
msg_type="PRIV", conversation_key="k2", text="t2",
sender_timestamp=ts, received_at=ts,
)
linked_1, _ = await RawPacketRepository.create(b"\x01\x02\x03", ts)
linked_2, _ = await RawPacketRepository.create(b"\x04\x05\x06", ts)
await RawPacketRepository.mark_decrypted(linked_1, 101)
await RawPacketRepository.mark_decrypted(linked_2, 102)
await RawPacketRepository.mark_decrypted(linked_1, msg_id_1)
await RawPacketRepository.mark_decrypted(linked_2, msg_id_2)
await RawPacketRepository.create(b"\x07\x08\x09", ts) # undecrypted, should remain
@@ -1232,10 +1247,18 @@ class TestMaintenanceEndpoint:
from app.routers.packets import MaintenanceRequest, run_maintenance
ts = int(time.time())
msg_id_1 = await MessageRepository.create(
msg_type="PRIV", conversation_key="k1", text="t1",
sender_timestamp=ts, received_at=ts,
)
msg_id_2 = await MessageRepository.create(
msg_type="PRIV", conversation_key="k2", text="t2",
sender_timestamp=ts, received_at=ts,
)
linked_1, _ = await RawPacketRepository.create(b"\x0a\x0b\x0c", ts)
linked_2, _ = await RawPacketRepository.create(b"\x0d\x0e\x0f", ts)
await RawPacketRepository.mark_decrypted(linked_1, 201)
await RawPacketRepository.mark_decrypted(linked_2, 202)
await RawPacketRepository.mark_decrypted(linked_1, msg_id_1)
await RawPacketRepository.mark_decrypted(linked_2, msg_id_2)
request = MaintenanceRequest(purge_linked_raw_packets=True)
result = await run_maintenance(request)
+19 -17
View File
@@ -513,7 +513,9 @@ class TestMigration018:
from hashlib import sha256
assert bytes(rows[0]["payload_hash"]) == sha256(b"hash_a").digest()
assert rows[1]["message_id"] == 42
# message_id=42 was orphaned (no matching messages row), so
# migration 49's orphan cleanup NULLs it out.
assert rows[1]["message_id"] is None
# Verify payload_hash unique index still works
cursor = await conn.execute(
@@ -1247,8 +1249,8 @@ class TestMigration039:
applied = await run_migrations(conn)
assert applied == 10
assert await get_version(conn) == 48
assert applied == 11
assert await get_version(conn) == 49
cursor = await conn.execute(
"""
@@ -1319,8 +1321,8 @@ class TestMigration039:
applied = await run_migrations(conn)
assert applied == 10
assert await get_version(conn) == 48
assert applied == 11
assert await get_version(conn) == 49
cursor = await conn.execute(
"""
@@ -1386,8 +1388,8 @@ class TestMigration039:
applied = await run_migrations(conn)
assert applied == 4
assert await get_version(conn) == 48
assert applied == 5
assert await get_version(conn) == 49
cursor = await conn.execute(
"""
@@ -1439,8 +1441,8 @@ class TestMigration040:
applied = await run_migrations(conn)
assert applied == 9
assert await get_version(conn) == 48
assert applied == 10
assert await get_version(conn) == 49
await conn.execute(
"""
@@ -1501,8 +1503,8 @@ class TestMigration041:
applied = await run_migrations(conn)
assert applied == 8
assert await get_version(conn) == 48
assert applied == 9
assert await get_version(conn) == 49
await conn.execute(
"""
@@ -1554,8 +1556,8 @@ class TestMigration042:
applied = await run_migrations(conn)
assert applied == 7
assert await get_version(conn) == 48
assert applied == 8
assert await get_version(conn) == 49
await conn.execute(
"""
@@ -1694,8 +1696,8 @@ class TestMigration046:
applied = await run_migrations(conn)
assert applied == 3
assert await get_version(conn) == 48
assert applied == 4
assert await get_version(conn) == 49
cursor = await conn.execute(
"""
@@ -1788,8 +1790,8 @@ class TestMigration047:
applied = await run_migrations(conn)
assert applied == 2
assert await get_version(conn) == 48
assert applied == 3
assert await get_version(conn) == 49
cursor = await conn.execute(
"""