diff --git a/server/db/mysql/adapter.go b/server/db/mysql/adapter.go index c658940f0b390073df2a3aeb6c2d7f6921263307..99e6bfed153e98c4a2d0ff4e4140b9e9455034e3 100644 --- a/server/db/mysql/adapter.go +++ b/server/db/mysql/adapter.go @@ -10,6 +10,7 @@ import ( "encoding/json" "errors" "hash/fnv" + "net/url" "strconv" "strings" "time" @@ -62,8 +63,7 @@ type configType struct { // for the full list of fields. ms.Config // Deprecated. - DSN string `json:"dsn,omitempty"` - Database string `json:"database,omitempty"` + DSN string `json:"dsn,omitempty"` // Connection pool settings. // @@ -114,18 +114,26 @@ func (a *adapter) Open(jsonconfig json.RawMessage) error { // MySql config is specified. Use it. a.dbName = config.DBName a.dsn = dsn - if config.DSN != "" || config.Database != "" { - return errors.New("mysql config: `dsn` and `database` fields are deprecated. Please, specify individual connection settings via mysql.Config: https://pkg.go.dev/github.com/go-sql-driver/mysql#Config") + if config.DSN != "" { + return errors.New("mysql config: conflicting config and DSN are provided") } } else { - // Otherwise, use DSN and Database to configure database connection. + // Otherwise, use DSN to configure database connection. // Note: this method is deprecated. if config.DSN != "" { - a.dsn = config.DSN + // Remove optional schema. + a.dsn = strings.TrimPrefix(config.DSN, "mysql://") } else { a.dsn = defaultDSN } - a.dbName = config.Database + + // Parse out the database name from the DSN. + // Add schema to create a valid URL. + if uri, err := url.Parse("mysql://" + a.dsn); err == nil { + a.dbName = strings.TrimPrefix(uri.Path, "/") + } else { + return err + } } if a.dbName == "" { diff --git a/server/db/postgres/adapter.go b/server/db/postgres/adapter.go index 0511ac7d37b70c7aca481044dbf4c653d06fa778..11db5eb8c7cf1f02a1267ca9b629a3121fb3e08c 100644 --- a/server/db/postgres/adapter.go +++ b/server/db/postgres/adapter.go @@ -11,6 +11,7 @@ import ( "fmt" "hash/fnv" "log" + "net/url" "reflect" "strconv" "strings" @@ -45,9 +46,6 @@ type adapter struct { } const ( - defaultDSN = "postgresql://postgres:postgres@localhost:5432/tinode?sslmode=disable&connect_timeout=10" - defaultDatabase = "tinode" - adpVersion = 113 adapterName = "postgres" @@ -106,7 +104,7 @@ func (a *adapter) Open(jsonconfig json.RawMessage) error { } if len(jsonconfig) < 2 { - return errors.New("adapter postgres missing config") + return errors.New("postgres adapter missing config") } var err error @@ -118,21 +116,16 @@ func (a *adapter) Open(jsonconfig json.RawMessage) error { if config.DSN != "" { a.dsn = config.DSN + if uri, err := url.Parse(a.dsn); err == nil { + a.dbName = strings.TrimPrefix(uri.Path, "/") + } else { + return err + } } else { - a.dsn, err = setConnStr(config) - if err != nil { + if a.dsn, err = setConnStr(config); err != nil { return err } - } - - a.dbName = config.DBName - - if a.dsn == "" { - a.dsn = defaultDSN - } - - if a.dbName == "" { - a.dbName = defaultDatabase + a.dbName = config.DBName } if a.maxResults <= 0 { @@ -143,9 +136,8 @@ func (a *adapter) Open(jsonconfig json.RawMessage) error { a.maxMessageResults = defaultMaxMessageResults } - a.poolConfig, err = pgxpool.ParseConfig(a.dsn) - if err != nil { - return errors.New("adapter postgres failed to parse config: " + err.Error()) + if a.poolConfig, err = pgxpool.ParseConfig(a.dsn); err != nil { + return errors.New("postgres adapter failed to parse DSN: " + err.Error()) } // ConnectConfig creates a new Pool and immediately establishes one connection. diff --git a/server/main.go b/server/main.go index 5b2d27bdef19ef920369a668ce03edfaed28ac37..b2af3634c9763b245e7fdfbe420dbaca587c71f7 100644 --- a/server/main.go +++ b/server/main.go @@ -413,10 +413,10 @@ func main() { } err = store.Store.Open(workerId, config.Store) + logs.Info.Println("DB adapter", store.Store.GetAdapterName()) if err != nil { logs.Err.Fatal("Failed to connect to DB: ", err) } - logs.Info.Println("DB adapter", store.Store.GetAdapterName()) defer func() { store.Store.Close() logs.Info.Println("Closed database connection(s)") diff --git a/server/tinode.conf b/server/tinode.conf index 8587b944672253c3a497a77a38931ddf1a38b353..1150260be9a2b2506d919043af612db0b306d2b3 100644 --- a/server/tinode.conf +++ b/server/tinode.conf @@ -233,6 +233,10 @@ "Port": "5432", "DBName": "tinode", + // DSN: alternative way of specifying database configuration, passed unchanged + // to the driver. See https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING + // "dsn": "postgresql://postgres:postgres@localhost:5432/tinode?sslmode=disable&connect_timeout=10", + // PostgreSQL connection pool settings. // Maximum number of open connections to the database. Zero means unlimited. "max_open_conns": 64, @@ -261,11 +265,10 @@ // Parse time values to time.Time. Required. "ParseTime": true, - // Deprecated connection settings. Kept for backward compatibility. - // DSN, passed unchanged to MySQL driver. See https://github.com/go-sql-driver/mysql#dsn-data-source-name for syntax. + // DSN: alternative way of specifying database configuration, passed unchanged + // to MySQL driver. See https://github.com/go-sql-driver/mysql#dsn-data-source-name for syntax. + // DSN may optionally start with mysql:// // "dsn": "root@tcp(localhost)/tinode?parseTime=true&collation=utf8mb4_unicode_ci", - // Name of the main database. - // "database": "tinode", // MySQL connection pool settings. // Maximum number of open connections to the database. Default: 0 (unlimited).