-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from kesuskim/master
Add MySQL variable assignments - Fixed #5
- Loading branch information
Showing
3 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
@@ -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; | ||
` | ||
|