From a1b7b8dff549e149a33ab031cb688d5044d789f8 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 10 Jul 2023 12:09:10 -0300 Subject: [PATCH] remove DSN func and use ConnString --- cmd/substreams-sink-postgres/inject_csv.go | 2 +- db/db.go | 2 +- db/dsn.go | 10 +--------- db/dsn_test.go | 2 +- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/cmd/substreams-sink-postgres/inject_csv.go b/cmd/substreams-sink-postgres/inject_csv.go index e2edcba..c78416a 100644 --- a/cmd/substreams-sink-postgres/inject_csv.go +++ b/cmd/substreams-sink-postgres/inject_csv.go @@ -64,7 +64,7 @@ func injectCSVE(cmd *cobra.Command, args []string) error { return fmt.Errorf("unable to create input store: %w", err) } - pool, err := pgxpool.Connect(ctx, fmt.Sprintf("%s pool_min_conns=%d pool_max_conns=%d", postgresDSN.DSN(), 2, 3)) + pool, err := pgxpool.Connect(ctx, fmt.Sprintf("%s pool_min_conns=%d pool_max_conns=%d", postgresDSN.ConnString(), 2, 3)) if err != nil { return fmt.Errorf("connecting to postgres: %w", err) } diff --git a/db/db.go b/db/db.go index 3e7a2c1..2a7bee1 100644 --- a/db/db.go +++ b/db/db.go @@ -57,7 +57,7 @@ func NewLoader( return nil, fmt.Errorf("parse dsn: %w", err) } - db, err := sql.Open("postgres", dsn.connString()) + db, err := sql.Open("postgres", dsn.ConnString()) if err != nil { return nil, fmt.Errorf("open db connection: %w", err) } diff --git a/db/dsn.go b/db/dsn.go index 6b853be..0663205 100644 --- a/db/dsn.go +++ b/db/dsn.go @@ -23,14 +23,6 @@ type DSN struct { options []string } -func (c *DSN) DSN() string { - out := fmt.Sprintf("host=%s port=%d user=%s dbname=%s %s", c.host, c.port, c.username, c.database, strings.Join(c.options, " ")) - if c.password != "" { - out = out + " password=" + c.password - } - return out -} - func ParseDSN(dsn string) (*DSN, error) { expanded, err := envsubst.Eval(dsn, os.Getenv) if err != nil { @@ -87,7 +79,7 @@ func ParseDSN(dsn string) (*DSN, error) { return d, nil } -func (c *DSN) connString() string { +func (c *DSN) ConnString() string { out := fmt.Sprintf("host=%s port=%d user=%s dbname=%s %s", c.host, c.port, c.username, c.database, strings.Join(c.options, " ")) if c.password != "" { out = out + " password=" + c.password diff --git a/db/dsn_test.go b/db/dsn_test.go index 8c5a685..c1a4bb8 100644 --- a/db/dsn_test.go +++ b/db/dsn_test.go @@ -35,7 +35,7 @@ func TestParseDSN(t *testing.T) { require.Error(t, err) } else { require.NoError(t, err) - assert.Equal(t, test.expectConnString, d.connString()) + assert.Equal(t, test.expectConnString, d.ConnString()) assert.Equal(t, test.expectSchema, d.schema) } })