Skip to content

Commit

Permalink
feat(connection): add support for mysql connection via ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed Dec 20, 2024
1 parent 55ab8c1 commit 366e075
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
ErrInvalidMySQLURLFormat = errors.New(
"invalid url - valid format: mysql://user:password@tcp(host:port)/db",
)
// ErrInvalidOracleURLFormat is the error used to notify the user that the oracle url is invalid.
ErrInvalidOracleURLFormat = errors.New(
"invalid url - valid format: oracle://user:pass@server/service_name",
)
Expand Down Expand Up @@ -192,6 +193,8 @@ func BuildConnectionFromOpts(opts command.Options) (string, command.Options, err

return connDB.String(), opts, nil
case drivers.MySQL:
var netParam = "tcp"

if opts.Socket != "" {
if !validSocketFile(opts.Socket) {
return "", opts, ErrInvalidSocketFile
Expand All @@ -210,10 +213,15 @@ func BuildConnectionFromOpts(opts command.Options) (string, command.Options, err
), opts, nil
}

if opts.SSHHost != "" {
netParam = "mysql+tcp"
}

return fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s",
"%s:%s@%s(%s:%s)/%s",
opts.User,
opts.Pass,
netParam,
opts.Host,
opts.Port,
opts.DBName,
Expand Down
46 changes: 46 additions & 0 deletions pkg/connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ func TestBuildConnectionFromOptsFromURL(t *testing.T) {
},
},
// mysql
{
name: "valid mysql localhost via ssh",
given: given{
opts: command.Options{
SSHHost: "example.com",
SSHPort: "22",
SSHUser: "ssh-user",
SSHPass: "ssh-pass",
URL: "mysql://user:password@mysql+tcp(localhost:3306)/db",
},
},
want: want{
uri: "user:password@mysql+tcp(localhost:3306)/db",
},
},
{
name: "valid mysql localhost",
given: given{
Expand Down Expand Up @@ -452,6 +467,26 @@ func TestBuildConnectionFromOptsUserData(t *testing.T) {
},
},
// mysql
{
name: "success - localhost - mysql - via ssh",
given: given{
opts: command.Options{
Driver: drivers.MySQL,
SSHHost: "example.com",
SSHPort: "22",
SSHUser: "ssh-user",
SSHPass: "ssh-pass",
User: "user",
Pass: "password",
Host: "localhost",
Port: "3306",
DBName: "db",
},
},
want: want{
uri: "user:password@mysql+tcp(localhost:3306)/db",
},
},
{
name: "success - localhost - mysql",
given: given{
Expand Down Expand Up @@ -742,6 +777,17 @@ func TestFormatMySQLURL(t *testing.T) {
given given
want want
}{
{
name: "valid mysql localhost",
given: given{
opts: command.Options{
URL: "mysql://user:password@mysql+tcp(localhost:3306)/db",
},
},
want: want{
uri: "user:password@mysql+tcp(localhost:3306)/db",
},
},
{
name: "valid mysql localhost",
given: given{
Expand Down

0 comments on commit 366e075

Please sign in to comment.