preliminary release
This commit is contained in:
@@ -13,6 +13,7 @@ from typing import Any, Iterator
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from persistance.repository import ConflictPolicy
|
||||
from webapi.imports.audit import ImportAudit
|
||||
from webapi.imports.connectors import Connector, ConnectorError, build_connector
|
||||
from webapi.imports.schemas.registry import ENTITY_REGISTRY, EntitySpec
|
||||
from webapi.imports.schemas.service import BatchValidationResult, validate_rows
|
||||
@@ -130,8 +131,16 @@ def _jsonable(value: Any) -> Any:
|
||||
return str(value)
|
||||
|
||||
|
||||
def validate_source(raw_source: dict[str, Any]) -> BatchValidationResult:
|
||||
return ImportSource.parse(raw_source).validate()
|
||||
def validate_source(raw_source: dict[str, Any], audit: ImportAudit | None = None) -> BatchValidationResult:
|
||||
source = ImportSource.parse(raw_source)
|
||||
if audit is None:
|
||||
return source.validate()
|
||||
tracked = audit.source(source)
|
||||
rows = list(source.rows())
|
||||
tracked.observe_rows(rows)
|
||||
validation = validate_rows(source.spec.schema, rows)
|
||||
tracked.observe_validation(validation)
|
||||
return validation
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -215,7 +224,9 @@ def _key_part(value: Any) -> Any:
|
||||
return str(value).strip().upper()
|
||||
|
||||
|
||||
def commit_sources(raw_sources: Any, *, on_conflict: str, merge: bool = False) -> list[SourceCommitResult]:
|
||||
def commit_sources(
|
||||
raw_sources: Any, *, on_conflict: str, merge: bool = False, audit: ImportAudit | None = None
|
||||
) -> list[SourceCommitResult]:
|
||||
"""Writes a batch in one transaction. With `merge`, rows of different
|
||||
sources sharing a natural key are combined first (see `merge_duplicates`)."""
|
||||
try:
|
||||
@@ -224,6 +235,7 @@ def commit_sources(raw_sources: Any, *, on_conflict: str, merge: bool = False) -
|
||||
raise ImportRequestError(f"invalid on_conflict: {on_conflict!r}") from exc
|
||||
|
||||
sources = _parse_batch(raw_sources)
|
||||
tracked = [audit.source(source) for source in sources] if audit else []
|
||||
if policy is ConflictPolicy.UPSERT:
|
||||
keyless = sorted({s.target for s in sources if not s.spec.conflict_fields})
|
||||
if keyless:
|
||||
@@ -234,6 +246,10 @@ def commit_sources(raw_sources: Any, *, on_conflict: str, merge: bool = False) -
|
||||
rows: list[list[dict[str, Any] | None]] = [list(source.rows()) for source in sources]
|
||||
merged, merge_warnings = merge_duplicates(sources, rows) if merge else ({}, {})
|
||||
validated = [(source, validate_rows(source.spec.schema, source_rows)) for source, source_rows in zip(sources, rows)]
|
||||
for position, source_audit in enumerate(tracked):
|
||||
source_audit.observe_rows(rows[position])
|
||||
source_audit.observe_validation(validated[position][1])
|
||||
source_audit.observe_warnings(merge_warnings.get(position, []))
|
||||
|
||||
session = sources[0].spec.repository.open_session()
|
||||
links = LinkResolver(session)
|
||||
@@ -279,6 +295,11 @@ def commit_sources(raw_sources: Any, *, on_conflict: str, merge: bool = False) -
|
||||
merged=merged.get(position, 0),
|
||||
warnings=merge_warnings.get(position, []),
|
||||
)
|
||||
if tracked:
|
||||
tracked[position].observe_write(
|
||||
created=write.created_count, updated=write.updated_count,
|
||||
conflicts=write.rejected_count, linked=linked,
|
||||
)
|
||||
session.commit()
|
||||
except SQLAlchemyError as exc:
|
||||
session.rollback()
|
||||
|
||||
Reference in New Issue
Block a user