Skip to content

Commit 5e60692

Browse files
committed
Merge branch 'm-kovalsky/warehouse-restore-points'
2 parents 705aa99 + 781dd11 commit 5e60692

File tree

4 files changed

+574
-1
lines changed

4 files changed

+574
-1
lines changed

src/sempy_labs/_mirrored_databases.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ def update_mirrored_database_definition(
384384
item_id = resolve_item_id(
385385
item=mirrored_database, type="MirroredDatabase", workspace=workspace
386386
)
387-
payload = base64.b64encode(mirrored_database_content)
387+
payload = (
388+
base64.b64encode(mirrored_database_content).encode("utf-8").decode("utf-8")
389+
)
388390

389391
request_body = {
390392
"displayName": mirrored_database,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from ._restore_points import (
2+
create_restore_point,
3+
delete_restore_point,
4+
list_restore_points,
5+
update_restore_point,
6+
restore_to_restore_point,
7+
)
8+
from ._items import (
9+
create_warehouse,
10+
delete_warehouse,
11+
get_warehouse_tables,
12+
get_warehouse_columns,
13+
list_warehouses,
14+
)
15+
16+
17+
__all__ = [
18+
"create_restore_point",
19+
"delete_restore_point",
20+
"list_restore_points",
21+
"update_restore_point",
22+
"restore_to_restore_point",
23+
"create_warehouse",
24+
"delete_warehouse",
25+
"get_warehouse_tables",
26+
"get_warehouse_columns",
27+
"list_warehouses",
28+
]

src/sempy_labs/warehouse/_items.py

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
from sempy_labs._helper_functions import (
2+
resolve_workspace_name_and_id,
3+
_base_api,
4+
_create_dataframe,
5+
_update_dataframe_datatypes,
6+
delete_item,
7+
resolve_workspace_id,
8+
)
9+
import pandas as pd
10+
from typing import Optional
11+
import sempy_labs._icons as icons
12+
from uuid import UUID
13+
from sempy._utils._log import log
14+
15+
16+
@log
17+
def create_warehouse(
18+
warehouse: str,
19+
description: Optional[str] = None,
20+
case_insensitive_collation: bool = False,
21+
workspace: Optional[str | UUID] = None,
22+
) -> UUID:
23+
"""
24+
Creates a Fabric warehouse.
25+
26+
This is a wrapper function for the following API: `Items - Create Warehouse <https://learn.microsoft.com/rest/api/fabric/warehouse/items/create-warehouse>`_.
27+
28+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
29+
30+
Parameters
31+
----------
32+
warehouse: str
33+
Name of the warehouse.
34+
description : str, default=None
35+
A description of the warehouse.
36+
case_insensitive_collation: bool, default=False
37+
If True, creates the warehouse with case-insensitive collation.
38+
workspace : str | uuid.UUID, default=None
39+
The Fabric workspace name or ID.
40+
Defaults to None which resolves to the workspace of the attached lakehouse
41+
or if no lakehouse attached, resolves to the workspace of the notebook.
42+
43+
Returns
44+
-------
45+
uuid.UUID
46+
The ID of the created warehouse.
47+
"""
48+
49+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
50+
51+
payload = {"displayName": warehouse}
52+
53+
if description:
54+
payload["description"] = description
55+
if case_insensitive_collation:
56+
payload.setdefault("creationPayload", {})
57+
payload["creationPayload"][
58+
"defaultCollation"
59+
] = "Latin1_General_100_CI_AS_KS_WS_SC_UTF8"
60+
61+
result = _base_api(
62+
request=f"/v1/workspaces/{workspace_id}/warehouses",
63+
payload=payload,
64+
method="post",
65+
lro_return_json=True,
66+
status_codes=[201, 202],
67+
client="fabric_sp",
68+
)
69+
70+
print(
71+
f"{icons.green_dot} The '{warehouse}' warehouse has been created within the '{workspace_name}' workspace."
72+
)
73+
74+
return result.get("id")
75+
76+
77+
@log
78+
def list_warehouses(workspace: Optional[str | UUID] = None) -> pd.DataFrame:
79+
"""
80+
Shows the warehouses within a workspace.
81+
82+
This is a wrapper function for the following API: `Items - List Warehouses <https://learn.microsoft.com/rest/api/fabric/warehouse/items/list-warehouses>`_.
83+
84+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
85+
86+
Parameters
87+
----------
88+
workspace : str | uuid.UUID, default=None
89+
The Fabric workspace name or ID.
90+
Defaults to None which resolves to the workspace of the attached lakehouse
91+
or if no lakehouse attached, resolves to the workspace of the notebook.
92+
93+
Returns
94+
-------
95+
pandas.DataFrame
96+
A pandas dataframe showing the warehouses within a workspace.
97+
"""
98+
99+
columns = {
100+
"Warehouse Name": "string",
101+
"Warehouse Id": "string",
102+
"Description": "string",
103+
"Connection Info": "string",
104+
"Created Date": "datetime",
105+
"Last Updated Time": "datetime",
106+
}
107+
df = _create_dataframe(columns=columns)
108+
109+
workspace_id = resolve_workspace_id(workspace)
110+
111+
responses = _base_api(
112+
request=f"/v1/workspaces/{workspace_id}/warehouses",
113+
uses_pagination=True,
114+
client="fabric_sp",
115+
)
116+
117+
rows = []
118+
for r in responses:
119+
for v in r.get("value", []):
120+
prop = v.get("properties", {})
121+
122+
rows.append(
123+
{
124+
"Warehouse Name": v.get("displayName"),
125+
"Warehouse Id": v.get("id"),
126+
"Description": v.get("description"),
127+
"Connection Info": prop.get("connectionInfo"),
128+
"Created Date": prop.get("createdDate"),
129+
"Last Updated Time": prop.get("lastUpdatedTime"),
130+
}
131+
)
132+
133+
if rows:
134+
df = pd.DataFrame(rows, columns=list(columns.keys()))
135+
_update_dataframe_datatypes(dataframe=df, column_map=columns)
136+
137+
return df
138+
139+
140+
@log
141+
def delete_warehouse(name: str | UUID, workspace: Optional[str | UUID] = None):
142+
"""
143+
Deletes a Fabric warehouse.
144+
145+
This is a wrapper function for the following API: `Items - Delete Warehouse <https://learn.microsoft.com/rest/api/fabric/warehouse/items/delete-warehouse>`_.
146+
147+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
148+
149+
Parameters
150+
----------
151+
name: str | uuid.UUID
152+
Name or ID of the warehouse.
153+
workspace : str | uuid.UUID, default=None
154+
The Fabric workspace name or ID.
155+
Defaults to None which resolves to the workspace of the attached lakehouse
156+
or if no lakehouse attached, resolves to the workspace of the notebook.
157+
"""
158+
159+
delete_item(item=name, type="Warehouse", workspace=workspace)
160+
161+
162+
@log
163+
def get_warehouse_tables(
164+
warehouse: str | UUID, workspace: Optional[str | UUID] = None
165+
) -> pd.DataFrame:
166+
"""
167+
Shows a list of the tables in the Fabric warehouse. This function is based on INFORMATION_SCHEMA.TABLES.
168+
169+
Parameters
170+
----------
171+
warehouse : str | uuid.UUID
172+
Name or ID of the Fabric warehouse.
173+
workspace : str | uuid.UUID, default=None
174+
The Fabric workspace name or ID.
175+
Defaults to None which resolves to the workspace of the attached lakehouse
176+
or if no lakehouse attached, resolves to the workspace of the notebook.
177+
178+
Returns
179+
-------
180+
pandas.DataFrame
181+
A pandas dataframe showing a list of the tables in the Fabric warehouse.
182+
"""
183+
184+
from sempy_labs._sql import ConnectWarehouse
185+
186+
with ConnectWarehouse(warehouse=warehouse, workspace=workspace) as sql:
187+
df = sql.query(
188+
"""
189+
SELECT TABLE_SCHEMA AS [Schema], TABLE_NAME AS [Table Name], TABLE_TYPE AS [Table Type]
190+
FROM INFORMATION_SCHEMA.TABLES
191+
WHERE TABLE_TYPE = 'BASE TABLE'
192+
"""
193+
)
194+
195+
return df
196+
197+
198+
@log
199+
def get_warehouse_columns(
200+
warehouse: str | UUID, workspace: Optional[str | UUID] = None
201+
) -> pd.DataFrame:
202+
"""
203+
Shows a list of the columns in each table within the Fabric warehouse. This function is based on INFORMATION_SCHEMA.COLUMNS.
204+
205+
Parameters
206+
----------
207+
warehouse : str | uuid.UUID
208+
Name or ID of the Fabric warehouse.
209+
workspace : str | uuid.UUID, default=None
210+
The Fabric workspace name or ID.
211+
Defaults to None which resolves to the workspace of the attached lakehouse
212+
or if no lakehouse attached, resolves to the workspace of the notebook.
213+
214+
Returns
215+
-------
216+
pandas.DataFrame
217+
A pandas dataframe showing a list of the columns in each table within the Fabric warehouse.
218+
"""
219+
220+
from sempy_labs._sql import ConnectWarehouse
221+
222+
with ConnectWarehouse(warehouse=warehouse, workspace=workspace) as sql:
223+
df = sql.query(
224+
"""
225+
SELECT t.TABLE_SCHEMA AS [Schema], t.TABLE_NAME AS [Table Name], c.COLUMN_NAME AS [Column Name], c.DATA_TYPE AS [Data Type], c.IS_NULLABLE AS [Is Nullable], c.CHARACTER_MAXIMUM_LENGTH AS [Character Max Length]
226+
FROM INFORMATION_SCHEMA.TABLES AS t
227+
LEFT JOIN INFORMATION_SCHEMA.COLUMNS AS c
228+
ON t.TABLE_NAME = c.TABLE_NAME
229+
AND t.TABLE_SCHEMA = c.TABLE_SCHEMA
230+
WHERE t.TABLE_TYPE = 'BASE TABLE'
231+
"""
232+
)
233+
234+
return df

0 commit comments

Comments
 (0)