Skip to content

Commit 421296b

Browse files
Feature: add bun as ORM and test
1 parent e9794d1 commit 421296b

File tree

11 files changed

+1066
-97
lines changed

11 files changed

+1066
-97
lines changed

database/bun.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package database
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"fmt"
7+
8+
"github.com/dipdup-net/go-lib/config"
9+
"github.com/pkg/errors"
10+
"github.com/uptrace/bun"
11+
"github.com/uptrace/bun/dialect/pgdialect"
12+
"github.com/uptrace/bun/driver/pgdriver"
13+
)
14+
15+
// Bun -
16+
type Bun struct {
17+
sqldb *sql.DB
18+
conn *bun.DB
19+
}
20+
21+
// NewBun -
22+
func NewBun() *Bun {
23+
return new(Bun)
24+
}
25+
26+
// DB -
27+
func (db *Bun) DB() *bun.DB {
28+
return db.conn
29+
}
30+
31+
// Connect -
32+
func (db *Bun) Connect(ctx context.Context, cfg config.Database) error {
33+
if cfg.Kind != config.DBKindPostgres {
34+
return errors.Wrap(ErrUnsupportedDatabaseType, cfg.Kind)
35+
}
36+
if cfg.Path != "" {
37+
db.sqldb = sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(cfg.Path)))
38+
db.conn = bun.NewDB(db.sqldb, pgdialect.New())
39+
} else {
40+
db.sqldb = sql.OpenDB(pgdriver.NewConnector(
41+
pgdriver.WithAddr(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)),
42+
pgdriver.WithDatabase(cfg.Database),
43+
pgdriver.WithPassword(cfg.Password),
44+
pgdriver.WithUser(cfg.User),
45+
pgdriver.WithInsecure(true),
46+
))
47+
db.conn = bun.NewDB(db.sqldb, pgdialect.New())
48+
}
49+
return nil
50+
}
51+
52+
// Close -
53+
func (db *Bun) Close() error {
54+
if err := db.conn.Close(); err != nil {
55+
return err
56+
}
57+
return db.sqldb.Close()
58+
}
59+
60+
// Ping -
61+
func (db *Bun) Ping(ctx context.Context) error {
62+
if db.conn == nil {
63+
return ErrConnectionIsNotInitialized
64+
}
65+
return db.conn.PingContext(ctx)
66+
}
67+
68+
// State -
69+
func (db *Bun) State(ctx context.Context, indexName string) (*State, error) {
70+
var s State
71+
err := db.conn.NewSelect().Model(&s).Where("index_name = ?", indexName).Limit(1).Scan(ctx)
72+
return &s, err
73+
}
74+
75+
// CreateState -
76+
func (db *Bun) CreateState(ctx context.Context, s *State) error {
77+
_, err := db.conn.NewInsert().Model(s).Exec(ctx)
78+
return err
79+
}
80+
81+
// UpdateState -
82+
func (db *Bun) UpdateState(ctx context.Context, s *State) error {
83+
_, err := db.conn.NewUpdate().Model(s).Where("index_name = ?", s.IndexName).Exec(ctx)
84+
return err
85+
}
86+
87+
// DeleteState -
88+
func (db *Bun) DeleteState(ctx context.Context, s *State) error {
89+
_, err := db.conn.NewDelete().Model(s).Where("index_name = ?", s.IndexName).Exec(ctx)
90+
return err
91+
}
92+
93+
// MakeTableComment -
94+
func (db *Bun) MakeTableComment(ctx context.Context, name string, comment string) error {
95+
_, err := db.conn.ExecContext(ctx,
96+
`COMMENT ON TABLE ? IS ?`,
97+
bun.Ident(name),
98+
comment)
99+
100+
return err
101+
}
102+
103+
// MakeColumnComment -
104+
func (db *Bun) MakeColumnComment(ctx context.Context, tableName string, columnName string, comment string) error {
105+
_, err := db.conn.ExecContext(ctx,
106+
`COMMENT ON COLUMN ?.? IS ?`,
107+
bun.Ident(tableName),
108+
bun.Ident(columnName),
109+
comment)
110+
111+
return err
112+
}

database/comment.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package database
22

33
import (
44
"context"
5-
"github.com/dipdup-net/go-lib/hasura"
6-
"github.com/pkg/errors"
75
"reflect"
86
"strings"
7+
8+
"github.com/dipdup-net/go-lib/hasura"
9+
"github.com/pkg/errors"
910
)
1011

12+
// MakeComments -
1113
func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}) error {
1214
if models == nil {
1315
return nil

database/container.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package database
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/docker/go-connections/nat"
9+
"github.com/testcontainers/testcontainers-go"
10+
"github.com/testcontainers/testcontainers-go/modules/postgres"
11+
"github.com/testcontainers/testcontainers-go/wait"
12+
)
13+
14+
type (
15+
PostgreSQLContainer struct {
16+
testcontainers.Container
17+
cfg PostgreSQLContainerConfig
18+
mappedPort nat.Port
19+
}
20+
21+
PostgreSQLContainerConfig struct {
22+
User string
23+
Password string
24+
Database string
25+
Host string
26+
Port int
27+
Image string
28+
}
29+
)
30+
31+
// NewPostgreSQLContainer -
32+
func NewPostgreSQLContainer(ctx context.Context, cfg PostgreSQLContainerConfig) (*PostgreSQLContainer, error) {
33+
if cfg.Port == 0 {
34+
cfg.Port = 5432
35+
}
36+
port := fmt.Sprintf("%d/tcp", cfg.Port)
37+
38+
if cfg.Image == "" {
39+
cfg.Image = "postgres:15"
40+
}
41+
42+
container, err := postgres.RunContainer(ctx,
43+
testcontainers.WithImage(cfg.Image),
44+
postgres.WithDatabase(cfg.Database),
45+
postgres.WithUsername(cfg.User),
46+
postgres.WithPassword(cfg.Password),
47+
testcontainers.WithWaitStrategy(wait.ForLog("database system is ready to accept connections").WithOccurrence(2).WithStartupTimeout(5*time.Second)),
48+
)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
host, err := container.Host(ctx)
54+
if err != nil {
55+
return nil, fmt.Errorf("getting host for: %w", err)
56+
}
57+
cfg.Host = host
58+
59+
mappedPort, err := container.MappedPort(ctx, nat.Port(port))
60+
if err != nil {
61+
return nil, fmt.Errorf("getting mapped port for %s: %w", port, err)
62+
}
63+
64+
return &PostgreSQLContainer{
65+
Container: container,
66+
cfg: cfg,
67+
mappedPort: mappedPort,
68+
}, nil
69+
}
70+
71+
// GetDSN -
72+
func (c PostgreSQLContainer) GetDSN() string {
73+
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", c.cfg.User, c.cfg.Password, c.cfg.Host, c.mappedPort.Port(), c.cfg.Database)
74+
}
75+
76+
// MappedPort -
77+
func (c PostgreSQLContainer) MappedPort() nat.Port {
78+
return c.mappedPort
79+
}

database/db.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ import (
1111
"github.com/rs/zerolog/log"
1212
)
1313

14+
// SchemeCommenter -
15+
type SchemeCommenter interface {
16+
MakeTableComment(ctx context.Context, name string, comment string) error
17+
MakeColumnComment(ctx context.Context, tableName string, columnName string, comment string) error
18+
}
19+
1420
// Database -
1521
type Database interface {
1622
Connect(ctx context.Context, cfg config.Database) error
1723

1824
StateRepository
25+
SchemeCommenter
1926

2027
driver.Pinger
2128
io.Closer

0 commit comments

Comments
 (0)