Skip to content

Commit

Permalink
Add method to HistoryLog for encoding Historical (#3065)
Browse files Browse the repository at this point in the history
## Changes
Add method to `HistoryLog` for encoding `Historical`. Some pre-work for
the remainder of the issues in 3064.

### Linked issue

Progresses #3064
  • Loading branch information
JCZuurmond authored Oct 24, 2024
1 parent 58b1cba commit 8e6c337
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/databricks/labs/ucx/progress/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
import dataclasses
import datetime as dt
import typing
from enum import Enum, EnumMeta
import json
import logging
from enum import Enum, EnumMeta
from collections.abc import Iterable, Sequence
from typing import ClassVar, Protocol, TypeVar, Generic, Any, get_type_hints
from typing import Any, ClassVar, Generic, Protocol, TypeVar, get_type_hints, final

from databricks.labs.lsql.backends import SqlBackend

from databricks.labs.ucx.framework.owners import Ownership
from databricks.labs.ucx.framework.utils import escape_sql_identifier
from databricks.labs.ucx.progress.install import Historical


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -275,8 +276,13 @@ def __init__(
def full_name(self) -> str:
return f"{self._catalog}.{self._schema}.{self._table}"

@final
def append_inventory_snapshot(self, snapshot: Iterable[Record]) -> None:
history_records = [self._encoder.to_historical(record) for record in snapshot]
history_records = [self._encode_record_as_historical(record) for record in snapshot]
logger.debug(f"Appending {len(history_records)} {self._klass} record(s) to history.")
# This is the only writer, and the mode is 'append'. This is documented as conflict-free.
self._sql_backend.save_table(escape_sql_identifier(self.full_name), history_records, Historical, mode="append")

def _encode_record_as_historical(self, record: Record) -> Historical:
"""Encode a snapshot record as a historical log entry."""
return self._encoder.to_historical(record)

0 comments on commit 8e6c337

Please sign in to comment.