Skip to content

Commit

Permalink
feat: postgres tool (#463)
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Linville <[email protected]>
  • Loading branch information
g-linville authored Mar 4, 2025
1 parent 3290e6e commit d9a1866
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ tools:
reference: ./search/tavily
pagerduty:
reference: ./pagerduty
postgres:
reference: ./postgres

knowledgeDataSources:
notion-data-source:
Expand Down
50 changes: 50 additions & 0 deletions postgres/credential/tool.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Name: Postgres Credential
Share Credential: postgres-cred as postgres
Type: credential

---
Name: postgres-cred
Tools: github.com/obot-platform/tools/generic-credential

#!sys.call github.com/obot-platform/tools/generic-credential

{
"promptInfo": {
"fields": [
{
"name": "Postgres Host",
"description": "The host of your Postgres database",
"env": "POSTGRES_HOST"
},
{
"name": "Postgres Port",
"description": "The port number of your Postgres database",
"env": "POSTGRES_PORT"
},
{
"name": "Postgres Database",
"description": "The name of your Postgres database",
"env": "POSTGRES_DATABASE"
},
{
"name": "Postgres Username",
"description": "The username for your Postgres database",
"env": "POSTGRES_USERNAME"
},
{
"name": "Postgres Password",
"description": "The password for your Postgres database",
"env": "POSTGRES_PASSWORD",
"sensitive": true
},
{
"name": "Postgres sslmode (optional)",
"description": "The setting for the sslmode parameter when connecting to the database. `require` is the default.",
"env": "POSTGRES_SSLMODE"
}
],
"message": "Enter your Postgres database credentials."
},
"validationTool": "github.com/obot-platform/tools/postgres/validate-cred.gpt"
}

20 changes: 20 additions & 0 deletions postgres/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module postgres_query_executor

go 1.24.0

require (
gorm.io/driver/postgres v1.5.11
gorm.io/gorm v1.25.12
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.2 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/text v0.22.0 // indirect
)
36 changes: 36 additions & 0 deletions postgres/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
79 changes: 79 additions & 0 deletions postgres/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"encoding/json"
"fmt"
"os"
"strings"

"gorm.io/driver/postgres"
"gorm.io/gorm"
)

func main() {
query := os.Getenv("QUERY")
host := os.Getenv("POSTGRES_HOST")
port := os.Getenv("POSTGRES_PORT")
database := os.Getenv("POSTGRES_DATABASE")
user := os.Getenv("POSTGRES_USERNAME")
password := os.Getenv("POSTGRES_PASSWORD")
sslmode := os.Getenv("POSTGRES_SSLMODE")

if sslmode == "" {
sslmode = "require"
}

dsn := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=%s",
user, password, host, port, database, sslmode)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Printf("Error opening database: %v\n", err)
os.Exit(1)
}

// Check for validate mode
if len(os.Args) > 1 && os.Args[1] == "validate" {
// Test the connection with a simple query
var result int
err = db.Raw("SELECT 1").Scan(&result).Error
if err != nil {
fmt.Printf("Error validating connection: %v\n", err)
os.Exit(1)
}

fmt.Println("Database connection validated successfully")
os.Exit(0)
}

// Check if the query returns data
queryUpper := strings.TrimSpace(strings.ToUpper(query))
returnsData := strings.Contains(queryUpper, "SELECT") ||
strings.Contains(queryUpper, "RETURNING") ||
strings.HasPrefix(queryUpper, "VALUES") ||
strings.HasPrefix(queryUpper, "SHOW")

if returnsData {
var results []map[string]any
err := db.Raw(query).Scan(&results).Error
if err != nil {
fmt.Printf("Error executing query: %v\n", err)
os.Exit(1)
}

jsonData, err := json.Marshal(results)
if err != nil {
fmt.Printf("Error marshaling JSON: %v\n", err)
os.Exit(1)
}

fmt.Println(string(jsonData))
} else {
result := db.Exec(query)
if result.Error != nil {
fmt.Printf("Error executing query: %v\n", result.Error)
os.Exit(1)
}

fmt.Printf("Query executed successfully! Rows affected: %d\n", result.RowsAffected)
}
}
10 changes: 10 additions & 0 deletions postgres/tool.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Name: Postgres Query Executor
Description: Execute SQL queries against a Postgres database
Credential: ./credential
Param: query: The SQL query to execute

#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool

---
!metadata:*:icon
https://cdn.jsdelivr.net/npm/simple-icons@v13/icons/postgresql.svg
5 changes: 5 additions & 0 deletions postgres/validate-cred.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name: postgres-cred-validate
Description: Validate PostgreSQL credentials
Metadata: index: false

#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool validate

0 comments on commit d9a1866

Please sign in to comment.