Forward-port lab3 SQLAlchemy and Alembic foundation
Carry source f591fa64e525679a31c9db7f942d36c789e1a12f forward from 72a1e57. Preserve lab4 attachment transactions, reconciliation locks, quarantine, and existing schema data through verified legacy adoption. Keep report and evidence work in progress untouched.
This commit is contained in:
@@ -3,12 +3,11 @@ from collections.abc import AsyncIterator
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
import asyncpg
|
||||
from httpx import AsyncClient
|
||||
from starlette.types import Message
|
||||
from test_attachments import storage, trio, upload
|
||||
|
||||
from conftest import APPS, DATABASE, actor, keyed, resolve, running_app, scalar
|
||||
from conftest import APPS, actor, keyed, resolve, running_app, scalar, sql_connection
|
||||
from messenger.attachments.domain.events import AttachmentDiscarded
|
||||
from messenger.attachments.entrypoints.sweeper import UploadSweeper
|
||||
from messenger.shared.application.messagebus import EventBus
|
||||
@@ -18,11 +17,8 @@ AGED = "UPDATE attachments SET created_at = created_at - interval '1 hour' WHERE
|
||||
|
||||
|
||||
async def execute(sql: str, *args: Any) -> None:
|
||||
connection = await asyncpg.connect(DATABASE["url"])
|
||||
try:
|
||||
await connection.execute(sql, *args)
|
||||
finally:
|
||||
await connection.close()
|
||||
async with sql_connection() as connection:
|
||||
await connection.exec_driver_sql(sql, args)
|
||||
|
||||
|
||||
def sweeper(http: AsyncClient) -> UploadSweeper:
|
||||
@@ -69,15 +65,18 @@ async def test_commit_failure_after_put_is_compensated(client: AsyncClient) -> N
|
||||
trigger = f"lab4_fail_{uuid4().hex[:8]}"
|
||||
await execute(
|
||||
f"""CREATE FUNCTION {trigger}() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN RAISE EXCEPTION 'injected' USING ERRCODE = '55P03'; END $$;
|
||||
CREATE TRIGGER {trigger} BEFORE UPDATE ON attachments FOR EACH ROW
|
||||
BEGIN RAISE EXCEPTION 'injected' USING ERRCODE = '55P03'; END $$"""
|
||||
)
|
||||
await execute(
|
||||
f"""CREATE TRIGGER {trigger} BEFORE UPDATE ON attachments FOR EACH ROW
|
||||
WHEN (NEW.filename = '{name}' AND NEW.status = 'ready') EXECUTE FUNCTION {trigger}()"""
|
||||
)
|
||||
before, seen = set(store.objects), await discarded(client)
|
||||
try:
|
||||
response = await upload(client, alice, chat, filename=name)
|
||||
finally:
|
||||
await execute(f"DROP TRIGGER {trigger} ON attachments; DROP FUNCTION {trigger}()")
|
||||
await execute(f"DROP TRIGGER {trigger} ON attachments")
|
||||
await execute(f"DROP FUNCTION {trigger}()")
|
||||
assert response.status_code == 503
|
||||
assert response.json()["error"]["code"] == "unavailable"
|
||||
assert await scalar("SELECT count(*) FROM attachments WHERE filename = $1", name) == 0
|
||||
|
||||
Reference in New Issue
Block a user