Skip to content

Commit bd8c412

Browse files
authored
Changed revert report to report on all "what" types rather than managed/external tables (#924)
1 parent 784ba01 commit bd8c412

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,12 @@ def _get_revert_count(self, schema: str | None = None, table: str | None = None)
153153

154154
migration_list = []
155155
for cur_database, tables in table_by_database.items():
156-
external_tables = 0
157-
managed_tables = 0
158-
views = 0
156+
what_count: dict[What, int] = {}
159157
for current_table in tables:
160158
if current_table.upgraded_to is not None:
161-
if current_table.kind == "VIEW":
162-
views += 1
163-
continue
164-
if current_table.object_type == "EXTERNAL":
165-
external_tables += 1
166-
continue
167-
if current_table.object_type == "MANAGED":
168-
managed_tables += 1
169-
continue
170-
migration_list.append(
171-
MigrationCount(
172-
database=cur_database, managed_tables=managed_tables, external_tables=external_tables, views=views
173-
)
174-
)
159+
count = what_count.get(current_table.what, 0)
160+
what_count[current_table.what] = count + 1
161+
migration_list.append(MigrationCount(database=cur_database, what_count=what_count))
175162
return migration_list
176163

177164
def is_upgraded(self, schema: str, table: str) -> bool:
@@ -189,17 +176,36 @@ def print_revert_report(self, *, delete_managed: bool) -> bool | None:
189176
logger.info("No migrated tables were found.")
190177
return False
191178
print("The following is the count of migrated tables and views found in scope:")
192-
print("Database | External Tables | Managed Table | Views |")
193-
print("=" * 88)
179+
table_header = "Database |"
180+
headers = 1
181+
for what in list(What):
182+
headers = max(headers, len(what.name.split("_")))
183+
table_header += f" {what.name.split('_')[0]:<10} |"
184+
print(table_header)
185+
# Split the header so _ separated what names are splitted into multiple lines
186+
for header in range(1, headers):
187+
table_sub_header = " |"
188+
for what in list(What):
189+
if len(what.name.split("_")) - 1 < header:
190+
table_sub_header += f"{' '*12}|"
191+
continue
192+
table_sub_header += f" {what.name.split('_')[header]:<10} |"
193+
print(table_sub_header)
194+
separator = "=" * (22 + 13 * len(What))
195+
print(separator)
194196
for count in migrated_count:
195-
print(f"{count.database:<30}| {count.external_tables:16} | {count.managed_tables:16} | {count.views:16} |")
196-
print("=" * 88)
197-
print("Migrated External Tables and Views (targets) will be deleted")
197+
table_row = f"{count.database:<20}|"
198+
for what in list(What):
199+
table_row += f" {count.what_count.get(what, 0):10} |"
200+
print(table_row)
201+
print(separator)
202+
print("The following actions will be performed")
203+
print("- Migrated External Tables and Views (targets) will be deleted")
198204
if delete_managed:
199-
print("Migrated Manged Tables (targets) will be deleted")
205+
print("- Migrated DBFS Root Tables will be deleted")
200206
else:
201-
print("Migrated Manged Tables (targets) will be left intact.")
202-
print("To revert and delete Migrated Tables, add --delete_managed true flag to the command.")
207+
print("- Migrated DBFS Root Tables will be left intact")
208+
print("To revert and delete Migrated Tables, add --delete_managed true flag to the command")
203209
return True
204210

205211

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ class TableError:
147147
@dataclass
148148
class MigrationCount:
149149
database: str
150-
managed_tables: int = 0
151-
external_tables: int = 0
152-
views: int = 0
150+
what_count: dict[What, int]
153151

154152

155153
class TablesCrawler(CrawlerBase):

tests/unit/hive_metastore/test_table_migrate.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def get_table_migrate(backend: SqlBackend) -> TablesMigrate:
238238
catalog="hive_metastore",
239239
database="test_schema1",
240240
name="test_table1",
241+
location="s3://some_location/table",
241242
upgraded_to="cat1.schema1.dest1",
242243
),
243244
Table(
@@ -255,6 +256,7 @@ def get_table_migrate(backend: SqlBackend) -> TablesMigrate:
255256
catalog="hive_metastore",
256257
database="test_schema1",
257258
name="test_table2",
259+
location="dbfs:/dbfs_location/table",
258260
upgraded_to="cat1.schema1.dest2",
259261
),
260262
Table(
@@ -263,6 +265,7 @@ def get_table_migrate(backend: SqlBackend) -> TablesMigrate:
263265
catalog="hive_metastore",
264266
database="test_schema2",
265267
name="test_table3",
268+
location="s3://some_location/table",
266269
upgraded_to="cat1.schema2.dest3",
267270
),
268271
]
@@ -352,8 +355,13 @@ def test_get_migrated_count():
352355
backend = MockBackend(fails_on_first=errors, rows=rows)
353356
table_migrate = get_table_migrate(backend)
354357
migrated_count = table_migrate._get_revert_count()
355-
assert MigrationCount("test_schema1", 1, 1, 1) in migrated_count
356-
assert MigrationCount("test_schema2", 0, 1, 0) in migrated_count
358+
assert (
359+
MigrationCount(
360+
database="test_schema1", what_count={What.VIEW: 1, What.DBFS_ROOT_DELTA: 1, What.EXTERNAL_SYNC: 1}
361+
)
362+
in migrated_count
363+
)
364+
assert MigrationCount("test_schema2", {What.EXTERNAL_SYNC: 1}) in migrated_count
357365

358366

359367
def test_revert_report(capsys):
@@ -363,13 +371,13 @@ def test_revert_report(capsys):
363371
table_migrate = get_table_migrate(backend)
364372
table_migrate.print_revert_report(delete_managed=True)
365373
captured = capsys.readouterr()
366-
assert "test_schema1|1|1|1" in captured.out.replace(" ", "")
367-
assert "test_schema2|1|0|0" in captured.out.replace(" ", "")
368-
assert "Migrated Manged Tables (targets) will be deleted" in captured.out
374+
assert "test_schema1|1|0|1|0|1|0|0|" in captured.out.replace(" ", "")
375+
assert "test_schema2|1|0|0|0|0|0|0|" in captured.out.replace(" ", "")
376+
assert "- Migrated DBFS Root Tables will be deleted" in captured.out
369377

370378
table_migrate.print_revert_report(delete_managed=False)
371379
captured = capsys.readouterr()
372-
assert "Migrated Manged Tables (targets) will be left intact" in captured.out
380+
assert "- Migrated DBFS Root Tables will be left intact" in captured.out
373381

374382

375383
def test_empty_revert_report(capsys):

0 commit comments

Comments
 (0)