fix(statements): вставка document_issue пакетами (#142, AURORA-1711) #143

Merged
aep merged 1 commits from feat/142-batch-document-issues into main 2026-10-20 19:12:27 +03:00
2 changed files with 46 additions and 29 deletions

View File

@ -59,7 +59,8 @@ __all__ = [
"value_flags",
]
# строк на один INSERT: у строки формы 15 колонок, у значения 10; 15 × 1000 < 32767 параметров asyncpg
# строк на один INSERT: у строки формы 15 колонок, у значения 10, у `document_issue` 16; 16 × 1000 < 32767 параметров
# asyncpg
BATCH = 1000
@ -151,34 +152,30 @@ async def insert_statement(
async def add_issues(
session: AsyncSession, document_id: uuid.UUID, drafts: Sequence[IssueDraft], *, now: datetime.datetime
) -> None:
"""Записи `document_issue` документа из черновиков (без commit)."""
if not drafts:
return
await session.execute(
insert(DocumentIssue).values(
[
{
"id": uuid.uuid4(),
"document_id": document_id,
"kind": draft.kind,
"code": draft.code,
"severity": draft.severity,
"message": draft.message,
"line_ids": [str(line_id) for line_id in draft.line_ids] or None,
"column_id": draft.column_id,
"column_index": draft.column_index,
"codes": list(draft.codes) or None,
"field": draft.field,
"source": draft.source,
"relation": draft.relation,
"expected": draft.expected,
"actual": draft.actual,
"created_at": now,
}
for draft in drafts
]
)
)
"""Записи `document_issue` документа из черновиков пакетами по `BATCH` (без commit)."""
issues = [
{
"id": uuid.uuid4(),
"document_id": document_id,
"kind": draft.kind,
"code": draft.code,
"severity": draft.severity,
"message": draft.message,
"line_ids": [str(line_id) for line_id in draft.line_ids] or None,
"column_id": draft.column_id,
"column_index": draft.column_index,
"codes": list(draft.codes) or None,
"field": draft.field,
"source": draft.source,
"relation": draft.relation,
"expected": draft.expected,
"actual": draft.actual,
"created_at": now,
}
for draft in drafts
]
for start in range(0, len(issues), BATCH):
await session.execute(insert(DocumentIssue).values(issues[start : start + BATCH]))
async def delete_issues( # noqa: PLR0913 - фильтры записей независимы

View File

@ -527,3 +527,23 @@ async def test_osv_v2_sheet_needs_check_blocks_recognized(seeded: AsyncSession)
osv["status"] = "needs_check"
(document,) = await _map(seeded, entity, [osv])
assert document.status is DocumentStatus.NEEDS_CHECK
async def test_osv_v2_many_sheet_warnings_stored(seeded: AsyncSession) -> None:
"""AURORA-1711: предупреждений листа больше, чем помещается в один INSERT `document_issue` (16 параметров на
запись, лимит asyncpg 32 767 — ≈ 2 047 записей). Так было на «11.xlsx» (сч. 11, 2 128 строк «Кол. доп.»):
документ создаётся, все предупреждения сохранены."""
entity = await _entity(seeded)
osv = _osv_doc()
count = 2131
osv["warnings"] = [
{
"code": "OSV_ROWS",
"severity": "info",
"message": f"строка {row}: метка «Кол. доп.» в колонке «Показатели» не распознана",
}
for row in range(count)
]
(document,) = await _map(seeded, entity, [osv])
assert document.document_type is DocumentType.OSV
assert len(await _issues(seeded, document)) == count