Skip to content

Commit 0008467

Browse files
authored
Added upgraded_from_workspace_id property to migrated tables to indicated the source workspace. (#987)
## Changes Added table parameter `upgraded_from_ws` to migrated tables. The parameters contains the sources workspace id. Resolves #899 ### Functionality - [ ] added relevant user documentation - [ ] added new CLI command - [ ] modified existing command: `databricks labs ucx ...` - [ ] added a new workflow - [ ] modified existing workflow: `...` - [ ] added a new table - [ ] modified existing table: `...` ### Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] manually tested - [x] added unit tests - [x] added integration tests - [x] verified on staging environment (screenshot attached)
1 parent bc863ae commit 0008467

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

src/databricks/labs/ucx/hive_metastore/table_migrate.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def _migrate_external_table(self, src_table: Table, rule: Rule):
7272
table_migrate_sql = src_table.sql_migrate_external(target_table_key)
7373
logger.debug(f"Migrating external table {src_table.key} to using SQL query: {table_migrate_sql}")
7474
self._backend.execute(table_migrate_sql)
75+
self._backend.execute(src_table.sql_alter_from(rule.as_uc_table_key, self._ws.get_workspace_id()))
7576
return True
7677

7778
def _migrate_dbfs_root_table(self, src_table: Table, rule: Rule):
@@ -80,7 +81,7 @@ def _migrate_dbfs_root_table(self, src_table: Table, rule: Rule):
8081
logger.debug(f"Migrating managed table {src_table.key} to using SQL query: {table_migrate_sql}")
8182
self._backend.execute(table_migrate_sql)
8283
self._backend.execute(src_table.sql_alter_to(rule.as_uc_table_key))
83-
self._backend.execute(src_table.sql_alter_from(rule.as_uc_table_key))
84+
self._backend.execute(src_table.sql_alter_from(rule.as_uc_table_key, self._ws.get_workspace_id()))
8485
return True
8586

8687
def _migrate_view(self, src_table: Table, rule: Rule):
@@ -89,7 +90,7 @@ def _migrate_view(self, src_table: Table, rule: Rule):
8990
logger.debug(f"Migrating view {src_table.key} to using SQL query: {table_migrate_sql}")
9091
self._backend.execute(table_migrate_sql)
9192
self._backend.execute(src_table.sql_alter_to(rule.as_uc_table_key))
92-
self._backend.execute(src_table.sql_alter_from(rule.as_uc_table_key))
93+
self._backend.execute(src_table.sql_alter_from(rule.as_uc_table_key, self._ws.get_workspace_id()))
9394
return True
9495

9596
def _init_seen_tables(self):

src/databricks/labs/ucx/hive_metastore/tables.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class Table:
5454
"dbfs:/databricks-datasets",
5555
]
5656

57+
UPGRADED_FROM_WS_PARAM: typing.ClassVar[str] = "upgraded_from_workspace_id"
58+
5759
@property
5860
def is_delta(self) -> bool:
5961
if self.table_format is None:
@@ -71,8 +73,12 @@ def kind(self) -> str:
7173
def sql_alter_to(self, target_table_key):
7274
return f"ALTER {self.kind} {self.key} SET TBLPROPERTIES ('upgraded_to' = '{target_table_key}');"
7375

74-
def sql_alter_from(self, target_table_key):
75-
return f"ALTER {self.kind} {target_table_key} SET TBLPROPERTIES ('upgraded_from' = '{self.key}');"
76+
def sql_alter_from(self, target_table_key, ws_id):
77+
return (
78+
f"ALTER {self.kind} {target_table_key} SET TBLPROPERTIES "
79+
f"('upgraded_from' = '{self.key}'"
80+
f" , '{self.UPGRADED_FROM_WS_PARAM}' = '{ws_id}');"
81+
)
7682

7783
def sql_unset_upgraded_to(self):
7884
return f"ALTER {self.kind} {self.key} UNSET TBLPROPERTIES IF EXISTS('upgraded_to');"

tests/integration/hive_metastore/test_migrate.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from databricks.labs.ucx.hive_metastore.mapping import Rule
99
from databricks.labs.ucx.hive_metastore.table_migrate import TablesMigrate
10+
from databricks.labs.ucx.hive_metastore.tables import Table
1011

1112
from ..conftest import StaticTableMapping, StaticTablesCrawler
1213

