Skip to content

Commit

Permalink
Merge pull request #97 from btnguyen2k/release
Browse files Browse the repository at this point in the history
Merge to main after releasing new version 1.2.0
  • Loading branch information
btnguyen2k authored Jan 6, 2024
2 parents 0ccd395 + 8d22083 commit 4655bb7
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 87 deletions.
16 changes: 12 additions & 4 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ updates:
schedule:
interval: "weekly"
day: "sunday"
# open-pull-requests-limit: 10
# ignore:
# - dependency-name: "*"
# update-types: ["version-update:semver-major"] # do not update to major version as it may contain breaking changes

- package-ecosystem: "gomod"
directory: "/module_test"
schedule:
interval: "weekly"
day: "sunday"

- package-ecosystem: "gomod"
directory: "/module_test_real"
schedule:
interval: "weekly"
day: "sunday"
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ err = tx.Commit()
if err != nil {
panic(err)
}

rowsAffected1, err1 := fmt.Println(result1.RowsAffected())
if err1 != nil {
panic(err1)
Expand All @@ -123,6 +124,44 @@ fmt.Println("RowsAffected:", rowsAffected2) // output "RowsAffected: 1"
>
> You can use [EXISTS function](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-functions.exists.html) for condition checking.
Notes on transactions:

- Results of `INSERT`/`UPDATE`/`DELETE` statements are not available until the transaction is committed. Which means, calling
`RowsAffected()` before `Commit()` will return `0, ErrInTx`.
- If the connection which has a non-commit/non-rollback transaction is used to execute another statement, the statement is
added to the transaction. If the transaction is being committed or rolled back, the execution of the statement will fail
with error `ErrInTx`. For example:

```go
conn, _ := db.Conn(context.Background())
tx, err := conn.BeginTx(context.Background(), nil)
if err != nil {
panic(err)
}
result1, _ := tx.Exec(`INSERT INTO "tbltest" VALUE {'app': ?, 'user': ?, 'active': ?}`, "app0", "user1", true)

// the statement is added to the existing transaction
// also, result2.RowsAffected() is not available until the transaction is committed
result2, _ := conn.ExecContext(context.Background(), `INSERT INTO "tbltest" VALUE {'app': ?, 'user': ?, 'duration': ?}`, "app0", "user2", 1.23)

err = tx.Commit()
if err != nil {
panic(err)
}

rowsAffected1, err1 := fmt.Println(result1.RowsAffected())
if err1 != nil {
panic(err1)
}
fmt.Println("RowsAffected:", rowsAffected1) // output "RowsAffected: 1"

rowsAffected2, err2 := fmt.Println(result2.RowsAffected())
if err2 != nil {
panic(err2)
}
fmt.Println("RowsAffected:", rowsAffected2) // output "RowsAffected: 1"
```

## Caveats

**Numerical values** are stored in DynamoDB as floating point numbers. Hence, numbers are always read back as `float64`.
Expand Down
10 changes: 10 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# godynamo release notes

## 2024-01-06 - v1.2.0

### Added/Refactoring

- Refactor transaction support

### Fixed/Improvement

- Fix: empty transaction should be committed successfully

## 2024-01-02 - v1.1.1

### Fixed/Improvement
Expand Down
73 changes: 66 additions & 7 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"errors"
"fmt"
"sync"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -13,8 +14,11 @@ import (
)

var (
ErrInTx = errors.New("statement is in the middle of a transaction, result is not available until committed")
ErrInvalidTxStage = errors.New("invalid transaction stage, cannot execute statement ")
ErrInTx = errors.New("there is an ongoing transaction, new transaction/statement or fetching result is not allowed")
ErrInvalidTxStage = errors.New("invalid transaction stage")
ErrNoTx = errors.New("no transaction is in progress")
ErrTxCommitting = errors.New("transaction is being committed")
ErrTxRollingBack = errors.New("transaction is being rolled back")
)

type txMode int
Expand All @@ -23,8 +27,10 @@ const (
txNone txMode = iota
txStarted
txCommitting
txRollingBack
)

// txStmt holds a statement to be executed in a transaction.
type txStmt struct {
stmt *Stmt
values []driver.NamedValue
Expand All @@ -37,6 +43,8 @@ type executeStatementOutputWrapper func() *dynamodb.ExecuteStatementOutput
type Conn struct {
client *dynamodb.Client // AWS DynamoDB client
timeout time.Duration
lock sync.Mutex
tx *Tx
txMode txMode
txStmtList []*txStmt
}
Expand All @@ -58,11 +66,29 @@ func (c *Conn) ensureContext(ctx context.Context) context.Context {
}

func (c *Conn) commit() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.tx == nil {
return ErrNoTx
}
if c.txMode == txRollingBack {
return ErrTxRollingBack
}
if c.txMode != txStarted && c.txMode != txCommitting {
return ErrInvalidTxStage
}
c.txMode = txCommitting
defer func() {
c.tx = nil
c.txMode = txNone
c.txStmtList = nil
}()
c.txMode = txCommitting

if len(c.txStmtList) == 0 {
//empty transaction should be successful
return nil
}

txStmts := make([]types.ParameterizedStatement, len(c.txStmtList))
for i, txStmt := range c.txStmtList {
params := make([]types.AttributeValue, len(txStmt.values))
Expand Down Expand Up @@ -95,7 +121,20 @@ func (c *Conn) commit() error {
}

func (c *Conn) rollback() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.tx == nil {
return ErrNoTx
}
if c.txMode == txCommitting {
return ErrTxCommitting
}
if c.txMode != txStarted && c.txMode != txRollingBack {
return ErrInvalidTxStage
}
c.txMode = txRollingBack
defer func() {
c.tx = nil
c.txMode = txNone
c.txStmtList = nil
}()
Expand All @@ -104,16 +143,24 @@ func (c *Conn) rollback() error {

// execute executes a PartiQL query and returns the result output.
func (c *Conn) executeContext(ctx context.Context, stmt *Stmt, values []driver.NamedValue) (executeStatementOutputWrapper, error) {
//fmt.Printf("[DEBUG] executeContext: in-tx %5v - %s\n", c.tx != nil, stmt.query)
if c.txMode == txStarted {
// transaction has started and not yet committed or rolled back
// --> can add more statements to the transaction
txStmt := txStmt{stmt: stmt, values: values}
c.txStmtList = append(c.txStmtList, &txStmt)
return func() *dynamodb.ExecuteStatementOutput {
return txStmt.output
}, ErrInTx
}
if c.txMode != txNone {
// transaction is in the middle of committing or rolling back
// --> can neither add more statements to the transaction nor execute any statement
return nil, ErrInvalidTxStage
}

/* not in transaction mode, execute the statement normally */

params := make([]types.AttributeValue, len(values))
var err error
for i, v := range values {
Expand Down Expand Up @@ -211,13 +258,19 @@ func (c *Conn) Prepare(query string) (driver.Stmt, error) {

// PrepareContext implements driver.ConnPrepareContext/PrepareContext.
//
// Note: since v1.2.0, this function returns ErrInTx if there is an outgoing transaction.
//
// @Available since v0.2.0
func (c *Conn) PrepareContext(_ context.Context, query string) (driver.Stmt, error) {
return parseQuery(c, query)
}

// Close implements driver.Conn/Close.
func (c *Conn) Close() error {
if c.tx != nil {
//rolling back any outgoing transaction
return c.tx.Rollback()
}
return nil
}

Expand All @@ -226,13 +279,19 @@ func (c *Conn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), driver.TxOptions{})
}

// BeginTx implements driver.ConnBeginTx/BeginTx.
// BeginTx implements driver.Conn/BeginTx.
//
// @Available since v0.2.0
func (c *Conn) BeginTx(_ context.Context, _ driver.TxOptions) (driver.Tx, error) {
c.txMode = txStarted
c.txStmtList = make([]*txStmt, 0)
return &Tx{conn: c}, nil
c.lock.Lock()
defer c.lock.Unlock()
if c.tx == nil {
c.tx = &Tx{conn: c}
c.txMode = txStarted
c.txStmtList = make([]*txStmt, 0)
return c.tx, nil
}
return c.tx, ErrInTx
}

// CheckNamedValue implements driver.NamedValueChecker/CheckNamedValue.
Expand Down
2 changes: 1 addition & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package godynamo

const (
// Version holds the semantic version number of this module.
Version = "1.1.1"
Version = "1.2.0"
)

// This file contains module's metadata only, which is package level documentation and module Version string.
Expand Down
1 change: 1 addition & 0 deletions module_test/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ func _verifyGSIInfo(t *testing.T, testName string, row map[string]interface{}, g
}

func _fetchAllRows(dbRows *sql.Rows) ([]map[string]interface{}, error) {
defer func() { _ = dbRows.Close() }()
colTypes, err := dbRows.ColumnTypes()
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 4655bb7

Please sign in to comment.