fix(spanner): move list graphs validation to runtime (#2154)

This pull request resolves the issue by changing the validation logic
for the spanner-list-graphs tool to prevent server crashes on PostgreSQL
dialects.
The spanner-list-graphs tool currently supports only the GoogleSQL
dialect. Previously, this check was enforced during initialization,
causing the entire server to crash on startup when connected to a
PostgreSQL-dialect database.

**Changes Implemented**

- The modification is in
internal/tools/spanner/spannerlistgraphs/spannerlistgraphs.go.
- Removed the dialect validation from the Initialize method (startup).
- Added the dialect validation to the Invoke method (runtime).

This change ensures the tool initializes successfully regardless of the
dialect, allowing the server to start. It now returns a graceful error
message only if a user explicitly attempts to execute the tool on an
unsupported dialect.

**Validation Process**
Validated changes by running the toolbox locally against a Spanner
instance using the PostgreSQL dialect.
CLI Configuration: Ran the server using the standard Spanner prebuilt
set: go run . --prebuilt spanner --ui
Testing: Confirmed the logic by testing two scenarios

1. PostgreSQL Dialect (Graceful Failure)
    Set SPANNER_DIALECT="postgresql".
Result: The server started successfully without crashing (fixing the
bug). When running the spanner-list-graphs tool in the UI, it returned a
clear error message: "operation not supported: The 'spanner-list-graphs'
tool is only available for GoogleSQL dialect databases."
    
<img width="2171" height="462" alt="Screenshot 2025-12-10 11 24 53 PM"
src="https://github.com/user-attachments/assets/c57e64e4-ddce-42a2-998d-b291d77b7d1d"
/>
<img width="2233" height="971" alt="Screenshot 2025-12-10 11 22 53 PM"
src="https://github.com/user-attachments/assets/8e510a72-4598-4d55-a175-bf9e2f85489d"
/>
<img width="2233" height="971" alt="Screenshot 2025-12-10 11 23 26 PM"
src="https://github.com/user-attachments/assets/a904a45f-25be-42ef-9d5a-a7975cc03a44"
/>

2. GoogleSQL Dialect (Success)
    Set SPANNER_DIALECT="googlesql".
Result: The tool executed successfully and returned the graph schema (or
empty results), confirming that normal functionality is preserved.
<img width="2171" height="572" alt="Screenshot 2025-12-10 11 26 59 PM"
src="https://github.com/user-attachments/assets/d9c6c677-cb38-4343-be39-d542439685c4"
/>
<img width="2250" height="938" alt="Screenshot 2025-12-10 11 27 32 PM"
src="https://github.com/user-attachments/assets/6e8f3628-8079-4c99-993a-7ada02a124b0"
/>
<img width="2250" height="938" alt="Screenshot 2025-12-10 11 27 45 PM"
src="https://github.com/user-attachments/assets/a3091228-d73a-44a0-acd2-e7fb463de4e2"
/>



🛠️ Fixes #2136

Co-authored-by: Averi Kitsch <akitsch@google.com>
This commit is contained in:
ishatilwani1301
2025-12-12 13:48:07 +00:00
committed by GitHub
parent 776a5ca438
commit 914b3eefda

View File

@@ -83,11 +83,6 @@ func (cfg Config) Initialize(srcs map[string]sources.Source) (tools.Tool, error)
return nil, fmt.Errorf("invalid source for %q tool: source kind must be one of %q", kind, compatibleSources)
}
// verify the dialect is GoogleSQL
if strings.ToLower(s.DatabaseDialect()) != "googlesql" {
return nil, fmt.Errorf("invalid source dialect for %q tool: source dialect must be GoogleSQL", kind)
}
// Define parameters for the tool
allParameters := parameters.Parameters{
parameters.NewStringParameterWithDefault(
@@ -166,6 +161,10 @@ func processRows(iter *spanner.RowIterator) ([]any, error) {
}
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
// Check dialect here at RUNTIME instead of startup
if strings.ToLower(t.dialect) != "googlesql" {
return nil, fmt.Errorf("operation not supported: The 'spanner-list-graphs' tool is only available for GoogleSQL dialect databases. Your current database dialect is '%s'", t.dialect)
}
paramsMap := params.AsMap()
graphNames, _ := paramsMap["graph_names"].(string)