@@ -46,6 +47,7 @@ def test_migrate_managed_tables(ws, sql_backend, inventory_schema, make_catalog,
4647

4748
target_table_properties = ws.tables.get(f"{dst_schema.full_name}.{src_managed_table.name}").properties
4849
assert target_table_properties["upgraded_from"] == src_managed_table.full_name
50+
assert target_table_properties[Table.UPGRADED_FROM_WS_PARAM] == str(ws.get_workspace_id())
4951

5052

5153
@retried(on=[NotFound], timeout=timedelta(minutes=5))
@@ -136,6 +138,9 @@ def test_migrate_external_table(ws, sql_backend, inventory_schema, make_catalog,
136138

137139
target_tables = list(sql_backend.fetch(f"SHOW TABLES IN {dst_schema.full_name}"))
138140
assert len(target_tables) == 1
141+
target_table_properties = ws.tables.get(f"{dst_schema.full_name}.{src_external_table.name}").properties
142+
assert target_table_properties["upgraded_from"] == src_external_table.full_name
143+
assert target_table_properties[Table.UPGRADED_FROM_WS_PARAM] == str(ws.get_workspace_id())
139144

140145

141146
@retried(on=[NotFound], timeout=timedelta(minutes=5))

tests/unit/hive_metastore/test_table_migrate.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def test_migrate_dbfs_root_tables_should_produce_proper_queries():
2424
rows = {}
2525
backend = MockBackend(fails_on_first=errors, rows=rows)
2626
table_crawler = TablesCrawler(backend, "inventory_database")
27-
client = MagicMock()
27+
client = create_autospec(WorkspaceClient)
28+
client.get_workspace_id.return_value = "12345"
2829
table_mapping = create_autospec(TableMapping)
2930
table_mapping.get_tables_to_migrate.return_value = [
3031
TableToMigrate(
@@ -54,8 +55,8 @@ def test_migrate_dbfs_root_tables_should_produce_proper_queries():
5455
"SET TBLPROPERTIES ('upgraded_to' = 'ucx_default.db1_dst.managed_dbfs');"
5556
) in list(backend.queries)
5657
assert (
57-
"ALTER TABLE ucx_default.db1_dst.managed_dbfs "
58-
"SET TBLPROPERTIES ('upgraded_from' = 'hive_metastore.db1_src.managed_dbfs');"
58+
f"ALTER TABLE ucx_default.db1_dst.managed_dbfs "
59+
f"SET TBLPROPERTIES ('upgraded_from' = 'hive_metastore.db1_src.managed_dbfs' , '{Table.UPGRADED_FROM_WS_PARAM}' = '12345');"
5960
) in list(backend.queries)
6061
assert "SYNC TABLE ucx_default.db1_dst.managed_other FROM hive_metastore.db1_src.managed_other;" in list(
6162
backend.queries
@@ -86,7 +87,8 @@ def test_migrate_external_tables_should_produce_proper_queries():
8687
rows = {}
8788
backend = MockBackend(fails_on_first=errors, rows=rows)
8889
table_crawler = TablesCrawler(backend, "inventory_database")
89-
client = MagicMock()
90+
client = create_autospec(WorkspaceClient)
91+
client.get_workspace_id.return_value = "12345"
9092
table_mapping = create_autospec(TableMapping)
9193
table_mapping.get_tables_to_migrate.return_value = [
9294
TableToMigrate(
@@ -98,7 +100,11 @@ def test_migrate_external_tables_should_produce_proper_queries():
98100
table_migrate.migrate_tables()
99101

100102
assert (list(backend.queries)) == [
101-
"SYNC TABLE ucx_default.db1_dst.external_dst FROM hive_metastore.db1_src.external_src;"
103+
"SYNC TABLE ucx_default.db1_dst.external_dst FROM hive_metastore.db1_src.external_src;",
104+
(
105+
f"ALTER TABLE ucx_default.db1_dst.external_dst "
106+
f"SET TBLPROPERTIES ('upgraded_from' = 'hive_metastore.db1_src.external_src' , '{Table.UPGRADED_FROM_WS_PARAM}' = '12345');"
107+
),
102108
]
103109

104110

@@ -159,7 +165,8 @@ def test_migrate_view_should_produce_proper_queries():
159165
rows = {}
160166
backend = MockBackend(fails_on_first=errors, rows=rows)
161167
table_crawler = TablesCrawler(backend, "inventory_database")
162-
client = MagicMock()
168+
client = create_autospec(WorkspaceClient)
169+
client.get_workspace_id.return_value = "12345"
163170
table_mapping = create_autospec(TableMapping)
164171
table_mapping.get_tables_to_migrate.return_value = [
165172
TableToMigrate(
@@ -176,8 +183,8 @@ def test_migrate_view_should_produce_proper_queries():
176183
"SET TBLPROPERTIES ('upgraded_to' = 'ucx_default.db1_dst.view_dst');"
177184
) in list(backend.queries)
178185
assert (
179-
"ALTER VIEW ucx_default.db1_dst.view_dst "
180-
"SET TBLPROPERTIES ('upgraded_from' = 'hive_metastore.db1_src.view_src');"
186+
f"ALTER VIEW ucx_default.db1_dst.view_dst "
187+
f"SET TBLPROPERTIES ('upgraded_from' = 'hive_metastore.db1_src.view_src' , '{Table.UPGRADED_FROM_WS_PARAM}' = '12345');"
181188
) in list(backend.queries)
182189

183190

0 commit comments

Comments
 (0)