Skip to content

Commit

Permalink
feat!: consolidate "x-postgres-generic" tools to "postgres-sql" tool (#…
Browse files Browse the repository at this point in the history
…43)

This PR introduces the following breaking change: The
`alloydb-pg-generic`, `cloud-sql-pg-generic`, and
`postgres-generic-tool` have been replaced by the `postgres-sql` tool,
which works with all 3 Postgres sources.

If you were using of the the previous tools, you will need to update it
as follows:
```diff
example_tool:
-    kind: cloud-sql-pg-generic
+    kind: postgres-sql
     source: my-cloud-sql-pg-instance
     description: some description
        statement: |
            SELECT * FROM SQL_STATEMENT;
        parameters:
        - name: country
          type: string
          description: some description
```

I'm proposing this change for the following reasons:
1. It provides greater flexibility between postgres-compatible sources
-- you can change between "postgres" and "alloydb-postgres" without
issue
2. The name "postgres-sql" is more clear that "postgres-generic" -- it
indicates it's a tool that runs SQL on the source
3. It's easier for us to maintain feature compatibility across a single
"postgres-sql" tool
  • Loading branch information
kurtisvg authored Nov 1, 2024
1 parent 0a0d206 commit f630965
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 354 deletions.
8 changes: 4 additions & 4 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
cloudsqlpgsrc "github.com/googleapis/genai-toolbox/internal/sources/cloudsqlpg"
"github.com/googleapis/genai-toolbox/internal/testutils"
"github.com/googleapis/genai-toolbox/internal/tools"
cloudsqlpgtool "github.com/googleapis/genai-toolbox/internal/tools/cloudsqlpg"
"github.com/googleapis/genai-toolbox/internal/tools/postgressql"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -186,7 +186,7 @@ func TestParseToolFile(t *testing.T) {
database: my_db
tools:
example_tool:
kind: cloud-sql-postgres-generic
kind: postgres-sql
source: my-pg-instance
description: some description
statement: |
Expand All @@ -210,9 +210,9 @@ func TestParseToolFile(t *testing.T) {
},
},
wantTools: server.ToolConfigs{
"example_tool": cloudsqlpgtool.GenericConfig{
"example_tool": postgressql.Config{
Name: "example_tool",
Kind: cloudsqlpgtool.ToolKind,
Kind: postgressql.ToolKind,
Source: "my-pg-instance",
Description: "some description",
Statement: "SELECT * FROM SQL_STATEMENT;\n",
Expand Down
6 changes: 1 addition & 5 deletions docs/tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ tools:
We currently support the following types of kinds of tools:
* [alloydb-postgres](./alloydb-pg.md) - Run a SQL statement against an AlloyDB
for PostgreSQL instance.
* [cloud-sql-postgres](./cloud-sql-pg.md) - Run a SQL statement against a Cloud
SQL for PostgreSQL instance.
* [postgres-generic](./postgres.md) - Run a PostgreSQL statement against a
* [postgres-sql](./sql.md) - Run a PostgreSQL statement against a
PostgreSQL-compatible database.
Expand Down
19 changes: 13 additions & 6 deletions docs/tools/postgres-generic.md → docs/tools/postgres-sql.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
# PostgreSQL Generic Tool
# Postgres SQL Tool

The "postgres-generic" tool executes a pre-defined SQL statement.
A "postgres-sql" tool executes a pre-defined SQL statement against a postgres
database. It's compatible with any of the following sources:
- [alloydb-postgres](../sources/alloydb-pg.md)
- [cloud-sql-postgres](../sources/cloud-sql-pg.md)
- [postgres](../sources/postgres.md)

## Requirements
The specified SQL statement is executed as a [prepared statement][pg-prepare],
and specified parameters will inserted according to their position: e.g. "$1"
will be the first parameter specified, "$@" will be the second parameter, and so
on.

PostgreSQL Generic Tools require one of the following sources:
- [postgres](../sources/postgres.md)

[pg-prepare]: https://www.postgresql.org/docs/current/sql-prepare.html

## Example

```yaml
tools:
search_flights_by_number:
kind: postgres-generic
kind: postgres-sql
source: my-pg-instance
statement: |
SELECT * FROM flights
Expand Down
23 changes: 5 additions & 18 deletions internal/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import (
cloudsqlpgsrc "github.com/googleapis/genai-toolbox/internal/sources/cloudsqlpg"
postgressrc "github.com/googleapis/genai-toolbox/internal/sources/postgres"
"github.com/googleapis/genai-toolbox/internal/tools"
alloydbpgtool "github.com/googleapis/genai-toolbox/internal/tools/alloydbpg"
cloudsqlpgtool "github.com/googleapis/genai-toolbox/internal/tools/cloudsqlpg"
postgrestool "github.com/googleapis/genai-toolbox/internal/tools/postgres"
"github.com/googleapis/genai-toolbox/internal/tools/postgressql"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -92,7 +90,7 @@ func (c *SourceConfigs) UnmarshalYAML(node *yaml.Node) error {
}

// ToolConfigs is a type used to allow unmarshal of the tool configs
type ToolConfigs map[string]tools.Config
type ToolConfigs map[string]tools.ToolConfig

// validate interface
var _ yaml.Unmarshaler = &ToolConfigs{}
Expand All @@ -114,20 +112,8 @@ func (c *ToolConfigs) UnmarshalYAML(node *yaml.Node) error {
return fmt.Errorf("missing 'kind' field for %q", name)
}
switch k.Kind {
case alloydbpgtool.ToolKind:
actual := alloydbpgtool.GenericConfig{Name: name}
if err := n.Decode(&actual); err != nil {
return fmt.Errorf("unable to parse as %q: %w", k.Kind, err)
}
(*c)[name] = actual
case cloudsqlpgtool.ToolKind:
actual := cloudsqlpgtool.GenericConfig{Name: name}
if err := n.Decode(&actual); err != nil {
return fmt.Errorf("unable to parse as %q: %w", k.Kind, err)
}
(*c)[name] = actual
case postgrestool.ToolKind:
actual := postgrestool.GenericConfig{Name: name}
case postgressql.ToolKind:
actual := postgressql.Config{Name: name}
if err := n.Decode(&actual); err != nil {
return fmt.Errorf("unable to parse as %q: %w", k.Kind, err)
}
Expand All @@ -140,6 +126,7 @@ func (c *ToolConfigs) UnmarshalYAML(node *yaml.Node) error {
return nil
}

// ToolConfigs is a type used to allow unmarshal of the toolset configs
type ToolsetConfigs map[string]tools.ToolsetConfig

// validate interface
Expand Down
4 changes: 4 additions & 0 deletions internal/sources/alloydbpg/alloydb_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (s Source) SourceKind() string {
return SourceKind
}

func (s *Source) PostgresPool() *pgxpool.Pool {
return s.Pool
}

func initAlloyDBPgConnectionPool(project, region, cluster, instance, user, pass, dbname string) (*pgxpool.Pool, error) {
// Configure the driver to connect to the database
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, pass, dbname)
Expand Down
4 changes: 4 additions & 0 deletions internal/sources/cloudsqlpg/cloud_sql_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (s Source) SourceKind() string {
return SourceKind
}

func (s *Source) PostgresPool() *pgxpool.Pool {
return s.Pool
}

func initCloudSQLPgConnectionPool(project, region, instance, user, pass, dbname string) (*pgxpool.Pool, error) {
// Configure the driver to connect to the database
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, pass, dbname)
Expand Down
4 changes: 4 additions & 0 deletions internal/sources/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (s Source) SourceKind() string {
return SourceKind
}

func (s *Source) PostgresPool() *pgxpool.Pool {
return s.Pool
}

func initPostgresConnectionPool(host, port, user, pass, dbname string) (*pgxpool.Pool, error) {
// urlExample := "postgres:dd//username:password@localhost:5432/database_name"
i := fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, pass, host, port, dbname)
Expand Down
69 changes: 0 additions & 69 deletions internal/tools/alloydbpg/alloydb_pg.go

This file was deleted.

79 changes: 0 additions & 79 deletions internal/tools/alloydbpg/alloydb_pg_test.go

This file was deleted.

69 changes: 0 additions & 69 deletions internal/tools/cloudsqlpg/cloud_sql_pg.go

This file was deleted.

Loading

0 comments on commit f630965

Please sign in to comment.