Forward-port lab3 SQLAlchemy and Alembic foundation
Carry sourcef591fa64e5forward from72a1e57. 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:
@@ -7,13 +7,18 @@ from datetime import UTC, datetime, timedelta
|
||||
from io import BytesIO
|
||||
from uuid import uuid4
|
||||
|
||||
import asyncpg
|
||||
from PIL import Image
|
||||
from sqlalchemy import func, insert, make_url, select
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
from messenger.attachments.config import AttachmentsSettings
|
||||
from messenger.attachments.infrastructure.postgres import AttachmentRow
|
||||
from messenger.attachments.infrastructure.s3 import S3ObjectStorage, s3_client
|
||||
from messenger.identity.infrastructure.postgres import UserRow
|
||||
from messenger.messaging.infrastructure.postgres import ChatMemberRow
|
||||
from messenger.shared.config import PlatformSettings
|
||||
|
||||
MODELS = (UserRow, ChatMemberRow, AttachmentRow)
|
||||
COLUMNS = (
|
||||
"id",
|
||||
"chat_id",
|
||||
@@ -41,9 +46,21 @@ async def seed(count: int, concurrency: int) -> dict[str, object]:
|
||||
platform, settings = PlatformSettings(), AttachmentsSettings()
|
||||
body = picture()
|
||||
digest = hashlib.sha256(body).hexdigest()
|
||||
connection = await asyncpg.connect(platform.database_url.get_secret_value())
|
||||
engine = create_async_engine(
|
||||
make_url(platform.database_url.get_secret_value()).set(drivername="postgresql+asyncpg"),
|
||||
hide_parameters=True,
|
||||
)
|
||||
try:
|
||||
member = await connection.fetchrow("SELECT chat_id, user_id FROM chat_members LIMIT 1")
|
||||
async with engine.connect() as connection:
|
||||
member = (
|
||||
(
|
||||
await connection.execute(
|
||||
select(ChatMemberRow.chat_id, ChatMemberRow.user_id).limit(1)
|
||||
)
|
||||
)
|
||||
.mappings()
|
||||
.first()
|
||||
)
|
||||
if member is None:
|
||||
raise SystemExit("нет ни одного чата: сначала make test")
|
||||
ids = [uuid4() for _ in range(count)]
|
||||
@@ -85,17 +102,22 @@ async def seed(count: int, concurrency: int) -> dict[str, object]:
|
||||
)
|
||||
for number, attachment_id in enumerate(ids)
|
||||
]
|
||||
await connection.copy_records_to_table("attachments", records=records, columns=COLUMNS)
|
||||
total = await connection.fetchval("SELECT count(*) FROM attachments")
|
||||
async with engine.begin() as connection:
|
||||
for start in range(0, count, 1000):
|
||||
await connection.execute(
|
||||
insert(AttachmentRow),
|
||||
[dict(zip(COLUMNS, row, strict=True)) for row in records[start : start + 1000]],
|
||||
)
|
||||
total = await connection.scalar(select(func.count()).select_from(AttachmentRow))
|
||||
finally:
|
||||
await connection.close()
|
||||
await engine.dispose()
|
||||
return {
|
||||
"added": count,
|
||||
"rows_total": total,
|
||||
"object_bytes": len(body),
|
||||
"seconds": {
|
||||
"s3_put": round(stored - started, 3),
|
||||
"pg_copy": round(time.monotonic() - stored, 3),
|
||||
"pg_insert": round(time.monotonic() - stored, 3),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user