-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from dipdup-io/feature/new-orm
Feature: add bun as ORM and test
- Loading branch information
Showing
17 changed files
with
1,342 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
lint: | ||
golangci-lint run --go=1.18 | ||
golangci-lint run | ||
|
||
test: | ||
go test ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/dipdup-net/go-lib/config" | ||
"github.com/pkg/errors" | ||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/dialect/pgdialect" | ||
) | ||
|
||
// Bun - | ||
type Bun struct { | ||
sqldb *sql.DB | ||
conn *bun.DB | ||
} | ||
|
||
// NewBun - | ||
func NewBun() *Bun { | ||
return new(Bun) | ||
} | ||
|
||
// DB - | ||
func (db *Bun) DB() *bun.DB { | ||
return db.conn | ||
} | ||
|
||
// Connect - | ||
func (db *Bun) Connect(ctx context.Context, cfg config.Database) error { | ||
if cfg.Kind != config.DBKindPostgres { | ||
return errors.Wrap(ErrUnsupportedDatabaseType, cfg.Kind) | ||
} | ||
if cfg.Path != "" { | ||
conn, err := sql.Open("postgres", cfg.Path) | ||
if err != nil { | ||
return err | ||
} | ||
db.sqldb = conn | ||
db.conn = bun.NewDB(db.sqldb, pgdialect.New()) | ||
} else { | ||
connStr := fmt.Sprintf( | ||
"postgres://%s:%s@%s:%d/%s?sslmode=disable", | ||
cfg.User, | ||
cfg.Password, | ||
cfg.Host, | ||
cfg.Port, | ||
cfg.Database, | ||
) | ||
conn, err := sql.Open("postgres", connStr) | ||
if err != nil { | ||
return err | ||
} | ||
db.sqldb = conn | ||
db.conn = bun.NewDB(db.sqldb, pgdialect.New()) | ||
} | ||
maxOpenConns := 4 * runtime.GOMAXPROCS(0) | ||
db.sqldb.SetMaxOpenConns(maxOpenConns) | ||
db.sqldb.SetMaxIdleConns(maxOpenConns) | ||
return nil | ||
} | ||
|
||
// Close - | ||
func (db *Bun) Close() error { | ||
if err := db.conn.Close(); err != nil { | ||
return err | ||
} | ||
return db.sqldb.Close() | ||
} | ||
|
||
// Exec - | ||
func (db *Bun) Exec(ctx context.Context, query string, args ...any) (int64, error) { | ||
result, err := db.conn.ExecContext(ctx, query, args...) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return result.RowsAffected() | ||
} | ||
|
||
// Ping - | ||
func (db *Bun) Ping(ctx context.Context) error { | ||
if db.conn == nil { | ||
return ErrConnectionIsNotInitialized | ||
} | ||
return db.conn.PingContext(ctx) | ||
} | ||
|
||
// State - | ||
func (db *Bun) State(ctx context.Context, indexName string) (*State, error) { | ||
var s State | ||
err := db.conn.NewSelect().Model(&s).Where("index_name = ?", indexName).Limit(1).Scan(ctx) | ||
return &s, err | ||
} | ||
|
||
// CreateState - | ||
func (db *Bun) CreateState(ctx context.Context, s *State) error { | ||
_, err := db.conn.NewInsert().Model(s).Exec(ctx) | ||
return err | ||
} | ||
|
||
// UpdateState - | ||
func (db *Bun) UpdateState(ctx context.Context, s *State) error { | ||
_, err := db.conn.NewUpdate().Model(s).Where("index_name = ?", s.IndexName).Exec(ctx) | ||
return err | ||
} | ||
|
||
// DeleteState - | ||
func (db *Bun) DeleteState(ctx context.Context, s *State) error { | ||
_, err := db.conn.NewDelete().Model(s).Where("index_name = ?", s.IndexName).Exec(ctx) | ||
return err | ||
} | ||
|
||
// MakeTableComment - | ||
func (db *Bun) MakeTableComment(ctx context.Context, name string, comment string) error { | ||
_, err := db.conn.ExecContext(ctx, | ||
`COMMENT ON TABLE ? IS ?`, | ||
bun.Ident(name), | ||
comment) | ||
|
||
return err | ||
} | ||
|
||
// MakeColumnComment - | ||
func (db *Bun) MakeColumnComment(ctx context.Context, tableName string, columnName string, comment string) error { | ||
_, err := db.conn.ExecContext(ctx, | ||
`COMMENT ON COLUMN ?.? IS ?`, | ||
bun.Ident(tableName), | ||
bun.Ident(columnName), | ||
comment) | ||
|
||
return err | ||
} | ||
|
||
// CreateTable - | ||
func (db *Bun) CreateTable(ctx context.Context, model any, opts ...CreateTableOption) error { | ||
if model == nil { | ||
return nil | ||
} | ||
var options CreateTableOptions | ||
for i := range opts { | ||
opts[i](&options) | ||
} | ||
|
||
query := db.DB(). | ||
NewCreateTable(). | ||
Model(model) | ||
|
||
if options.ifNotExists { | ||
query = query.IfNotExists() | ||
} | ||
|
||
if options.partitionBy != "" { | ||
query = query.PartitionBy(options.partitionBy) | ||
} | ||
|
||
if options.temporary { | ||
query = query.Temp() | ||
} | ||
|
||
_, err := query.Exec(ctx) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.