Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support database schema #76

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions cmd/sponge/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
17 changes: 12 additions & 5 deletions pkg/ggorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"context"
"database/sql"
"fmt"
"log"
"os"
"time"

"github.com/uptrace/opentelemetry-go-extra/otelgorm"
mysqlDriver "gorm.io/driver/mysql"
"gorm.io/driver/postgres"
Expand All @@ -17,6 +13,9 @@ import (
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"gorm.io/plugin/dbresolver"
"log"
"os"
"time"
)

const (
Expand Down Expand Up @@ -77,7 +76,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...)

Expand All @@ -86,6 +85,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())
Expand Down