Add the jobs, outbox and variants schema as an additive migration

Migration 0004 widens the attachment states to queued, processing and
failed, adds the staging and original keys, the published pipeline
version and the error code, and moves the lab 4 object key into them
while leaving the old column nullable until a contract step. New tables
hold the variants under the effect key (attachment, pipeline version,
name), the jobs with attempt, claims and lease, the outbox rows the
relay publishes and the audit of job transitions. Partial indexes serve
the relay scan of unpublished rows, the search for expired leases and
the staging cleanup.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-29 00:43:34 +03:00
co-authored by Claude Opus 5.5
parent d9bcc7324b
commit 64fdaa03f2
3 changed files with 160 additions and 4 deletions
+88 -2
View File
@@ -10,8 +10,31 @@ COUNTS = """
SELECT (SELECT count(*) FROM users), (SELECT count(*) FROM sessions),
(SELECT count(*) FROM chats), (SELECT count(*) FROM chat_members),
(SELECT count(*) FROM messages), (SELECT count(*) FROM idempotency_keys),
(SELECT count(*) FROM attachments), (SELECT count(*) FROM message_attachments)
(SELECT count(*) FROM attachments), (SELECT count(*) FROM message_attachments),
(SELECT count(*) FROM attachment_variants), (SELECT count(*) FROM jobs),
(SELECT count(*) FROM outbox), (SELECT count(*) FROM job_events)
"""
PIPELINE_COLUMNS = {
"attachments": {
"id",
"status",
"published_version",
"error_code",
"staging_key",
"original_key",
},
"attachment_variants": {
"attachment_id",
"pipeline_version",
"name",
"sha256",
"size_bytes",
"object_key",
},
"jobs": {"job_id", "attachment_id", "pipeline_version", "status", "attempt"},
"outbox": {"job_id", "payload", "published_at"},
"job_events": {"job_id", "attachment_id", "kind", "attempt"},
}
async def counts() -> list[int]:
@@ -25,7 +48,12 @@ async def counts() -> list[int]:
async def test_migrations_are_ordered_and_versioned() -> None:
versions = [version for version, _ in migrations()]
assert versions == sorted(versions)
assert versions[:3] == ["0001_init", "0002_idempotency_keys", "0003_attachments"]
assert versions[:4] == [
"0001_init",
"0002_idempotency_keys",
"0003_attachments",
"0004_jobs",
]
applied = await scalar("SELECT array_agg(version ORDER BY version) FROM schema_migrations")
assert applied == versions
@@ -149,3 +177,61 @@ async def test_message_links_only_attachments_of_its_chat() -> None:
finally:
await transaction.rollback()
await connection.close()
async def test_pipeline_tables_have_the_columns_the_suite_reads() -> None:
connection = await asyncpg.connect(DATABASE["url"])
try:
rows = await connection.fetch(
"SELECT table_name, column_name FROM information_schema.columns "
"WHERE table_schema = current_schema()"
)
finally:
await connection.close()
found: dict[str, set[str]] = {}
for row in rows:
found.setdefault(row["table_name"], set()).add(row["column_name"])
assert all(columns <= found.get(table, set()) for table, columns in PIPELINE_COLUMNS.items())
async def test_one_variant_row_per_effect_key() -> None:
user, chat, attachment = uuid4(), uuid4(), uuid4()
variant = (
"INSERT INTO attachment_variants (attachment_id, pipeline_version, name, object_key, "
"content_type, size_bytes, width, height, sha256, created_at) "
"VALUES ($1, 1, 'thumbnail', $2, 'image/jpeg', 1, 1, 1, repeat('a', 64), now())"
)
connection = await asyncpg.connect(DATABASE["url"])
transaction = connection.transaction()
await transaction.start()
try:
await connection.execute(
"INSERT INTO users (id, username, display_name, password_hash, created_at) "
"VALUES ($1, $2, 'A', '$argon2id$x', now())",
user,
f"u_{user.hex[:12]}",
)
await connection.execute(
"INSERT INTO chats (id, title, created_at) VALUES ($1, 'A', now())", chat
)
await connection.execute(
"INSERT INTO chat_members (chat_id, user_id, position, chat_created_at) "
"VALUES ($1, $2, 1, now())",
chat,
user,
)
await connection.execute(
"INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, "
"sha256, width, height, staging_key, status, created_at, updated_at) VALUES ($1, $2, "
"$3, 'a.png', 'image/png', 1, repeat('0', 64), 1, 1, $4, 'queued', now(), now())",
attachment,
chat,
user,
f"staging/{attachment}",
)
await connection.execute(variant, attachment, f"variants/{attachment}/1/a")
with pytest.raises(asyncpg.UniqueViolationError, match="attachment_variants_pkey"):
await connection.execute(variant, attachment, f"variants/{attachment}/1/b")
finally:
await transaction.rollback()
await connection.close()