From 94224f15cf691148d96d57f329ba9eba3de6c9dd Mon Sep 17 00:00:00 2001 From: zhengya Date: Tue, 14 Jan 2025 14:45:51 +0800 Subject: [PATCH] fix: fails to initialize postgres session --- server/controller/db/metadb/common/gorm.go | 29 +++++++++---------- .../migrator/schema/rawsql/postgres/sqlfmt.go | 2 +- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/server/controller/db/metadb/common/gorm.go b/server/controller/db/metadb/common/gorm.go index 65dc5e1b59a..a5429b6d00b 100644 --- a/server/controller/db/metadb/common/gorm.go +++ b/server/controller/db/metadb/common/gorm.go @@ -48,7 +48,7 @@ func GetConnector(cfg config.Config, useDatabase bool, timeout uint32, multiStat case config.MetaDBTypeMySQL: return getMySQLConnector(cfg, useDatabase, timeout, multiStatements) case config.MetaDBTypePostgreSQL: - return getPostgreSQLConnector(cfg, useDatabase, timeout, multiStatements) + return getPostgreSQLConnector(cfg, useDatabase, timeout) default: return nil, fmt.Errorf("unsupported database type: %s", cfg.Type) } @@ -87,24 +87,23 @@ func getMySQLConnector(cfg config.Config, useDatabase bool, timeout uint32, mult return connector, nil } -func getPostgreSQLConnector(cfg config.Config, useDatabase bool, timeout uint32, multiStatements bool) (driver.Connector, error) { - var database string - var schema string - if useDatabase { - database = cfg.Database - schema = cfg.Schema - } - +func getPostgreSQLConnector(cfg config.Config, useDatabase bool, timeout uint32) (driver.Connector, error) { connStr := "user=" + cfg.UserName + " password=" + cfg.UserPassword + " host=" + cfg.Host + " port=" + fmt.Sprintf("%d", cfg.Port) + - " dbname=" + database + - " search_path=" + schema + " connect_timeout=" + fmt.Sprintf("%d", int(timeout)) + " sslmode=disable" + " client_encoding=UTF8" + var database string + if useDatabase { + database = cfg.Database + connStr += " dbname=" + database + + " search_path=" + cfg.Schema + } + + log.Infof("postgres connection string: %s", connStr) connector, err := postgres_driver.NewConnector(connStr) if err != nil { log.Error("get database(%s) connector failed with error: %v", database, err.Error()) @@ -149,16 +148,16 @@ func InitSession(cfg config.Config, connector driver.Connector) (*gorm.DB, error func getDialector(cfg config.Config, connector driver.Connector) gorm.Dialector { switch cfg.Type { case config.MetaDBTypeMySQL: - return getMySQLDialector(cfg, connector) + return getMySQLDialector(connector) case config.MetaDBTypePostgreSQL: - return getPostgresDialector(cfg, connector) + return getPostgresDialector(connector) default: log.Errorf("unsupported database type: %s", cfg.Type) return nil } } -func getMySQLDialector(cfg config.Config, conn driver.Connector) gorm.Dialector { +func getMySQLDialector(conn driver.Connector) gorm.Dialector { return mysql.New(mysql.Config{ Conn: sql.OpenDB(conn), DefaultStringSize: 256, // string 类型字段的默认长度 @@ -169,7 +168,7 @@ func getMySQLDialector(cfg config.Config, conn driver.Connector) gorm.Dialector }) } -func getPostgresDialector(cfg config.Config, conn driver.Connector) gorm.Dialector { +func getPostgresDialector(conn driver.Connector) gorm.Dialector { return postgres.New(postgres.Config{ Conn: sql.OpenDB(conn), }) diff --git a/server/controller/db/metadb/migrator/schema/rawsql/postgres/sqlfmt.go b/server/controller/db/metadb/migrator/schema/rawsql/postgres/sqlfmt.go index 55cf640a3b3..86994139075 100644 --- a/server/controller/db/metadb/migrator/schema/rawsql/postgres/sqlfmt.go +++ b/server/controller/db/metadb/migrator/schema/rawsql/postgres/sqlfmt.go @@ -47,7 +47,7 @@ func (f *SqlFmt) SelectDatabase() string { } func (f *SqlFmt) SelectTable(tableName string) string { - return fmt.Sprintf("SELECT tablename FROM pg_catalog.pg_tables WHERE datname='%s' AND schemaname='%s' AND tablename='%s'", f.cfg.Database, f.cfg.Schema, tableName) + return fmt.Sprintf("SELECT table_name FROM information_schema.tables WHERE table_catalog='%s' AND table_schema='%s' AND table_name='%s'", f.cfg.Database, f.cfg.Schema, tableName) } func (f *SqlFmt) SelectColumn(tableName, columnName string) string {