Skip to content
This repository was archived by the owner on Feb 10, 2023. It is now read-only.

Commit 2ebac13

Browse files
committed
feature: add session variables setting #13
usage: ./bin/mydumper ... --vars 'SET @@radon_streaming_fetch=true'
1 parent b983fd2 commit 2ebac13

File tree

12 files changed

+29
-15
lines changed

12 files changed

+29
-15
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: go
22
sudo: required
33
go:
44
- 1.8
5+
- 1.10.x
56

67
before_install:
78
- go get github.com/pierrre/gotestcover

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Usage: ./bin/mydumper -h [HOST] -P [PORT] -u [USER] -p [PASSWORD] -db [DATABASE]
4949
Table to dump
5050
-u string
5151
Username with privileges to run the dump
52+
-vars string
53+
Session variables
5254
5355
Examples:
5456
$./bin/mydumper -h 192.168.0.1 -P 3306 -u mock -p mock -db sbtest -o sbtest.sql

src/common/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Args struct {
3030
Database string
3131
Table string
3232
Outdir string
33+
SessionVars string
3334
Threads int
3435
ChunksizeInMB int
3536
StmtSize int

src/common/dumper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func allTables(log *xlog.Log, conn *Connection, args *Args) []string {
136136

137137
// Dumper used to start the dumper worker.
138138
func Dumper(log *xlog.Log, args *Args) {
139-
pool, err := NewPool(log, args.Threads, args.Address, args.User, args.Password)
139+
pool, err := NewPool(log, args.Threads, args.Address, args.User, args.Password, args.SessionVars)
140140
AssertNil(err)
141141
defer pool.Close()
142142

src/common/dumper_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
"strings"
1616
"testing"
1717

18+
"github.com/stretchr/testify/assert"
1819
"github.com/xelabs/go-mysqlstack/driver"
1920
querypb "github.com/xelabs/go-mysqlstack/sqlparser/depends/query"
2021
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
2122
"github.com/xelabs/go-mysqlstack/xlog"
22-
"github.com/stretchr/testify/assert"
2323
)
2424

2525
func TestDumper(t *testing.T) {
@@ -111,6 +111,7 @@ func TestDumper(t *testing.T) {
111111
fakedbs.AddQueryPattern("show create table .*", schemaResult)
112112
fakedbs.AddQueryPattern("show tables from .*", tablesResult)
113113
fakedbs.AddQueryPattern("select .*", selectResult)
114+
fakedbs.AddQueryPattern("set .*", &sqltypes.Result{})
114115
}
115116

116117
args := &Args{
@@ -123,6 +124,7 @@ func TestDumper(t *testing.T) {
123124
Threads: 16,
124125
StmtSize: 10000,
125126
IntervalMs: 500,
127+
SessionVars: "SET @@radon_streaming_fetch='ON'; SET @@xx=1;",
126128
}
127129

128130
os.RemoveAll(args.Outdir)

src/common/loader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func restoreTable(log *xlog.Log, table string, conn *Connection) int {
148148

149149
// Loader used to start the loader worker.
150150
func Loader(log *xlog.Log, args *Args) {
151-
pool, err := NewPool(log, args.Threads, args.Address, args.User, args.Password)
151+
pool, err := NewPool(log, args.Threads, args.Address, args.User, args.Password, args.SessionVars)
152152
AssertNil(err)
153153
defer pool.Close()
154154

src/common/pool.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ func (conn *Connection) StreamFetch(query string) (driver.Rows, error) {
4747
}
4848

4949
// NewPool creates the new pool.
50-
func NewPool(log *xlog.Log, cap int, address string, user string, password string) (*Pool, error) {
50+
func NewPool(log *xlog.Log, cap int, address string, user string, password string, vars string) (*Pool, error) {
5151
conns := make(chan *Connection, cap)
5252
for i := 0; i < cap; i++ {
5353
client, err := driver.NewConn(user, password, address, "", "utf8")
5454
if err != nil {
5555
return nil, err
5656
}
57-
conns <- &Connection{ID: i, client: client}
57+
conn := &Connection{ID: i, client: client}
58+
if vars != "" {
59+
conn.Execute(vars)
60+
}
61+
conns <- conn
5862
}
5963

6064
return &Pool{

src/common/pool_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"testing"
1515
"time"
1616

17+
"github.com/stretchr/testify/assert"
1718
"github.com/xelabs/go-mysqlstack/driver"
1819
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
1920
"github.com/xelabs/go-mysqlstack/xlog"
20-
"github.com/stretchr/testify/assert"
2121
)
2222

2323
func TestPool(t *testing.T) {
@@ -33,7 +33,7 @@ func TestPool(t *testing.T) {
3333
fakedbs.AddQueryPattern("select .*", &sqltypes.Result{})
3434
}
3535

36-
pool, err := NewPool(log, 8, address, "mock", "mock")
36+
pool, err := NewPool(log, 8, address, "mock", "mock", "")
3737
assert.Nil(t, err)
3838

3939
var wg sync.WaitGroup

src/common/streamer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ func Streamer(log *xlog.Log, args *Args) {
131131
var tables []string
132132
var wg sync.WaitGroup
133133

134-
fromPool, err := NewPool(log, args.Threads, args.Address, args.User, args.Password)
134+
fromPool, err := NewPool(log, args.Threads, args.Address, args.User, args.Password, args.SessionVars)
135135
AssertNil(err)
136136
defer fromPool.Close()
137137

138-
toPool, err := NewPool(log, args.Threads, args.ToAddress, args.ToUser, args.ToPassword)
138+
toPool, err := NewPool(log, args.Threads, args.ToAddress, args.ToUser, args.ToPassword, "")
139139
AssertNil(err)
140140
defer toPool.Close()
141141

src/common/streamer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ package common
1212
import (
1313
"testing"
1414

15+
"github.com/stretchr/testify/assert"
1516
"github.com/xelabs/go-mysqlstack/driver"
1617
querypb "github.com/xelabs/go-mysqlstack/sqlparser/depends/query"
1718
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
1819
"github.com/xelabs/go-mysqlstack/xlog"
19-
"github.com/stretchr/testify/assert"
2020
)
2121

2222
func TestStreamer(t *testing.T) {

0 commit comments

Comments
 (0)