-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: add bun datastore package * feat(sqlc): add sqlc dep and config * feat(sqlc): add sqlc statements * feat(sqlc): add sqlc datastore * feat(sqlc): add sqlc toggle env var
- Loading branch information
1 parent
a598bb3
commit 16899a2
Showing
42 changed files
with
1,669 additions
and
85 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
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
2 changes: 1 addition & 1 deletion
2
adapters/datastore/audit_record.go → datastore/bun/audit_record.go
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,4 +1,4 @@ | ||
package datastore | ||
package bun | ||
|
||
import ( | ||
"context" | ||
|
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
2 changes: 1 addition & 1 deletion
2
adapters/datastore/idempotency_key.go → datastore/bun/idempotency_key.go
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,4 +1,4 @@ | ||
package datastore | ||
package bun | ||
|
||
import ( | ||
"context" | ||
|
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,4 +1,4 @@ | ||
package datastore | ||
package bun | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package bun | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/dialect/pgdialect" | ||
|
||
// loading bun's official Postgres driver. | ||
_ "github.com/uptrace/bun/driver/pgdriver" | ||
|
||
rocketride "github.com/rafael-piovesan/go-rocket-ride" | ||
) | ||
|
||
type sqlStore struct { | ||
conn *bun.DB | ||
db bun.IDB | ||
} | ||
|
||
func NewStore(dsn string) (s rocketride.Datastore, err error) { | ||
sqldb, err := sql.Open("pg", dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
db := bun.NewDB(sqldb, pgdialect.New()) | ||
s = &sqlStore{ | ||
conn: db, | ||
db: db, | ||
} | ||
return | ||
} | ||
|
||
func (s *sqlStore) Atomic(ctx context.Context, fn func(store rocketride.Datastore) error) (err error) { | ||
tx, err := s.conn.BeginTx(ctx, &sql.TxOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
if p := recover(); p != nil { | ||
_ = tx.Rollback() | ||
|
||
switch e := p.(type) { | ||
case runtime.Error: | ||
panic(e) | ||
case error: | ||
err = fmt.Errorf("panic err: %v", p) | ||
return | ||
default: | ||
panic(e) | ||
} | ||
} | ||
if err != nil { | ||
if rbErr := tx.Rollback(); rbErr != nil { | ||
err = fmt.Errorf("tx err: %v, rb err: %v", err, rbErr) | ||
} | ||
} else { | ||
err = tx.Commit() | ||
} | ||
}() | ||
|
||
// TODO: check if it works for nested transactions as well | ||
newStore := &sqlStore{ | ||
conn: s.conn, | ||
db: tx, | ||
} | ||
err = fn(newStore) | ||
return err | ||
} | ||
|
||
var _ rocketride.Datastore = (*sqlStore)(nil) |
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,4 +1,4 @@ | ||
package datastore | ||
package bun | ||
|
||
import ( | ||
"context" | ||
|
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,4 +1,4 @@ | ||
package datastore | ||
package bun | ||
|
||
import ( | ||
"context" | ||
|
Oops, something went wrong.