Skip to content

Commit 143398c

Browse files
committed
Add MySQL variable assignments
1 parent 5a2120b commit 143398c

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
# Go MYSQL Dump
22
Create MYSQL dumps in Go without the `mysqldump` CLI as a dependancy.
33

4+
### Simple Example
5+
```go
6+
package main
7+
8+
import (
9+
"database/sql"
10+
"fmt"
11+
12+
"github.com/JamesStewy/go-mysqldump"
13+
_ "github.com/go-sql-driver/mysql"
14+
)
15+
16+
func main() {
17+
// Open connection to database
18+
username := "your-user"
19+
password := "your-pw"
20+
hostname := "your-hostname"
21+
port := "your-port"
22+
dbname := "your-db"
23+
24+
dumpDir := "dumps" // you should create this directory
25+
dumpFilenameFormat := fmt.Sprintf("%s-20060102T150405", dbname) // accepts time layout string and add .sql at the end of file
26+
27+
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", username, password, hostname, port, dbname))
28+
if err != nil {
29+
fmt.Println("Error opening database: ", err)
30+
return
31+
}
32+
33+
// Register database with mysqldump
34+
dumper, err := mysqldump.Register(db, dumpDir, dumpFilenameFormat)
35+
if err != nil {
36+
fmt.Println("Error registering databse:", err)
37+
return
38+
}
39+
40+
// Dump database to file
41+
resultFilename, err := dumper.Dump()
42+
if err != nil {
43+
fmt.Println("Error dumping:", err)
44+
return
45+
}
46+
fmt.Printf("File is saved to %s", resultFilename)
47+
48+
// Close dumper and connected database
49+
dumper.Close()
50+
}
51+
52+
```
53+
454
[![GoDoc](https://godoc.org/github.com/JamesStewy/go-mysqldump?status.svg)](https://godoc.org/github.com/JamesStewy/go-mysqldump)
555
[![Build Status](https://travis-ci.org/JamesStewy/go-mysqldump.svg?branch=master)](https://travis-ci.org/JamesStewy/go-mysqldump)

dump.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,47 @@ type dump struct {
2323
CompleteTime string
2424
}
2525

26-
const version = "0.2.0"
26+
const version = "0.2.1"
2727

2828
const tmpl = `-- Go SQL Dump {{ .DumpVersion }}
2929
--
3030
-- ------------------------------------------------------
3131
-- Server version {{ .ServerVersion }}
3232
33+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
34+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
35+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
36+
/*!40101 SET NAMES utf8 */;
37+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
38+
/*!40103 SET TIME_ZONE='+00:00' */;
39+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
40+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
41+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
42+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
43+
3344
3445
{{range .Tables}}
3546
--
3647
-- Table structure for table {{ .Name }}
3748
--
3849
3950
DROP TABLE IF EXISTS {{ .Name }};
51+
/*!40101 SET @saved_cs_client = @@character_set_client */;
52+
/*!40101 SET character_set_client = utf8 */;
4053
{{ .SQL }};
41-
{{ if .Values }}
54+
/*!40101 SET character_set_client = @saved_cs_client */;
4255
--
4356
-- Dumping data for table {{ .Name }}
4457
--
4558
4659
LOCK TABLES {{ .Name }} WRITE;
60+
/*!40000 ALTER TABLE {{ .Name }} DISABLE KEYS */;
61+
{{ if .Values }}
4762
INSERT INTO {{ .Name }} VALUES {{ .Values }};
63+
{{ end }}
64+
/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
4865
UNLOCK TABLES;
49-
{{end}}{{ end }}
66+
{{ end }}
5067
-- Dump completed on {{ .CompleteTime }}
5168
`
5269

dump_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package mysqldump
22

33
import (
4-
"github.com/DATA-DOG/go-sqlmock"
54
"io/ioutil"
65
"os"
76
"reflect"
87
"strings"
98
"testing"
9+
10+
sqlmock "github.com/DATA-DOG/go-sqlmock"
1011
)
1112

1213
func TestGetTablesOk(t *testing.T) {
@@ -293,21 +294,38 @@ func TestDumpOk(t *testing.T) {
293294
-- ------------------------------------------------------
294295
-- Server version test_version
295296
297+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
298+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
299+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
300+
/*!40101 SET NAMES utf8 */;
301+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
302+
/*!40103 SET TIME_ZONE='+00:00' */;
303+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
304+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
305+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
306+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
307+
296308
297309
298310
--
299311
-- Table structure for table Test_Table
300312
--
301313
302314
DROP TABLE IF EXISTS Test_Table;
315+
/*!40101 SET @saved_cs_client = @@character_set_client */;
316+
/*!40101 SET character_set_client = utf8 */;
303317
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;
304-
318+
/*!40101 SET character_set_client = @saved_cs_client */;
305319
--
306320
-- Dumping data for table Test_Table
307321
--
308322
309323
LOCK TABLES Test_Table WRITE;
324+
/*!40000 ALTER TABLE Test_Table DISABLE KEYS */;
325+
310326
INSERT INTO Test_Table VALUES ('1','','Test Name 1'),('2','[email protected]','Test Name 2');
327+
328+
/*!40000 ALTER TABLE Test_Table ENABLE KEYS */;
311329
UNLOCK TABLES;
312330
313331
`

0 commit comments

Comments
 (0)