Skip to content

Commit

Permalink
feat: add postgres conn with unix sockets (#199)
Browse files Browse the repository at this point in the history
* feat(connection): add changes to support connection to posgres through unix sockects

* test(connection): add test to make sure the connection string is well parsed

* docs(readme): add a example to the readme on how to connect to postgres through unix sockets

* build(makefile): add a new target command to help devs to connect to postgres database using sockets
  • Loading branch information
danvergara authored May 20, 2024
1 parent d9a4d74 commit e68ffcb
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ run-mysql: build
## run-mysql-socket: Runs the application with a connection to mysql through a socket file. In this example the socke file is located in /var/lib/mysql/mysql.sock.
run-mysql-socket: build
./dblab --socket /var/lib/mysql/mysql.sock --user myuser --pass password --db mydb --ssl enable --port 3306 --driver mysql

.PHONY: run-postgres-socket
## run-postgres-socket: Runs the application with a connection to mysql through a socket file. In this example the socke file is located in /var/lib/mysql/mysql.sock.
run-postgres-socket: build
./dblab --socket /var/run/postgresql --user myuser --db my_project --pass postgres --ssl disable --port 5432 --driver postgres --limit 50

.PHONY: run-mysql-socket-url
## run-mysql-socket-url: Runs the application with a connection to mysql through a socket file. In this example the socke file is located in /var/lib/mysql/mysql.sock.
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ $ dblab --url "mysql://user:password@unix(/path/to/socket/mysql.sock)/dbname?cha
$ dblab --socket /path/to/socket/mysql.sock --user user --db dbname --pass password --ssl disable --port 5432 --driver mysql --limit 50
```

Postgres connection through Unix sockets:

```sh
$ dblab --url "postgres://user:password@/dbname?host=/path/to/socket"
$ dblab --socket /path/to/socket --user user --db dbname --pass password --ssl disable --port 5432 --driver postgres --limit 50
```

Now, it is possible to ensure SSL connections with `PostgreSQL` databases. SSL related parameters has been added, such as `--sslcert`, `--sslkey`, `--sslpassword`, `--sslrootcert`. More information on how to use such connection flags can be found [here](https://www.postgresql.org/docs/current/libpq-connect.html).

```{ .sh .copy }
Expand Down
36 changes: 34 additions & 2 deletions pkg/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ func BuildConnectionFromOpts(opts command.Options) (string, command.Options, err
switch opts.Driver {
case drivers.Postgres:
query := url.Values{}
if opts.Socket != "" {
query.Add("host", opts.Socket)

connDB := url.URL{
Scheme: opts.Driver,
Path: fmt.Sprintf("/%s", opts.DBName),
RawQuery: query.Encode(),
}

switch {
case opts.User != "" && opts.Pass == "":
connDB.User = url.User(opts.User)
case opts.User != "" && opts.Pass != "":
connDB.User = url.UserPassword(opts.User, opts.Pass)
}

return connDB.String(), opts, nil
}

if opts.SSL != "" {
query.Add("sslmode", opts.SSL)
} else {
Expand Down Expand Up @@ -122,10 +141,23 @@ func BuildConnectionFromOpts(opts command.Options) (string, command.Options, err
return "", opts, ErrSocketFileDoNotExist
}

return fmt.Sprintf("%s:%s@unix(%s)/%s?charset=utf8", opts.User, opts.Pass, opts.Socket, opts.DBName), opts, nil
return fmt.Sprintf(
"%s:%s@unix(%s)/%s?charset=utf8",
opts.User,
opts.Pass,
opts.Socket,
opts.DBName,
), opts, nil
}

return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", opts.User, opts.Pass, opts.Host, opts.Port, opts.DBName), opts, nil
return fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s",
opts.User,
opts.Pass,
opts.Host,
opts.Port,
opts.DBName,
), opts, nil
case drivers.SQLite:
if hasValidSqlite3FileExtension(opts.DBName) {
return opts.DBName, opts, nil
Expand Down
72 changes: 70 additions & 2 deletions pkg/connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package connection
import (
"errors"
"fmt"
"net/url"
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/danvergara/dblab/pkg/command"
"github.com/danvergara/dblab/pkg/drivers"
"github.com/stretchr/testify/assert"
)

func createTempSocketFile(t *testing.T) *os.File {
Expand Down Expand Up @@ -37,6 +39,34 @@ func TestBuildConnectionFromOptsFromURL(t *testing.T) {
want want
}{
// postgres
{
name: "valid socket connection with postgres with no password or user",
given: given{
opts: command.Options{
URL: "postgres:///db?host=/path/to/socket",
},
},
want: want{
uri: fmt.Sprintf(
"postgres:///db?host=%s",
url.QueryEscape("/path/to/socket"),
),
},
},
{
name: "valid socket connection with postgres",
given: given{
opts: command.Options{
URL: "postgres://user:password@/db?host=/path/to/socket",
},
},
want: want{
uri: fmt.Sprintf(
"postgres://user:password@/db?host=%s",
url.QueryEscape("/path/to/socket"),
),
},
},
{
name: "valid postgres localhost",
given: given{
Expand Down Expand Up @@ -133,7 +163,10 @@ func TestBuildConnectionFromOptsFromURL(t *testing.T) {
name: "valid socket connection",
given: given{
opts: command.Options{
URL: fmt.Sprintf("mysql://user:password@unix(%s)/dbname?charset=utf8", socketFile.Name()),
URL: fmt.Sprintf(
"mysql://user:password@unix(%s)/dbname?charset=utf8",
socketFile.Name(),
),
},
},
want: want{
Expand Down Expand Up @@ -217,6 +250,41 @@ func TestBuildConnectionFromOptsUserData(t *testing.T) {
want want
}{
// postgres
{
name: "success - postgres socket connection without password",
given: given{
opts: command.Options{
Driver: drivers.Postgres,
User: "user",
DBName: "db",
Socket: "/path/to/socket",
},
},
want: want{
uri: fmt.Sprintf(
"postgres://user@/db?host=%s",
url.QueryEscape("/path/to/socket"),
),
},
},
{
name: "success - postgres socket connection",
given: given{
opts: command.Options{
Driver: drivers.Postgres,
User: "user",
Pass: "password",
DBName: "db",
Socket: "/path/to/socket",
},
},
want: want{
uri: fmt.Sprintf(
"postgres://user:password@/db?host=%s",
url.QueryEscape("/path/to/socket"),
),
},
},
{
name: "success - localhost with no explicit ssl mode - postgres",
given: given{
Expand Down

0 comments on commit e68ffcb

Please sign in to comment.