Skip to content
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

Use cached increment value #33

Merged
merged 4 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/go-rel/primaryreplica v0.4.0
github.com/go-rel/rel v0.38.0
github.com/go-rel/sql v0.11.0
github.com/go-rel/sql v0.12.0
github.com/go-sql-driver/mysql v1.6.0
github.com/stretchr/testify v1.8.0
)
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
github.com/go-rel/primaryreplica v0.4.0 h1:lhU+4dh0/sDQEs602Chiz0SJDXewPU06baWQlx7oB3c=
github.com/go-rel/primaryreplica v0.4.0/go.mod h1:HUBz+BUvUcg9JpRRk9PstV9J/qlEOqaHIIllGncbUs8=
github.com/go-rel/rel v0.33.1/go.mod h1:DhB9Xmt/ymaumJAB6Z3Kq+IybLwQhhrzr6ZLeXMygPo=
github.com/go-rel/rel v0.37.0/go.mod h1:Zq18pQqXZbDh2JBCo29jgt+y90nZWkUvI+W9Ls29ans=
github.com/go-rel/rel v0.38.0 h1:XooFDMrzHNaZSNvH1ZrEpcn/7TvPz37z1kA66N3Ahjo=
github.com/go-rel/rel v0.38.0/go.mod h1:Zq18pQqXZbDh2JBCo29jgt+y90nZWkUvI+W9Ls29ans=
github.com/go-rel/sql v0.11.0 h1:MeyoMtfDpn9sZSQNMuo7YbN8M/z74eIy9UMN3m6uonQ=
github.com/go-rel/sql v0.11.0/go.mod h1:L4XKALdxaEGwT7ngflceoHVFSooUJap5TO/Yu8FGKJI=
github.com/go-rel/sql v0.12.0 h1:1iIm2JgUr854TjN2C2403A9nZKH1RwbMJp09SQC4HO8=
github.com/go-rel/sql v0.12.0/go.mod h1:Usxy37iCTA5aIqoJGekV4ATdCUOK5w2FiR00/VvvLJQ=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
22 changes: 10 additions & 12 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package mysql

import (
"context"
db "database/sql"
"strings"

Expand Down Expand Up @@ -49,14 +48,19 @@ func New(database *db.DB) rel.Adapter {
DeleteBuilder: deleteBuilder,
TableBuilder: tableBuilder,
IndexBuilder: indexBuilder,
IncrementFunc: incrementFunc,
Increment: getIncrement(database),
ErrorMapper: errorMapper,
DB: database,
}
}

// Open mysql connection using dsn.
func Open(dsn string) (rel.Adapter, error) {
var database, err = db.Open("mysql", rewriteDsn(dsn))
return New(database), err
}

func rewriteDsn(dsn string) string {
// force clientFoundRows=true
// this allows not found record check when updating a record.
if strings.ContainsRune(dsn, '?') {
Expand All @@ -65,8 +69,7 @@ func Open(dsn string) (rel.Adapter, error) {
dsn += "?clientFoundRows=true"
}

var database, err = db.Open("mysql", dsn)
return New(database), err
return dsn
}

// MustOpen mysql connection using dsn.
Expand All @@ -79,19 +82,14 @@ func MustOpen(dsn string) rel.Adapter {
return adapter
}

func incrementFunc(adapter sql.SQL) int {
func getIncrement(database *db.DB) int {
var (
variable string
increment int
rows, err = adapter.DoQuery(context.TODO(), "SHOW VARIABLES LIKE 'auto_increment_increment';", nil)
row = database.QueryRow("SHOW VARIABLES LIKE 'auto_increment_increment';")
)

check(err)

defer rows.Close()
rows.Next()
check(rows.Scan(&variable, &increment))

check(row.Scan(&variable, &increment))
return increment
}

Expand Down
22 changes: 8 additions & 14 deletions mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,6 @@ func TestAdapter_PrimaryReplica_specs(t *testing.T) {
AdapterSpecs(t, repo)
}

func TestAdapter_Open(t *testing.T) {
// with parameter
assert.NotPanics(t, func() {
adapter, _ := Open("root@tcp(localhost:3306)/rel_test?charset=utf8")
defer adapter.Close()
})

// without paremeter
assert.NotPanics(t, func() {
adapter, _ := Open("root@tcp(localhost:3306)/rel_test")
defer adapter.Close()
})
}

func TestAdapter_Transaction_commitError(t *testing.T) {
adapter, err := Open(dsn())
assert.Nil(t, err)
Expand All @@ -164,6 +150,14 @@ func TestAdapter_Exec_error(t *testing.T) {
assert.NotNil(t, err)
}

func TestRewriteDsn(t *testing.T) {
// with parameter
assert.Contains(t, rewriteDsn("root@tcp(localhost:3306)/rel_test?charset=utf8"), "&clientFoundRows=true")

// without paremeter
assert.Contains(t, rewriteDsn("root@tcp(localhost:3306)/rel_test"), "?clientFoundRows=true")
}

func TestCheck(t *testing.T) {
assert.Panics(t, func() {
check(errors.New("error"))
Expand Down