Skip to content

Commit

Permalink
Accept extra args to build the connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Mar 23, 2023
1 parent 2c5f020 commit 4fab6c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions modules/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func StartContainer(ctx context.Context, opts ...MySQLContainerOption) (*MySQLCo
return &MySQLContainer{container, username, password, database}, nil
}

func (c *MySQLContainer) ConnectionString(ctx context.Context) (string, error) {
func (c *MySQLContainer) ConnectionString(ctx context.Context, args ...string) (string, error) {
containerPort, err := c.MappedPort(ctx, "3306/tcp")
if err != nil {
return "", err
Expand All @@ -90,7 +90,15 @@ func (c *MySQLContainer) ConnectionString(ctx context.Context) (string, error) {
return "", err
}

connectionString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", c.username, c.password, host, containerPort.Port(), c.database)
extraArgs := ""
if len(args) > 0 {
extraArgs = strings.Join(args, "&")
}
if extraArgs != "" {
extraArgs = "?" + extraArgs
}

connectionString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s%s", c.username, c.password, host, containerPort.Port(), c.database, extraArgs)
return connectionString, nil
}

Expand Down
2 changes: 1 addition & 1 deletion modules/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestMySQL(t *testing.T) {
})

// perform assertions
connectionString, _ := container.ConnectionString(ctx)
connectionString, _ := container.ConnectionString(ctx, "tls=skip-verify")

db, err := sql.Open("mysql", connectionString)
if err != nil {
Expand Down

0 comments on commit 4fab6c6

Please sign in to comment.