Coverage for src/commands/schema.py: 35%
26 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-05 22:56 -0700
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-05 22:56 -0700
1"""
2Command for showing details of a specific Unity Catalog schema.
3"""
5from typing import Optional, Any
6from src.clients.databricks import DatabricksAPIClient
7from src.commands.base import CommandResult
8from src.catalogs import get_schema as get_schema_details
9from src.config import get_active_catalog
10from src.command_registry import CommandDefinition
11import logging
14def handle_command(
15 client: Optional[DatabricksAPIClient], **kwargs: Any
16) -> CommandResult:
17 """
18 Get details of a specific schema from Unity Catalog.
20 Args:
21 client: DatabricksAPIClient instance for API calls
22 **kwargs: Command parameters
23 - name: Name of the schema to get details for
24 - catalog_name: Name of the catalog containing the schema (optional, uses active catalog if not provided)
26 Returns:
27 CommandResult with schema details if successful
28 """
29 if not client:
30 return CommandResult(
31 False,
32 message="No Databricks client available. Please set up your workspace first.",
33 )
35 # Extract parameters
36 schema_name = kwargs.get("name")
37 catalog_name = kwargs.get("catalog_name")
39 # If catalog_name not provided, try to use active catalog
40 if not catalog_name:
41 catalog_name = get_active_catalog()
42 if not catalog_name:
43 return CommandResult(
44 False,
45 message="No catalog specified and no active catalog selected. Please provide a catalog_name or select a catalog first.",
46 )
48 try:
49 # Construct full name for schema lookup
50 full_name = f"{catalog_name}.{schema_name}"
52 # Get schema details
53 schema = get_schema_details(client, full_name)
55 if not schema:
56 return CommandResult(
57 False,
58 message=f"Schema '{schema_name}' not found in catalog '{catalog_name}'.",
59 )
61 return CommandResult(
62 True, data=schema, message=f"Schema details for '{full_name}'."
63 )
64 except Exception as e:
65 logging.error(f"Error getting schema details: {str(e)}")
66 return CommandResult(
67 False, message=f"Failed to get schema details: {str(e)}", error=e
68 )
71DEFINITION = CommandDefinition(
72 name="schema",
73 description="Show details of a specific Unity Catalog schema. Used to retrieve schema information such as columns, types, and metadata.",
74 handler=handle_command,
75 parameters={
76 "name": {
77 "type": "string",
78 "description": "Name of the schema to get details for.",
79 },
80 "catalog_name": {
81 "type": "string",
82 "description": "Name of the catalog containing the schema (uses active catalog if not provided).",
83 },
84 },
85 required_params=["name"],
86 tui_aliases=["/schema", "/schema-details"],
87 needs_api_client=True,
88 visible_to_user=True,
89 visible_to_agent=True,
90 agent_display="full", # Show full schema details to agents
91 usage_hint="Usage: /schema --name <schema_name> [--catalog_name <catalog_name>]",
92)