From f19d4201f50a1e1977f0e5778a48603a9b1a12de Mon Sep 17 00:00:00 2001 From: Saif Ali Aljanahi Date: Thu, 17 Oct 2024 20:48:34 +0300 Subject: [PATCH 1/3] feat: support "set search_path" in Postgresql --- pkg/ggorm/gorm.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/ggorm/gorm.go b/pkg/ggorm/gorm.go index 3186a6bc..1aa34c92 100644 --- a/pkg/ggorm/gorm.go +++ b/pkg/ggorm/gorm.go @@ -9,14 +9,9 @@ import ( "os" "time" - "github.com/uptrace/opentelemetry-go-extra/otelgorm" mysqlDriver "gorm.io/driver/mysql" - "gorm.io/driver/postgres" - "gorm.io/driver/sqlite" - "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" - "gorm.io/plugin/dbresolver" ) const ( @@ -77,7 +72,7 @@ func InitMysql(dsn string, opts ...Option) (*gorm.DB, error) { } // InitPostgresql init postgresql -func InitPostgresql(dsn string, opts ...Option) (*gorm.DB, error) { +func InitPostgresql(dsn string, schemaName string, opts ...Option) (*gorm.DB, error) { o := defaultOptions() o.apply(opts...) @@ -86,6 +81,14 @@ func InitPostgresql(dsn string, opts ...Option) (*gorm.DB, error) { return nil, err } + // Set schema search path if a schema is provided + if schemaName != "" { + err = db.Exec(fmt.Sprintf("SET search_path TO %s", schemaName)).Error + if err != nil { + return nil, fmt.Errorf("setting search path to schema %s, err: %v", schemaName, err) + } + } + // register trace plugin if o.enableTrace { err = db.Use(otelgorm.NewPlugin()) From 0f3edebd1d96ed8392daaa13d57639cc446b8053 Mon Sep 17 00:00:00 2001 From: Saif Ali Aljanahi Date: Thu, 17 Oct 2024 20:49:08 +0300 Subject: [PATCH 2/3] feat: support querying from the specified schema type --- cmd/sponge/server/handler.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cmd/sponge/server/handler.go b/cmd/sponge/server/handler.go index 96153978..8d0c98aa 100644 --- a/cmd/sponge/server/handler.go +++ b/cmd/sponge/server/handler.go @@ -76,7 +76,9 @@ func ListTables(c *gin.Context) { case ggorm.DBDriverMysql, ggorm.DBDriverTidb: tables, err = getMysqlTables(form.Dsn) case ggorm.DBDriverPostgresql: - tables, err = getPostgresqlTables(form.Dsn) + // Assuming the schema is sent via the `Dsn` in this example + schemaName := extractSchemaFromDsn(form.Dsn) + tables, err = getPostgresqlTables(form.Dsn, schemaName) case ggorm.DBDriverSqlite: tables, err = getSqliteTables(form.Dsn) case mgo.DBDriverName: @@ -308,6 +310,16 @@ func checkFileType(typeName string) bool { return false } +// extractSchemaFromDsn extracts schema name from the DSN +func extractSchemaFromDsn(dsn string) string { + // Implement logic to extract schema from DSN if provided, otherwise default to "public" + parsedUrl, err := url.Parse(dsn) + if err != nil || parsedUrl.Query().Get("search_path") == "" { + return "public" + } + return parsedUrl.Query().Get("search_path") +} + func checkSameFile(files []string, file string) bool { for _, v := range files { if v == file { @@ -420,7 +432,7 @@ func getMysqlTables(dsn string) ([]string, error) { return tables, nil } -func getPostgresqlTables(dsn string) ([]string, error) { +func getPostgresqlTables(dsn string, schemaName string) ([]string, error) { dsn = utils.AdaptivePostgresqlDsn(dsn) db, err := ggorm.InitPostgresql(dsn) if err != nil { @@ -429,7 +441,8 @@ func getPostgresqlTables(dsn string) ([]string, error) { defer ggorm.CloseSQLDB(db) var tables []string - err = db.Raw("SELECT table_name FROM information_schema.tables WHERE table_schema = ?", "public").Scan(&tables).Error + query := "SELECT table_name FROM information_schema.tables WHERE table_schema = ?" + err = db.Raw(query, schemaName).Scan(&tables).Error if err != nil { return nil, err } From 03994e5a21eaa22751b38d55d83280818b6418cd Mon Sep 17 00:00:00 2001 From: Saif Ali Aljanahi Date: Sun, 20 Oct 2024 18:19:16 +0300 Subject: [PATCH 3/3] fix: add the required imports --- go.mod | 6 +++--- go.sum | 12 ++++++------ pkg/ggorm/gorm.go | 12 ++++++++---- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 88f0b182..3840e0f6 100644 --- a/go.mod +++ b/go.mod @@ -61,8 +61,8 @@ require ( gopkg.in/yaml.v3 v3.0.1 gorm.io/driver/mysql v1.5.2 gorm.io/driver/postgres v1.5.4 - gorm.io/driver/sqlite v1.5.4 - gorm.io/gorm v1.25.5 + gorm.io/driver/sqlite v1.5.6 + gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde gorm.io/plugin/dbresolver v1.5.1 // todo generate the local sponge template code version here ) @@ -159,7 +159,7 @@ require ( github.com/mailru/easyjson v0.7.6 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.19 // indirect - github.com/mattn/go-sqlite3 v1.14.17 // indirect + github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/mitchellh/copystructure v1.0.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect diff --git a/go.sum b/go.sum index b611d860..9732150e 100644 --- a/go.sum +++ b/go.sum @@ -499,8 +499,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= -github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= @@ -1195,13 +1195,13 @@ gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs= gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8= gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo= gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0= -gorm.io/driver/sqlite v1.5.4 h1:IqXwXi8M/ZlPzH/947tn5uik3aYQslP9BVveoax0nV0= -gorm.io/driver/sqlite v1.5.4/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4= +gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= +gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= -gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= -gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde h1:9DShaph9qhkIYw7QF91I/ynrr4cOO2PZra2PFD7Mfeg= +gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= gorm.io/plugin/dbresolver v1.5.1 h1:s9Dj9f7r+1rE3nx/Ywzc85nXptUEaeOO0pt27xdopM8= gorm.io/plugin/dbresolver v1.5.1/go.mod h1:l4Cn87EHLEYuqUncpEeTC2tTJQkjngPSD+lo8hIvcT0= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/pkg/ggorm/gorm.go b/pkg/ggorm/gorm.go index 1aa34c92..c4bacf50 100644 --- a/pkg/ggorm/gorm.go +++ b/pkg/ggorm/gorm.go @@ -5,13 +5,17 @@ import ( "context" "database/sql" "fmt" - "log" - "os" - "time" - + "github.com/uptrace/opentelemetry-go-extra/otelgorm" mysqlDriver "gorm.io/driver/mysql" + "gorm.io/driver/postgres" + "gorm.io/driver/sqlite" + "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" + "gorm.io/plugin/dbresolver" + "log" + "os" + "time" ) const (