Skip to content

Commit cb6708f

Browse files
committed
Fix(fabric): Fix failing janitor test
1 parent 4fc3ba6 commit cb6708f

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

sqlmesh/core/engine_adapter/fabric.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def __init__(self, tenant_id: str, workspace_id: str, client_id: str, client_sec
172172
self.client_secret = client_secret
173173
self.workspace_id = workspace_id
174174

175-
def create_warehouse(self, warehouse_name: str) -> None:
175+
def create_warehouse(self, warehouse_name: str, if_not_exists: bool = True) -> None:
176176
"""Create a catalog (warehouse) in Microsoft Fabric via REST API."""
177177
logger.info(f"Creating Fabric warehouse: {warehouse_name}")
178178

@@ -182,6 +182,15 @@ def create_warehouse(self, warehouse_name: str) -> None:
182182
}
183183

184184
response = self.session.post(self._endpoint_url("warehouses"), json=request_data)
185+
186+
if (
187+
if_not_exists
188+
and response.status_code == 400
189+
and response.json().get("errorCode", None) == "ItemDisplayNameAlreadyInUse"
190+
):
191+
logger.warning(f"Fabric warehouse {warehouse_name} already exists")
192+
return
193+
185194
response.raise_for_status()
186195

187196
# Handle direct success (201) or async creation (202)
@@ -197,11 +206,12 @@ def create_warehouse(self, warehouse_name: str) -> None:
197206
logger.error(f"Unexpected response from Fabric API: {response}\n{response.text}")
198207
raise SQLMeshError(f"Unable to create warehouse: {response}")
199208

200-
def delete_warehouse(self, warehouse_name: str) -> None:
209+
def delete_warehouse(self, warehouse_name: str, if_exists: bool = True) -> None:
201210
"""Drop a catalog (warehouse) in Microsoft Fabric via REST API."""
202211
logger.info(f"Deleting Fabric warehouse: {warehouse_name}")
203212

204213
# Get the warehouse ID by listing warehouses
214+
# TODO: handle continuationUri for pagination, ref: https://learn.microsoft.com/en-us/rest/api/fabric/warehouse/items/list-warehouses?tabs=HTTP#warehouses
205215
response = self.session.get(self._endpoint_url("warehouses"))
206216
response.raise_for_status()
207217

@@ -213,9 +223,12 @@ def delete_warehouse(self, warehouse_name: str) -> None:
213223
warehouse_id = warehouse_name_to_id.get(warehouse_name, None)
214224

215225
if not warehouse_id:
216-
logger.error(
226+
logger.warning(
217227
f"Fabric warehouse does not exist: {warehouse_name}\n(available warehouses: {', '.join(warehouse_name_to_id)})"
218228
)
229+
if if_exists:
230+
return
231+
219232
raise SQLMeshError(
220233
f"Unable to delete Fabric warehouse {warehouse_name} as it doesnt exist"
221234
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import typing as t
2+
import pytest
3+
from pytest import FixtureRequest
4+
from sqlmesh.core.engine_adapter import FabricEngineAdapter
5+
from tests.core.engine_adapter.integration import TestContext
6+
7+
from tests.core.engine_adapter.integration import (
8+
TestContext,
9+
generate_pytest_params,
10+
ENGINES_BY_NAME,
11+
IntegrationTestEngine,
12+
)
13+
14+
15+
@pytest.fixture(
16+
params=list(generate_pytest_params(ENGINES_BY_NAME["fabric"], show_variant_in_test_id=False))
17+
)
18+
def ctx(
19+
request: FixtureRequest,
20+
create_test_context: t.Callable[[IntegrationTestEngine, str, str], t.Iterable[TestContext]],
21+
) -> t.Iterable[TestContext]:
22+
yield from create_test_context(*request.param)
23+
24+
25+
@pytest.fixture
26+
def engine_adapter(ctx: TestContext) -> FabricEngineAdapter:
27+
assert isinstance(ctx.engine_adapter, FabricEngineAdapter)
28+
return ctx.engine_adapter
29+
30+
31+
def test_create_drop_catalog(ctx: TestContext, engine_adapter: FabricEngineAdapter):
32+
catalog_name = ctx.add_test_suffix("test_catalog")
33+
34+
try:
35+
ctx.create_catalog(catalog_name)
36+
# if already exists, should be no-op, not error
37+
ctx.create_catalog(catalog_name)
38+
ctx.drop_catalog(catalog_name)
39+
finally:
40+
# if doesnt exist, should be no-op, not error
41+
ctx.drop_catalog(catalog_name)

0 commit comments

Comments
 (0)