-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Changes Add pipeline progress encoder ### Linked issues Resolves #3061 Resolves #3064 ### Functionality - [x] modified existing workflow: `migration-progress` ### Tests - [x] added unit tests
- Loading branch information
1 parent
522f48a
commit 6432a28
Showing
7 changed files
with
152 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from dataclasses import replace | ||
|
||
from databricks.labs.lsql.backends import SqlBackend | ||
|
||
from databricks.labs.ucx.hive_metastore.tables import Table | ||
from databricks.labs.ucx.hive_metastore.table_migration_status import TableMigrationIndex | ||
from databricks.labs.ucx.hive_metastore.ownership import TableOwnership | ||
from databricks.labs.ucx.progress.history import ProgressEncoder | ||
from databricks.labs.ucx.progress.install import Historical | ||
|
||
|
||
class TableProgressEncoder(ProgressEncoder[Table]): | ||
"""Encoder class:Table to class:History. | ||
A progress failure for a table means: | ||
- the table is not migrated yet | ||
- the associated grants have a failure | ||
""" | ||
|
||
def __init__( | ||
self, | ||
sql_backend: SqlBackend, | ||
ownership: TableOwnership, | ||
table_migration_index: TableMigrationIndex, | ||
run_id: int, | ||
workspace_id: int, | ||
catalog: str, | ||
schema: str = "multiworkspace", | ||
table: str = "historical", | ||
) -> None: | ||
super().__init__( | ||
sql_backend, | ||
ownership, | ||
Table, | ||
run_id, | ||
workspace_id, | ||
catalog, | ||
schema, | ||
table, | ||
) | ||
self._table_migration_index = table_migration_index | ||
|
||
def _encode_record_as_historical(self, record: Table) -> Historical: | ||
"""Encode record as historical. | ||
A table failure means that the table is pending migration. Grants are purposefully lef out, because a grant | ||
might not be mappable to UC, like `READ_METADATA`, thus possibly resulting in false "pending migration" failure | ||
for tables that are migrated to UC with their relevant grants also being migrated. | ||
""" | ||
historical = super()._encode_record_as_historical(record) | ||
failures = [] | ||
if not self._table_migration_index.is_migrated(record.database, record.name): | ||
failures.append("Pending migration") | ||
return replace(historical, failures=historical.failures + failures) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from unittest.mock import create_autospec | ||
|
||
import pytest | ||
|
||
from databricks.labs.ucx.framework.owners import Ownership | ||
from databricks.labs.ucx.framework.utils import escape_sql_identifier | ||
from databricks.labs.ucx.hive_metastore.table_migration_status import TableMigrationIndex | ||
from databricks.labs.ucx.hive_metastore.tables import Table | ||
from databricks.labs.ucx.progress.grants import GrantProgressEncoder | ||
from databricks.labs.ucx.progress.tables import TableProgressEncoder | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"table", | ||
[ | ||
Table("hive_metastore", "schema", "table", "MANAGED", "DELTA"), | ||
], | ||
) | ||
def test_table_progress_encoder_no_failures(mock_backend, table: Table) -> None: | ||
ownership = create_autospec(Ownership) | ||
ownership.owner_of.return_value = "user" | ||
table_migration_index = create_autospec(TableMigrationIndex) | ||
table_migration_index.is_migrated.return_value = True | ||
grant_progress_encoder = create_autospec(GrantProgressEncoder) | ||
encoder = TableProgressEncoder( | ||
mock_backend, ownership, table_migration_index, run_id=1, workspace_id=123456789, catalog="test" | ||
) | ||
|
||
encoder.append_inventory_snapshot([table]) | ||
|
||
rows = mock_backend.rows_written_for(escape_sql_identifier(encoder.full_name), "append") | ||
assert len(rows) > 0, f"No rows written for: {encoder.full_name}" | ||
assert len(rows[0].failures) == 0 | ||
ownership.owner_of.assert_called_once() | ||
table_migration_index.is_migrated.assert_called_with(table.database, table.name) | ||
grant_progress_encoder.assert_not_called() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"table", | ||
[ | ||
Table("hive_metastore", "schema", "table", "MANAGED", "DELTA"), | ||
], | ||
) | ||
def test_table_progress_encoder_pending_migration_failure(mock_backend, table: Table) -> None: | ||
ownership = create_autospec(Ownership) | ||
ownership.owner_of.return_value = "user" | ||
table_migration_index = create_autospec(TableMigrationIndex) | ||
table_migration_index.is_migrated.return_value = False | ||
grant_progress_encoder = create_autospec(GrantProgressEncoder) | ||
encoder = TableProgressEncoder( | ||
mock_backend, ownership, table_migration_index, run_id=1, workspace_id=123456789, catalog="test" | ||
) | ||
|
||
encoder.append_inventory_snapshot([table]) | ||
|
||
rows = mock_backend.rows_written_for(escape_sql_identifier(encoder.full_name), "append") | ||
assert len(rows) > 0, f"No rows written for: {encoder.full_name}" | ||
assert rows[0].failures == ["Pending migration"] | ||
ownership.owner_of.assert_called_once() | ||
table_migration_index.is_migrated.assert_called_with(table.database, table.name) | ||
grant_progress_encoder.assert_not_called() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters