Skip to content

Commit ee024cd

Browse files
authored
Let create-catalog-schemas command run as collection (#2653)
## Changes Let `validate-external-locations` command to run as collection ### Linked issues Resolves #2609 ### Functionality - [x] modified existing command: `databricks labs ucx create-catalog-schemas` ### Tests - [x] manually tested - [x] added unit tests - [ ] ~added integration tests~ : Covering after #2507
1 parent 61b6715 commit ee024cd

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

labs.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,12 @@ commands:
212212
description: Run the command for the collection of workspaces with ucx installed. Default is False.
213213

214214
- name: create-catalogs-schemas
215-
description: Create UC external catalogs and schemas based on the destinations created from create_table_mapping command.
216-
This command is supposed to be run before migrating tables to UC.
215+
description: |
216+
Create UC external catalogs and schemas based on the destinations created from `create_table_mapping` command.
217+
This command should be executed before migrating tables to Unity Catalog.
218+
flags:
219+
- name: run-as-collection
220+
description: Run the command for the collection of workspaces with ucx installed. Default is False.
217221

218222
- name: cluster-remap
219223
description: Re-mapping the cluster to UC

src/databricks/labs/ucx/cli.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,20 @@ def migrate_locations(
452452

453453

454454
@ucx.command
455-
def create_catalogs_schemas(w: WorkspaceClient, prompts: Prompts):
455+
def create_catalogs_schemas(
456+
w: WorkspaceClient,
457+
prompts: Prompts,
458+
ctx: WorkspaceContext | None = None,
459+
run_as_collection: bool = False,
460+
a: AccountClient | None = None,
461+
) -> None:
456462
"""Create UC catalogs and schemas based on the destinations created from create_table_mapping command."""
457-
ctx = WorkspaceContext(w)
458-
ctx.catalog_schema.create_all_catalogs_schemas(prompts)
463+
if ctx:
464+
workspace_contexts = [ctx]
465+
else:
466+
workspace_contexts = _get_workspace_contexts(w, a, run_as_collection)
467+
for workspace_context in workspace_contexts:
468+
workspace_context.catalog_schema.create_all_catalogs_schemas(prompts)
459469

460470

461471
@ucx.command

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(
2727
self._principal_grants = principal_grants
2828
self._backend = sql_backend
2929

30-
def create_all_catalogs_schemas(self, prompts: Prompts):
30+
def create_all_catalogs_schemas(self, prompts: Prompts) -> None:
3131
candidate_catalogs, candidate_schemas = self._get_missing_catalogs_schemas()
3232
for candidate_catalog in candidate_catalogs:
3333
try:

tests/unit/test_cli.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -713,19 +713,26 @@ def test_migrate_locations_gcp(ws):
713713
migrate_locations(ws, ctx=ctx)
714714

715715

716-
def test_create_catalogs_schemas(ws):
716+
@pytest.mark.parametrize("run_as_collection", [False, True])
717+
def test_create_catalogs_schemas_lists_catalogs(run_as_collection, workspace_clients, acc_client) -> None:
718+
if not run_as_collection:
719+
workspace_clients = [workspace_clients[0]]
720+
for workspace_client in workspace_clients:
721+
workspace_client.external_locations.list.return_value = [ExternalLocationInfo(url="s3://test")]
717722
prompts = MockPrompts({'.*': 's3://test'})
718-
ws.external_locations.list.return_value = [ExternalLocationInfo(url="s3://test")]
719-
create_catalogs_schemas(ws, prompts)
720-
ws.catalogs.list.assert_called_once()
723+
724+
create_catalogs_schemas(workspace_clients[0], prompts, run_as_collection=run_as_collection, a=acc_client)
725+
726+
for workspace_client in workspace_clients:
727+
workspace_client.catalogs.list.assert_called_once()
721728

722729

723-
def test_create_catalogs_schemas_handles_existing(ws, caplog):
730+
def test_create_catalogs_schemas_handles_existing(ws, caplog) -> None:
724731
prompts = MockPrompts({'.*': 's3://test'})
725732
ws.external_locations.list.return_value = [ExternalLocationInfo(url="s3://test")]
726733
ws.catalogs.create.side_effect = [BadRequest("Catalog 'test' already exists")]
727734
ws.schemas.create.side_effect = [BadRequest("Schema 'test' already exists")]
728-
create_catalogs_schemas(ws, prompts)
735+
create_catalogs_schemas(ws, prompts, ctx=WorkspaceContext(ws))
729736
ws.catalogs.list.assert_called_once()
730737

731738
assert "Catalog test already exists. Skipping." in caplog.messages

0 commit comments

Comments
 (0)