Skip to content

Commit 0489759

Browse files
Feature: add application_name to database config
1 parent cb135f8 commit 0489759

File tree

5 files changed

+43
-78
lines changed

5 files changed

+43
-78
lines changed

config/config.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ type Contract struct {
4141

4242
// Database
4343
type Database struct {
44-
Path string `yaml:"path,omitempty"`
45-
Kind string `yaml:"kind" validate:"required,oneof=sqlite postgres mysql clickhouse elasticsearch"`
46-
Host string `yaml:"host" validate:"required_with=Port User Database"`
47-
Port int `yaml:"port" validate:"required_with=Host User Database,gt=-1,lt=65535"`
48-
User string `yaml:"user" validate:"required_with=Host Port Database"`
49-
Password string `yaml:"password"`
50-
Database string `yaml:"database" validate:"required_with=Host Port User"`
51-
SchemaName string `yaml:"schema_name"`
44+
Path string `yaml:"path,omitempty"`
45+
Kind string `yaml:"kind" validate:"required,oneof=sqlite postgres mysql clickhouse elasticsearch"`
46+
Host string `yaml:"host" validate:"required_with=Port User Database"`
47+
Port int `yaml:"port" validate:"required_with=Host User Database,gt=-1,lt=65535"`
48+
User string `yaml:"user" validate:"required_with=Host Port Database"`
49+
Password string `yaml:"password"`
50+
Database string `yaml:"database" validate:"required_with=Host Port User"`
51+
SchemaName string `yaml:"schema_name"`
52+
ApplicationName string `yaml:"application_name"`
5253
}
5354

5455
// Hasura -

database/bun.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql"
66
"fmt"
7+
"net/url"
78
"runtime"
89

910
"github.com/dipdup-net/go-lib/config"
@@ -41,14 +42,25 @@ func (db *Bun) Connect(ctx context.Context, cfg config.Database) error {
4142
db.sqldb = conn
4243
db.conn = bun.NewDB(db.sqldb, pgdialect.New())
4344
} else {
45+
values := make(url.Values)
46+
values.Set("sslmode", "disable")
47+
if cfg.ApplicationName != "" {
48+
values.Set("application_name", cfg.ApplicationName)
49+
}
50+
4451
connStr := fmt.Sprintf(
45-
"postgres://%s:%s@%s:%d/%s?sslmode=disable",
52+
"postgres://%s:%s@%s:%d/%s",
4653
cfg.User,
4754
cfg.Password,
4855
cfg.Host,
4956
cfg.Port,
5057
cfg.Database,
5158
)
59+
60+
if len(values) > 0 {
61+
connStr = fmt.Sprintf("%s?%s", connStr, values.Encode())
62+
}
63+
5264
conn, err := sql.Open("postgres", connStr)
5365
if err != nil {
5466
return err

database/db_test.go

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (s *DBTestSuite) TearDownSuite() {
7575
s.Require().NoError(s.psqlContainer.Terminate(ctx))
7676
}
7777

78-
func (s *DBTestSuite) TestStateCreate() {
78+
func (s *DBTestSuite) BeforeTest(suiteName, testName string) {
7979
db, err := sql.Open("postgres", s.psqlContainer.GetDSN())
8080
s.Require().NoError(err)
8181

@@ -87,7 +87,9 @@ func (s *DBTestSuite) TestStateCreate() {
8787
s.Require().NoError(err)
8888
s.Require().NoError(fixtures.Load())
8989
s.Require().NoError(db.Close())
90+
}
9091

92+
func (s *DBTestSuite) TestStateCreate() {
9193
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
9294
defer ctxCancel()
9395

@@ -107,18 +109,6 @@ func (s *DBTestSuite) TestStateCreate() {
107109
}
108110

109111
func (s *DBTestSuite) TestStateUpdate() {
110-
db, err := sql.Open("postgres", s.psqlContainer.GetDSN())
111-
s.Require().NoError(err)
112-
113-
fixtures, err := testfixtures.New(
114-
testfixtures.Database(db),
115-
testfixtures.Dialect("postgres"),
116-
testfixtures.Directory("fixtures"),
117-
)
118-
s.Require().NoError(err)
119-
s.Require().NoError(fixtures.Load())
120-
s.Require().NoError(db.Close())
121-
122112
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
123113
defer ctxCancel()
124114

@@ -137,18 +127,6 @@ func (s *DBTestSuite) TestStateUpdate() {
137127
}
138128

139129
func (s *DBTestSuite) TestState() {
140-
db, err := sql.Open("postgres", s.psqlContainer.GetDSN())
141-
s.Require().NoError(err)
142-
143-
fixtures, err := testfixtures.New(
144-
testfixtures.Database(db),
145-
testfixtures.Dialect("postgres"),
146-
testfixtures.Directory("fixtures"),
147-
)
148-
s.Require().NoError(err)
149-
s.Require().NoError(fixtures.Load())
150-
s.Require().NoError(db.Close())
151-
152130
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
153131
defer ctxCancel()
154132

@@ -161,26 +139,14 @@ func (s *DBTestSuite) TestState() {
161139
}
162140

163141
func (s *DBTestSuite) TestDeleteState() {
164-
db, err := sql.Open("postgres", s.psqlContainer.GetDSN())
165-
s.Require().NoError(err)
166-
167-
fixtures, err := testfixtures.New(
168-
testfixtures.Database(db),
169-
testfixtures.Dialect("postgres"),
170-
testfixtures.Directory("fixtures"),
171-
)
172-
s.Require().NoError(err)
173-
s.Require().NoError(fixtures.Load())
174-
s.Require().NoError(db.Close())
175-
176142
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
177143
defer ctxCancel()
178144
newState := State{
179145
IndexName: testIndex,
180146
}
181147
s.Require().NoError(s.db.DeleteState(ctx, &newState))
182148

183-
_, err = s.db.State(ctx, testIndex)
149+
_, err := s.db.State(ctx, testIndex)
184150
s.Require().Error(err)
185151
}
186152

@@ -199,18 +165,6 @@ func (s *DBTestSuite) TestMakeComments() {
199165
}
200166

201167
func (s *DBTestSuite) TestExec() {
202-
db, err := sql.Open("postgres", s.psqlContainer.GetDSN())
203-
s.Require().NoError(err)
204-
205-
fixtures, err := testfixtures.New(
206-
testfixtures.Database(db),
207-
testfixtures.Dialect("postgres"),
208-
testfixtures.Directory("fixtures"),
209-
)
210-
s.Require().NoError(err)
211-
s.Require().NoError(fixtures.Load())
212-
s.Require().NoError(db.Close())
213-
214168
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
215169
defer ctxCancel()
216170

@@ -221,7 +175,7 @@ func (s *DBTestSuite) TestExec() {
221175

222176
func TestSuite_Run(t *testing.T) {
223177
ts := new(DBTestSuite)
224-
for _, typ := range []string{"gorm", "pg-go", "bun"} {
178+
for _, typ := range []string{"bun"} {
225179
ts.typ = typ
226180
}
227181
suite.Run(t, ts)

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ require (
1515
github.com/lib/pq v1.10.9
1616
github.com/pkg/errors v0.9.1
1717
github.com/prometheus/client_golang v1.16.0
18-
github.com/rs/zerolog v1.30.0
18+
github.com/rs/zerolog v1.31.0
1919
github.com/sergi/go-diff v1.3.1
2020
github.com/shopspring/decimal v1.3.1
2121
github.com/stretchr/testify v1.8.4
2222
github.com/testcontainers/testcontainers-go v0.22.0
2323
github.com/testcontainers/testcontainers-go/modules/postgres v0.22.0
2424
github.com/tidwall/gjson v1.16.0
25-
github.com/uptrace/bun v1.1.14
26-
github.com/uptrace/bun/dialect/pgdialect v1.1.14
25+
github.com/uptrace/bun v1.1.17
26+
github.com/uptrace/bun/dialect/pgdialect v1.1.17
2727
github.com/yhirose/go-peg v0.0.0-20210804202551-de25d6753cf1
2828
golang.org/x/crypto v0.19.0
2929
golang.org/x/text v0.14.0
@@ -61,7 +61,7 @@ require (
6161
github.com/leodido/go-urn v1.4.0 // indirect
6262
github.com/magiconair/properties v1.8.7 // indirect
6363
github.com/mattn/go-colorable v0.1.13 // indirect
64-
github.com/mattn/go-isatty v0.0.19 // indirect
64+
github.com/mattn/go-isatty v0.0.20 // indirect
6565
github.com/mattn/go-sqlite3 v1.14.17 // indirect
6666
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
6767
github.com/moby/patternmatcher v0.5.0 // indirect
@@ -84,7 +84,7 @@ require (
8484
github.com/tidwall/match v1.1.1 // indirect
8585
github.com/tidwall/pretty v1.2.1 // indirect
8686
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
87-
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
87+
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
8888
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
8989
go.opentelemetry.io/otel v1.19.0 // indirect
9090
go.opentelemetry.io/otel/trace v1.19.0 // indirect

go.sum

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,12 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
130130
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
131131
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
132132
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
133-
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
134133
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
135134
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
136-
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
137135
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
138-
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
139136
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
137+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
138+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
140139
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
141140
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
142141
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
@@ -181,8 +180,8 @@ github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPH
181180
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
182181
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
183182
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
184-
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
185-
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
183+
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
184+
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
186185
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
187186
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
188187
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
@@ -212,12 +211,12 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
212211
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
213212
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
214213
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
215-
github.com/uptrace/bun v1.1.14 h1:S5vvNnjEynJ0CvnrBOD7MIRW7q/WbtvFXrdfy0lddAM=
216-
github.com/uptrace/bun v1.1.14/go.mod h1:RHk6DrIisO62dv10pUOJCz5MphXThuOTpVNYEYv7NI8=
217-
github.com/uptrace/bun/dialect/pgdialect v1.1.14 h1:b7+V1KDJPQSFYgkG/6YLXCl2uvwEY3kf/GSM7hTHRDY=
218-
github.com/uptrace/bun/dialect/pgdialect v1.1.14/go.mod h1:v6YiaXmnKQ2FlhRD2c0ZfKd+QXH09pYn4H8ojaavkKk=
219-
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
220-
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
214+
github.com/uptrace/bun v1.1.17 h1:qxBaEIo0hC/8O3O6GrMDKxqyT+mw5/s0Pn/n6xjyGIk=
215+
github.com/uptrace/bun v1.1.17/go.mod h1:hATAzivtTIRsSJR4B8AXR+uABqnQxr3myKDKEf5iQ9U=
216+
github.com/uptrace/bun/dialect/pgdialect v1.1.17 h1:NsvFVHAx1Az6ytlAD/B6ty3cVE6j9Yp82bjqd9R9hOs=
217+
github.com/uptrace/bun/dialect/pgdialect v1.1.17/go.mod h1:fLBDclNc7nKsZLzNjFL6BqSdgJzbj2HdnyOnLoDvAME=
218+
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
219+
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
221220
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
222221
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
223222
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
@@ -271,11 +270,10 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
271270
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
272271
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
273272
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
274-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
275-
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
276273
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
277274
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
278275
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
276+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
279277
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
280278
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
281279
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

0 commit comments

Comments
 (0)