Skip to content

Commit 209f160

Browse files
committed
Add IN GROUP clause to USE WORKSPACE
1 parent 66a00b6 commit 209f160

File tree

1 file changed

+57
-12
lines changed

1 file changed

+57
-12
lines changed

singlestoredb/fusion/handlers/workspace.py

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class UseWorkspaceHandler(SQLHandler):
1717
"""
18-
USE WORKSPACE workspace [ with_database ];
18+
USE WORKSPACE workspace [ in_group ] [ with_database ];
1919
2020
# Workspace
2121
workspace = { workspace_id | workspace_name | current_workspace }
@@ -29,6 +29,15 @@ class UseWorkspaceHandler(SQLHandler):
2929
# Current workspace
3030
current_workspace = @@CURRENT
3131
32+
# Workspace group specification
33+
in_group = IN GROUP { group_id | group_name }
34+
35+
# ID of workspace group
36+
group_id = ID '<group-id>'
37+
38+
# Name of workspace group
39+
group_name = '<group-name>'
40+
3241
# Name of database
3342
with_database = WITH DATABASE 'database-name'
3443
@@ -38,13 +47,18 @@ class UseWorkspaceHandler(SQLHandler):
3847
3948
Arguments
4049
---------
41-
* ``<workspace-id>``: The ID of the workspace to delete.
42-
* ``<workspace-name>``: The name of the workspace to delete.
50+
* ``<workspace-id>``: The ID of the workspace to use.
51+
* ``<workspace-name>``: The name of the workspace to use.
52+
* ``<group-id>``: The ID of the workspace group to search in.
53+
* ``<group-name>``: The name of the workspace group to search in.
4354
4455
Remarks
4556
-------
4657
* If you want to specify a database in the current workspace,
4758
the workspace name can be specified as ``@@CURRENT``.
59+
* Use the ``IN GROUP`` clause to specify the ID or name of the workspace
60+
group where the workspace should be found. If not specified, the current
61+
workspace group will be used.
4862
* Specify the ``WITH DATABASE`` clause to select a default
4963
database for the session.
5064
* This command only works in a notebook session in the
@@ -57,23 +71,54 @@ class UseWorkspaceHandler(SQLHandler):
5771
5872
USE WORKSPACE 'examplews' WITH DATABASE 'dbname';
5973
74+
The following command sets the workspace to ``examplews`` from a specific
75+
workspace group::
76+
77+
USE WORKSPACE 'examplews' IN GROUP 'my-workspace-group';
78+
6079
"""
6180
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
6281
from singlestoredb.notebook import portal
82+
83+
# Handle current workspace case
6384
if params['workspace'].get('current_workspace'):
6485
if params.get('with_database'):
6586
portal.default_database = params['with_database']
66-
elif params.get('with_database'):
67-
if params['workspace'].get('workspace_name'):
68-
portal.connection = params['workspace']['workspace_name'], \
69-
params['with_database']
87+
return None
88+
89+
# Get workspace name or ID
90+
workspace_name = params['workspace'].get('workspace_name')
91+
workspace_id = params['workspace'].get('workspace_id')
92+
93+
# If IN GROUP is specified, look up workspace in that group
94+
if params.get('in_group'):
95+
workspace_group = get_workspace_group(params)
96+
97+
if workspace_name:
98+
workspace = workspace_group.workspaces[workspace_name]
99+
elif workspace_id:
100+
# Find workspace by ID in the specified group
101+
workspace = next(
102+
(w for w in workspace_group.workspaces if w.id == workspace_id),
103+
None,
104+
)
105+
if workspace is None:
106+
raise KeyError(f'no workspace found with ID: {workspace_id}')
107+
108+
workspace_id = workspace.id
109+
110+
# Set workspace and database
111+
if params.get('with_database'):
112+
if workspace_name and not params.get('in_group'):
113+
portal.connection = workspace_name, params['with_database']
70114
else:
71-
portal.connection = params['workspace']['workspace_id'], \
72-
params['with_database']
73-
elif params['workspace'].get('workspace_name'):
74-
portal.workspace = params['workspace']['workspace_name']
115+
portal.connection = workspace_id, params['with_database']
75116
else:
76-
portal.workspace = params['workspace']['workspace_id']
117+
if workspace_name and not params.get('in_group'):
118+
portal.workspace = workspace_name
119+
else:
120+
portal.workspace = workspace_id
121+
77122
return None
78123

79124

0 commit comments

Comments
 (0)