Skip to content

Commit

Permalink
Merge pull request #6 from kesuskim/master
Browse files Browse the repository at this point in the history
Add MySQL variable assignments - Fixed #5
  • Loading branch information
JamesStewy authored Dec 28, 2017
2 parents 5a2120b + 143398c commit 417d97b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
# Go MYSQL Dump
Create MYSQL dumps in Go without the `mysqldump` CLI as a dependancy.

### Simple Example
```go
package main

import (
"database/sql"
"fmt"

"github.com/JamesStewy/go-mysqldump"
_ "github.com/go-sql-driver/mysql"
)

func main() {
// Open connection to database
username := "your-user"
password := "your-pw"
hostname := "your-hostname"
port := "your-port"
dbname := "your-db"

dumpDir := "dumps" // you should create this directory
dumpFilenameFormat := fmt.Sprintf("%s-20060102T150405", dbname) // accepts time layout string and add .sql at the end of file

db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", username, password, hostname, port, dbname))
if err != nil {
fmt.Println("Error opening database: ", err)
return
}

// Register database with mysqldump
dumper, err := mysqldump.Register(db, dumpDir, dumpFilenameFormat)
if err != nil {
fmt.Println("Error registering databse:", err)
return
}

// Dump database to file
resultFilename, err := dumper.Dump()
if err != nil {
fmt.Println("Error dumping:", err)
return
}
fmt.Printf("File is saved to %s", resultFilename)

// Close dumper and connected database
dumper.Close()
}

```

[![GoDoc](https://godoc.org/github.com/JamesStewy/go-mysqldump?status.svg)](https://godoc.org/github.com/JamesStewy/go-mysqldump)
[![Build Status](https://travis-ci.org/JamesStewy/go-mysqldump.svg?branch=master)](https://travis-ci.org/JamesStewy/go-mysqldump)
23 changes: 20 additions & 3 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,47 @@ type dump struct {
CompleteTime string
}

const version = "0.2.0"
const version = "0.2.1"

const tmpl = `-- Go SQL Dump {{ .DumpVersion }}
--
-- ------------------------------------------------------
-- Server version {{ .ServerVersion }}
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
{{range .Tables}}
--
-- Table structure for table {{ .Name }}
--
DROP TABLE IF EXISTS {{ .Name }};
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
{{ .SQL }};
{{ if .Values }}
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table {{ .Name }}
--
LOCK TABLES {{ .Name }} WRITE;
/*!40000 ALTER TABLE {{ .Name }} DISABLE KEYS */;
{{ if .Values }}
INSERT INTO {{ .Name }} VALUES {{ .Values }};
{{ end }}
/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
UNLOCK TABLES;
{{end}}{{ end }}
{{ end }}
-- Dump completed on {{ .CompleteTime }}
`

Expand Down
22 changes: 20 additions & 2 deletions dump_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package mysqldump

import (
"github.com/DATA-DOG/go-sqlmock"
"io/ioutil"
"os"
"reflect"
"strings"
"testing"

sqlmock "github.com/DATA-DOG/go-sqlmock"
)

func TestGetTablesOk(t *testing.T) {
Expand Down Expand Up @@ -293,21 +294,38 @@ func TestDumpOk(t *testing.T) {
-- ------------------------------------------------------
-- Server version test_version
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table Test_Table
--
DROP TABLE IF EXISTS Test_Table;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
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;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table Test_Table
--
LOCK TABLES Test_Table WRITE;
/*!40000 ALTER TABLE Test_Table DISABLE KEYS */;
INSERT INTO Test_Table VALUES ('1','','Test Name 1'),('2','[email protected]','Test Name 2');
/*!40000 ALTER TABLE Test_Table ENABLE KEYS */;
UNLOCK TABLES;
`
Expand Down

0 comments on commit 417d97b

Please sign in to comment.