diff --git a/CHANGELOG.md b/CHANGELOG.md index 26386a7..3d6b077 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning]. - -## [0.5.4] - 2024-11-04 +## [0.5.4] - 2024-11-13 ### Added in 0.5.4 diff --git a/connector/connector.go b/connector/connector.go index dd54149..47d803d 100644 --- a/connector/connector.go +++ b/connector/connector.go @@ -15,6 +15,7 @@ import ( "github.com/senzing-garage/go-databasing/connectororacle" "github.com/senzing-garage/go-databasing/connectorpostgresql" "github.com/senzing-garage/go-databasing/connectorsqlite" + "github.com/senzing-garage/go-databasing/dbhelper" ) // ---------------------------------------------------------------------------- @@ -33,18 +34,9 @@ Input func NewConnector(ctx context.Context, databaseURL string) (driver.Connector, error) { var result driver.Connector - parsedURL, err := url.Parse(databaseURL) + parsedURL, err := dbhelper.ParseDatabaseURL(databaseURL) if err != nil { - - if strings.HasPrefix(databaseURL, "postgresql") { - index := strings.LastIndex(databaseURL, ":") - newDatabaseURL := databaseURL[:index] + "/" + databaseURL[index+1:] - parsedURL, err = url.Parse(newDatabaseURL) - } - - if err != nil { - return result, err - } + return result, err } // Parse URL: https://pkg.go.dev/net/url#URL diff --git a/dbhelper/dbhelper.go b/dbhelper/dbhelper.go index d11fe7a..ca377c9 100644 --- a/dbhelper/dbhelper.go +++ b/dbhelper/dbhelper.go @@ -25,7 +25,7 @@ func ExtractSqliteDatabaseFilename(databaseURL string) (string, error) { return result, fmt.Errorf("sqlite3 URL schema needed") } - parsedURL, err := url.Parse(databaseURL) + parsedURL, err := ParseDatabaseURL(databaseURL) if err != nil { return result, err } @@ -68,3 +68,26 @@ func GetMessenger(componentID int, idMessages map[int]string, callerSkip int, op } return result } + +/* +Function ParseDatabaseURL is fixes database URLs prior to parsing them. + +Input + - databaseURL: The database URL to be parsed. + +Output + - [url.URL] + +[url.URL]: https://pkg.go.dev/net/url#URL +*/ +func ParseDatabaseURL(databaseURL string) (*url.URL, error) { + result, err := url.Parse(databaseURL) + if err != nil { + if strings.HasPrefix(databaseURL, "postgresql") { + index := strings.LastIndex(databaseURL, ":") + newDatabaseURL := databaseURL[:index] + "/" + databaseURL[index+1:] + result, err = url.Parse(newDatabaseURL) + } + } + return result, err +} diff --git a/dbhelper/dbhelper_test.go b/dbhelper/dbhelper_test.go index b9f7b3a..38b7ccc 100644 --- a/dbhelper/dbhelper_test.go +++ b/dbhelper/dbhelper_test.go @@ -10,6 +10,7 @@ import ( type testCaseMetadata struct { databaseFilename string databaseURL string + fixedDatabaseURL string name string succeeds bool } @@ -55,6 +56,12 @@ var testCasesForMultiPlatform = []testCaseMetadata{ databaseURL: "postgresql://username:password@hostname:5432/G2/?schema=schemaname", succeeds: false, }, + { + name: "postgresql-003", + databaseURL: "postgresql://username:password@hostname:5432:G2/?schema=schemaname", + fixedDatabaseURL: "postgresql://username:password@hostname:5432/G2/?schema=schemaname", + succeeds: false, + }, { name: "sqlite-001", databaseURL: "sqlite3://na:na@/tmp/sqlite/G2C.db", @@ -89,3 +96,17 @@ func TestGetMessenger(test *testing.T) { options := []interface{}{} _ = GetMessenger(1, map[int]string{}, 0, options...) } + +func TestParseDatabaseURL(test *testing.T) { + for _, testCase := range testCases { + test.Run(testCase.name, func(test *testing.T) { + result, err := ParseDatabaseURL(testCase.databaseURL) + require.NoError(test, err) + if len(testCase.fixedDatabaseURL) > 0 { + assert.Equal(test, testCase.fixedDatabaseURL, result.String()) + } else { + assert.Equal(test, testCase.databaseURL, result.String()) + } + }) + } +} diff --git a/go.mod b/go.mod index ed26142..d1b4923 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( golang.org/x/net v0.31.0 // indirect golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.20.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20241113154021-e0fbfb71d213 // indirect google.golang.org/grpc v1.68.0 // indirect google.golang.org/protobuf v1.35.1 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect diff --git a/go.sum b/go.sum index 4cdbf03..db07b95 100644 --- a/go.sum +++ b/go.sum @@ -87,8 +87,8 @@ golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU= golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E= golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= -google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 h1:XVhgTWWV3kGQlwJHR3upFWZeTsei6Oks1apkZSeonIE= -google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI= +google.golang.org/genproto/googleapis/rpc v0.0.0-20241113154021-e0fbfb71d213 h1:L+WcQXqkyf5MX6g7AudgYEuJjmYbqSRkTmJqGfAPw+Y= +google.golang.org/genproto/googleapis/rpc v0.0.0-20241113154021-e0fbfb71d213/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI= google.golang.org/grpc v1.68.0 h1:aHQeeJbo8zAkAa3pRzrVjZlbz6uSfeOXlJNQM0RAbz0= google.golang.org/grpc v1.68.0/go.mod h1:fmSPC5AsjSBCK54MyHRx48kpOti1/jRfOlwEWywNjWA= google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=