Skip to content

Commit dabcd57

Browse files
committed
With data streams
1 parent bb5c4f9 commit dabcd57

File tree

2 files changed

+58
-47
lines changed

2 files changed

+58
-47
lines changed

dump.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type metaData struct {
4747

4848
const version = "0.3.5"
4949

50+
// takes a *metaData
5051
const headerTmpl = `-- Go SQL Dump {{ .DumpVersion }}
5152
--
5253
-- ------------------------------------------------------
@@ -64,6 +65,21 @@ const headerTmpl = `-- Go SQL Dump {{ .DumpVersion }}
6465
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
6566
`
6667

68+
// takes a *metaData
69+
const footerTmpl = `/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
70+
71+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
72+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
73+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
74+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
75+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
76+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
77+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
78+
79+
-- Dump completed on {{ .CompleteTime }}
80+
`
81+
82+
// Takes a *table
6783
const tableTmpl = `
6884
--
6985
-- Table structure for table {{ .NameEsc }}
@@ -89,19 +105,6 @@ INSERT INTO {{ .NameEsc }} VALUES {{ .RowValues }}
89105
UNLOCK TABLES;
90106
`
91107

92-
const footerTmpl = `/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
93-
94-
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
95-
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
96-
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
97-
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
98-
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
99-
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
100-
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
101-
102-
-- Dump completed on {{ .CompleteTime }}
103-
`
104-
105108
const nullType = "NULL"
106109

107110
// Dump data using struct

mysqldump_test.go

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,14 @@ package mysqldump
22

33
import (
44
"bytes"
5-
"os"
65
"strings"
76
"testing"
87

98
sqlmock "github.com/DATA-DOG/go-sqlmock"
109
"github.com/stretchr/testify/assert"
1110
)
1211

13-
func TestDumpOk(t *testing.T) {
14-
15-
tmpFile := "/tmp/test_format.sql"
16-
os.Remove(tmpFile)
17-
18-
db, mock, err := sqlmock.New()
19-
assert.NoError(t, err, "an error was not expected when opening a stub database connection")
20-
defer db.Close()
21-
22-
showTablesRows := sqlmock.NewRows([]string{"Tables_in_Testdb"}).
23-
AddRow("Test_Table")
24-
25-
serverVersionRows := sqlmock.NewRows([]string{"Version()"}).
26-
AddRow("test_version")
27-
28-
createTableRows := sqlmock.NewRows([]string{"Table", "Create Table"}).
29-
AddRow("Test_Table", "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")
30-
31-
createTableValueRows := sqlmock.NewRows([]string{"id", "email", "name"}).
32-
AddRow(1, nil, "Test Name 1").
33-
AddRow(2, "[email protected]", "Test Name 2")
34-
35-
mock.ExpectQuery("^SELECT version()").WillReturnRows(serverVersionRows)
36-
mock.ExpectQuery("^SHOW TABLES$").WillReturnRows(showTablesRows)
37-
mock.ExpectQuery("^SHOW CREATE TABLE `Test_Table`$").WillReturnRows(createTableRows)
38-
mock.ExpectQuery("^SELECT (.+) FROM `Test_Table`$").WillReturnRows(createTableValueRows)
39-
40-
buf := new(bytes.Buffer)
41-
assert.NoError(t, Dump(db, buf), "an error was not expected when dumping a stub database connection")
42-
43-
result := strings.Replace(strings.Split(buf.String(), "-- Dump completed")[0], "`", "~", -1)
44-
45-
expected := `-- Go SQL Dump ` + version + `
12+
const expected = `-- Go SQL Dump ` + version + `
4613
--
4714
-- ------------------------------------------------------
4815
-- Server version test_version
@@ -88,5 +55,46 @@ UNLOCK TABLES;
8855
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
8956
9057
`
58+
59+
func RunDump(t testing.TB) string {
60+
db, mock, err := sqlmock.New()
61+
assert.NoError(t, err, "an error was not expected when opening a stub database connection")
62+
defer db.Close()
63+
64+
showTablesRows := sqlmock.NewRows([]string{"Tables_in_Testdb"}).
65+
AddRow("Test_Table")
66+
67+
serverVersionRows := sqlmock.NewRows([]string{"Version()"}).
68+
AddRow("test_version")
69+
70+
createTableRows := sqlmock.NewRows([]string{"Table", "Create Table"}).
71+
AddRow("Test_Table", "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")
72+
73+
createTableValueRows := sqlmock.NewRows([]string{"id", "email", "name"}).
74+
AddRow(1, nil, "Test Name 1").
75+
AddRow(2, "[email protected]", "Test Name 2")
76+
77+
mock.ExpectQuery("^SELECT version()").WillReturnRows(serverVersionRows)
78+
mock.ExpectQuery("^SHOW TABLES$").WillReturnRows(showTablesRows)
79+
mock.ExpectQuery("^SHOW CREATE TABLE `Test_Table`$").WillReturnRows(createTableRows)
80+
mock.ExpectQuery("^SELECT (.+) FROM `Test_Table`$").WillReturnRows(createTableValueRows)
81+
82+
var buf bytes.Buffer
83+
assert.NoError(t, Dump(db, &buf), "an error was not expected when dumping a stub database connection")
84+
85+
return buf.String()
86+
}
87+
88+
func TestDumpOk(t *testing.T) {
89+
out := RunDump(t)
90+
91+
result := strings.Replace(strings.Split(out, "-- Dump completed")[0], "`", "~", -1)
92+
9193
assert.Equal(t, expected, result)
9294
}
95+
96+
func BenchmarkDump(b *testing.B) {
97+
for i := 0; i < b.N; i++ {
98+
_ = RunDump(b)
99+
}
100+
}

0 commit comments

Comments
 (0)