Resolve a SQL warehouse to use for DDL. Picking warehouses[0] is fragile — the API returns warehouses in an implementation-defined order, so on a workspace with multiple warehouses we'd silently grab whichever one happens to be first. Instead: - If `name` is provided, look it up by
(workspace: WorkspaceClient, name: str | None = None)
| 3 | |
| 4 | |
| 5 | def get_warehouse_id(workspace: WorkspaceClient, name: str | None = None) -> str: |
| 6 | """Resolve a SQL warehouse to use for DDL. |
| 7 | |
| 8 | Picking warehouses[0] is fragile — the API returns warehouses in an |
| 9 | implementation-defined order, so on a workspace with multiple warehouses |
| 10 | we'd silently grab whichever one happens to be first. Instead: |
| 11 | |
| 12 | - If `name` is provided, look it up by name (fail loudly if missing). |
| 13 | - Otherwise, succeed only if there's exactly one warehouse; fail with |
| 14 | a clear "ambiguous, pass --warehouse-name" error when there are many. |
| 15 | """ |
| 16 | warehouses = list(workspace.warehouses.list()) |
| 17 | if not warehouses: |
| 18 | raise ValueError("No SQL warehouse found. Please create one to run SQL statements.") |
| 19 | |
| 20 | if name: |
| 21 | match = next((w for w in warehouses if w.name == name), None) |
| 22 | if match is None: |
| 23 | raise ValueError(f"SQL warehouse {name!r} not found. Available: {[w.name for w in warehouses]}") |
| 24 | return match.id |
| 25 | |
| 26 | if len(warehouses) > 1: |
| 27 | raise ValueError( |
| 28 | f"Multiple SQL warehouses found ({[w.name for w in warehouses]}); pass --warehouse-name to disambiguate." |
| 29 | ) |
| 30 | return warehouses[0].id |
| 31 | |
| 32 | |
| 33 | def run_sql(workspace: WorkspaceClient, warehouse_id: str, sql: str) -> None: |