-
Notifications
You must be signed in to change notification settings - Fork 9
Feature: add bun as ORM and test #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
421296b
Feature: add bun as ORM and test
aopoltorzhicky 8180aca
Fix: gorm comments
aopoltorzhicky 097f4ea
Fix: support tag bun for comments building
aopoltorzhicky c85b82d
Update packages
aopoltorzhicky 4756a50
Bump go versio in tests.yml
aopoltorzhicky 1f90d97
Fix: state model
aopoltorzhicky a2f0424
Add range partition manager
aopoltorzhicky 8fb3e3f
Feature: create tables with patitioning
aopoltorzhicky 29ec6a2
Fix: change sql driver for bun
aopoltorzhicky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.