Skip to content

Commit cceeb3e

Browse files
committed
Implement custom mysql options
Implement clitools feature “Add --defaults-file option to mysql/mysqldump“ webdevops/clitools#116
1 parent 20d8779 commit cceeb3e

File tree

5 files changed

+77
-35
lines changed

5 files changed

+77
-35
lines changed

examples/gosync.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,19 @@ sync:
6464
password: dev
6565
port: 13306
6666
hostname: mysql.example.com
67+
options:
68+
# custom mysqldump options (for local)
69+
mysqldump:
70+
# custom mysql options (for local)
71+
mysql:
6772
options:
6873
## clear database before restore
6974
## (delete all tables inside)
7075
clear-database: true
76+
# custom mysqldump options (for remote)
77+
mysqldump:
78+
# custom mysql options (for remote)
79+
mysql:
7180

7281
## DOCKER
7382
## Sync schema typo3 to local test-local into a docker container

sync/config.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ type Filesystem struct {
2525
}
2626
}
2727

28+
type DatabaseOptions struct {
29+
ClearDatabase bool `yaml:"clear-database"`
30+
Mysqldump string
31+
Mysql string
32+
}
2833

2934
type Database struct {
3035
Type string
@@ -35,6 +40,8 @@ type Database struct {
3540
Password string
3641

3742
Filter Filter
43+
Connection commandbuilder.Connection
44+
3845
Local struct {
3946
Type string
4047
Schema string
@@ -44,16 +51,13 @@ type Database struct {
4451
Password string
4552

4653
Connection commandbuilder.Connection
54+
Options DatabaseOptions
4755
}
48-
Options struct {
49-
ClearDatabase bool `yaml:"clear-database"`
50-
}
56+
Options DatabaseOptions
5157

5258
// local cache
5359
cacheRemoteTableList []string
5460
cacheLocalTableList []string
55-
56-
Connection commandbuilder.Connection
5761
}
5862

5963
type Execution struct {

sync/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (database *Database) mysqlTableFilter(connection *commandbuilder.Connection
4040
// calc excludes
4141
excludeTableList := database.Filter.CalcExcludes(tableList)
4242
for _, table := range excludeTableList {
43-
exclude = append(exclude, fmt.Sprintf("--ignore-table=%s.%s", database.Schema, table))
43+
exclude = append(exclude, shell.Quote(fmt.Sprintf("--ignore-table=%s.%s", database.Schema, table)))
4444
}
4545

4646
// calc includes

sync/database_local.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package sync
22

3+
import "github.com/webdevops/go-shell"
4+
35
func (database *Database) localMysqldumpCmdBuilder(additionalArgs []string, useFilter bool) []interface{} {
46
var args []string
57

8+
69
if database.Local.User != "" {
7-
args = append(args, "-u" + database.Local.User)
10+
args = append(args, shell.Quote("-u" + database.Local.User))
811
}
912

1013
if database.Local.Password != "" {
11-
args = append(args, "-p" + database.Local.Password)
14+
args = append(args, shell.Quote("-p" + database.Local.Password))
1215
}
1316

1417
if database.Local.Hostname != "" {
15-
args = append(args, "-h" + database.Local.Hostname)
18+
args = append(args, shell.Quote("-h" + database.Local.Hostname))
1619
}
1720

1821
if database.Local.Port != "" {
19-
args = append(args, "-P" + database.Local.Port)
22+
args = append(args, shell.Quote("-P" + database.Local.Port))
2023
}
2124

2225
if len(args) > 0 {
@@ -29,38 +32,48 @@ func (database *Database) localMysqldumpCmdBuilder(additionalArgs []string, useF
2932
args = append(args, excludeArgs...)
3033
}
3134

35+
// add custom options (raw)
36+
if database.Local.Options.Mysqldump != "" {
37+
args = append(args, database.Local.Options.Mysqldump)
38+
}
39+
3240
// schema
33-
args = append(args, database.Local.Schema)
41+
args = append(args, shell.Quote(database.Local.Schema))
3442

3543
// include
3644
if useFilter && len(includeArgs) > 0 {
3745
args = append(args, includeArgs...)
3846
}
3947

40-
return database.Local.Connection.CommandBuilder("mysqldump", args...)
48+
return database.Local.Connection.RawCommandBuilder("mysqldump", args...)
4149
}
4250

4351
func (database *Database) localMysqlCmdBuilder(args ...string) []interface{} {
4452
args = append(args, "-BN")
4553

4654
if database.Local.User != "" {
47-
args = append(args, "-u" + database.Local.User)
55+
args = append(args, shell.Quote("-u" + database.Local.User))
4856
}
4957

5058
if database.Local.Password != "" {
51-
args = append(args, "-p" + database.Local.Password)
59+
args = append(args, shell.Quote("-p" + database.Local.Password))
5260
}
5361

5462
if database.Local.Hostname != "" {
55-
args = append(args, "-h" + database.Local.Hostname)
63+
args = append(args, shell.Quote("-h" + database.Local.Hostname))
5664
}
5765

5866
if database.Local.Port != "" {
59-
args = append(args, "-P" + database.Local.Port)
67+
args = append(args, shell.Quote("-P" + database.Local.Port))
68+
}
69+
70+
// add custom options (raw)
71+
if database.Local.Options.Mysql != "" {
72+
args = append(args, database.Local.Options.Mysql)
6073
}
6174

6275
if database.Local.Schema != "" {
63-
args = append(args, database.Local.Schema)
76+
args = append(args, shell.Quote(database.Local.Schema))
6477
}
6578

6679
return database.Local.Connection.CommandBuilder("mysql", args...)

sync/database_remote.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ func (database *Database) remoteMysqldumpCmdBuilder(additionalArgs []string, use
99
var args []string
1010

1111
if database.User != "" {
12-
args = append(args, "-u" + database.User)
12+
args = append(args, shell.Quote("-u" + database.User))
1313
}
1414

1515
if database.Password != "" {
16-
args = append(args, "-p" + database.Password)
16+
args = append(args, shell.Quote("-p" + database.Password))
1717
}
1818

1919
if database.Hostname != "" {
20-
args = append(args, "-h" + database.Hostname)
20+
args = append(args, shell.Quote("-h" + database.Hostname))
2121
}
2222

2323
if database.Port != "" {
24-
args = append(args, "-P" + database.Port)
24+
args = append(args, shell.Quote("-P" + database.Port))
2525
}
2626

2727
if len(args) > 0 {
@@ -35,15 +35,21 @@ func (database *Database) remoteMysqldumpCmdBuilder(additionalArgs []string, use
3535
}
3636

3737
// schema
38-
args = append(args, database.Schema)
38+
args = append(args, shell.Quote(database.Schema))
3939

4040
// include
4141
if useFilter && len(includeArgs) > 0 {
4242
args = append(args, includeArgs...)
4343
}
4444

4545
cmd := []string{"mysqldump"}
46-
cmd = append(cmd, shell.QuoteValues(args...)...)
46+
47+
// add custom options (raw)
48+
if database.Options.Mysqldump != "" {
49+
cmd = append(cmd, database.Options.Mysqldump)
50+
}
51+
52+
cmd = append(cmd, args...)
4753
cmd = append(cmd, "|", "gzip", "--stdout")
4854

4955
return database.Connection.RawShellCommandBuilder(cmd...)
@@ -53,53 +59,63 @@ func (database *Database) remoteMysqlCmdBuilder(args ...string) []interface{} {
5359
args = append(args, "-BN")
5460

5561
if database.User != "" {
56-
args = append(args, "-u" + database.User)
62+
args = append(args, shell.Quote("-u" + database.User))
5763
}
5864

5965
if database.Password != "" {
60-
args = append(args, "-p" + database.Password)
66+
args = append(args, shell.Quote("-p" + database.Password))
6167
}
6268

6369
if database.Hostname != "" {
64-
args = append(args, "-h" + database.Hostname)
70+
args = append(args, shell.Quote("-h" + database.Hostname))
6571
}
6672

6773
if database.Port != "" {
68-
args = append(args, "-P" + database.Port)
74+
args = append(args, shell.Quote("-P" + database.Port))
6975
}
7076

7177
if database.Schema != "" {
72-
args = append(args, database.Schema)
78+
args = append(args, shell.Quote(database.Schema))
7379
}
7480

75-
return database.Connection.CommandBuilder("mysql", args...)
81+
// append options in raw
82+
if database.Options.Mysqldump != "" {
83+
args = append(args, database.Options.Mysql)
84+
}
85+
86+
return database.Connection.RawCommandBuilder("mysql", args...)
7687
}
7788

7889

7990
func (database *Database) remoteMysqlCmdBuilderUncompress(args ...string) []interface{} {
8091
args = append(args, "-BN")
8192

8293
if database.User != "" {
83-
args = append(args, "-u" + database.User)
94+
args = append(args, shell.Quote("-u" + database.User))
8495
}
8596

8697
if database.Password != "" {
87-
args = append(args, "-p" + database.Password)
98+
args = append(args, shell.Quote("-p" + database.Password))
8899
}
89100

90101
if database.Hostname != "" {
91-
args = append(args, "-h" + database.Hostname)
102+
args = append(args, shell.Quote("-h" + database.Hostname))
92103
}
93104

94105
if database.Port != "" {
95-
args = append(args, "-P" + database.Port)
106+
args = append(args, shell.Quote("-P" + database.Port))
107+
}
108+
109+
// add custom options (raw)
110+
if database.Options.Mysqldump != "" {
111+
args = append(args, database.Options.Mysql)
96112
}
97113

98114
if database.Schema != "" {
99-
args = append(args, database.Schema)
115+
args = append(args, shell.Quote(database.Schema))
100116
}
101117

102-
cmd := []string{"gunzip", "--stdout", "|", "mysql", strings.Join(shell.QuoteValues(args...), " ")}
118+
cmd := []string{"gunzip", "--stdout", "|", "mysql", strings.Join(args, " ")}
103119

104120
return database.Connection.RawShellCommandBuilder(cmd...)
105121
}

0 commit comments

Comments
 (0)