Skip to content

Commit 47914c6

Browse files
committed
Go concurrency and backticks
1 parent 6112651 commit 47914c6

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

dump.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path"
99
"strings"
10+
"sync"
1011
"text/template"
1112
"time"
1213
)
@@ -21,11 +22,14 @@ type dump struct {
2122
DumpVersion string
2223
ServerVersion string
2324
CompleteTime string
24-
HeaderTmpl *template.Template
25-
TableTmpl *template.Template
26-
FooterTmpl *template.Template
27-
Connection *sql.DB
2825
Out io.Writer
26+
Connection *sql.DB
27+
28+
headerTmpl *template.Template
29+
tableTmpl *template.Template
30+
footerTmpl *template.Template
31+
mux sync.Mutex
32+
wg sync.WaitGroup
2933
}
3034

3135
const version = "0.3.1"
@@ -38,7 +42,7 @@ const headerTmpl = `-- Go SQL Dump {{ .DumpVersion }}
3842
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
3943
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
4044
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
41-
SET NAMES utf8mb4;
45+
SET NAMES utf8mb4 ;
4246
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
4347
/*!40103 SET TIME_ZONE='+00:00' */;
4448
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
@@ -52,9 +56,9 @@ const tableTmpl = `
5256
-- Table structure for table {{ .Name }}
5357
--
5458
55-
DROP TABLE IF EXISTS ` + "`{{ .Name }}`" + `;
59+
DROP TABLE IF EXISTS {{ .Name }};
5660
/*!40101 SET @saved_cs_client = @@character_set_client */;
57-
SET character_set_client = utf8mb4;
61+
SET character_set_client = utf8mb4 ;
5862
{{ .SQL }};
5963
/*!40101 SET character_set_client = @saved_cs_client */;
6064
@@ -72,30 +76,40 @@ UNLOCK TABLES;
7276
`
7377

7478
const footerTmpl = `
79+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
80+
81+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
82+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
83+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
84+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
85+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
86+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
87+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
88+
7589
-- Dump completed on {{ .CompleteTime }}
7690
`
7791

7892
func (data *dump) getTemplates() (err error) {
7993
// Write dump to file
80-
data.HeaderTmpl, err = template.New("mysqldumpHeader").Parse(headerTmpl)
94+
data.headerTmpl, err = template.New("mysqldumpHeader").Parse(headerTmpl)
8195
if err != nil {
8296
return
8397
}
8498

85-
data.TableTmpl, err = template.New("mysqldumpTable").Parse(tableTmpl)
99+
data.tableTmpl, err = template.New("mysqldumpTable").Parse(tableTmpl)
86100
if err != nil {
87101
return
88102
}
89103

90-
data.FooterTmpl, err = template.New("mysqldumpTable").Parse(footerTmpl)
104+
data.footerTmpl, err = template.New("mysqldumpTable").Parse(footerTmpl)
91105
if err != nil {
92106
return
93107
}
94108
return
95109
}
96110

97111
func (data *dump) dump() error {
98-
if err := data.HeaderTmpl.Execute(data.Out, data); err != nil {
112+
if err := data.headerTmpl.Execute(data.Out, data); err != nil {
99113
return err
100114
}
101115

@@ -106,20 +120,17 @@ func (data *dump) dump() error {
106120
}
107121

108122
// Get sql for each table
123+
data.wg.Add(len(tables))
109124
for _, name := range tables {
110125
if err := data.dumpTable(name); err != nil {
111126
return err
112127
}
113128
}
129+
data.wg.Wait()
114130

115131
// Set complete time
116132
data.CompleteTime = time.Now().String()
117-
118-
if err = data.FooterTmpl.Execute(data.Out, data); err != nil {
119-
return err
120-
}
121-
122-
return nil
133+
return data.footerTmpl.Execute(data.Out, data)
123134
}
124135

125136
func (data *dump) dumpTable(name string) error {
@@ -128,13 +139,18 @@ func (data *dump) dumpTable(name string) error {
128139
return err
129140
}
130141

131-
if err = data.TableTmpl.Execute(data.Out, table); err != nil {
132-
return err
133-
}
134-
142+
go data.writeTable(table)
135143
return nil
136144
}
137145

146+
func (data *dump) writeTable(table *table) error {
147+
data.mux.Lock()
148+
err := data.tableTmpl.Execute(data.Out, table)
149+
data.mux.Unlock()
150+
data.wg.Done()
151+
return err
152+
}
153+
138154
// Dump creates a MySQL dump based on the options supplied through the dumper.
139155
func (d *Dumper) Dump() (string, error) {
140156
name := time.Now().Format(d.format)
@@ -209,7 +225,7 @@ func getServerVersion(db *sql.DB) (string, error) {
209225

210226
func createTable(db *sql.DB, name string) (*table, error) {
211227
var err error
212-
t := &table{Name: name}
228+
t := &table{Name: "`" + name + "`"}
213229

214230
if t.SQL, err = createTableSQL(db, name); err != nil {
215231
return nil, err

dump_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func TestDumpOk(t *testing.T) {
298298
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
299299
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
300300
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
301-
SET NAMES utf8mb4;
301+
SET NAMES utf8mb4 ;
302302
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
303303
/*!40103 SET TIME_ZONE='+00:00' */;
304304
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
@@ -307,25 +307,35 @@ func TestDumpOk(t *testing.T) {
307307
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
308308
309309
--
310-
-- Table structure for table Test_Table
310+
-- Table structure for table \Test_Table\
311311
--
312312
313313
DROP TABLE IF EXISTS \Test_Table\;
314314
/*!40101 SET @saved_cs_client = @@character_set_client */;
315-
SET character_set_client = utf8mb4;
315+
SET character_set_client = utf8mb4 ;
316316
CREATE TABLE 'Test_Table' (\id\ int(11) NOT NULL AUTO_INCREMENT,\email\ char(60) DEFAULT NULL, \name\ char(60), PRIMARY KEY (\id\))ENGINE=InnoDB DEFAULT CHARSET=latin1;
317317
/*!40101 SET character_set_client = @saved_cs_client */;
318318
319319
--
320-
-- Dumping data for table Test_Table
320+
-- Dumping data for table \Test_Table\
321321
--
322322
323-
LOCK TABLES Test_Table WRITE;
324-
/*!40000 ALTER TABLE Test_Table DISABLE KEYS */;
325-
INSERT INTO Test_Table VALUES ('1',null,'Test Name 1'),('2','[email protected]','Test Name 2');
326-
/*!40000 ALTER TABLE Test_Table ENABLE KEYS */;
323+
LOCK TABLES \Test_Table\ WRITE;
324+
/*!40000 ALTER TABLE \Test_Table\ DISABLE KEYS */;
325+
INSERT INTO \Test_Table\ VALUES ('1',null,'Test Name 1'),('2','[email protected]','Test Name 2');
326+
/*!40000 ALTER TABLE \Test_Table\ ENABLE KEYS */;
327327
UNLOCK TABLES;
328328
329+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
330+
331+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
332+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
333+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
334+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
335+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
336+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
337+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
338+
329339
`
330340

331341
if !reflect.DeepEqual(result, expected) {

0 commit comments

Comments
 (0)