Skip to content

Commit

Permalink
Merge branch 'release/v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
aliakseiz committed May 28, 2024
2 parents 4d1d81a + 39cf2ac commit 75bc230
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 134 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Security

## [1.1.0] - 2024-05-28

### Added
- Support of VIEW tables

### Changed
- Update `go-sqlmock` from `v1.5.0` to `v1.5.2`
- Update `testify` from `v1.6.1` to `v1.9.0`

## [1.0.2] - 2022-07-04

### Fixed
Expand All @@ -32,4 +41,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
Initial release based on [JamesStewy/go-mysqldump](https://github.com/JamesStewy/go-mysqldump) including [PR#14](https://github.com/JamesStewy/go-mysqldump/pull/14)

### Added
- Database name to drop and recreate in a dump
- Database name to drop and recreate in a dump
92 changes: 46 additions & 46 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
/*
Create MYSQL dumps in Go without the 'mysqldump' CLI as a dependency.
Example
# Example
This example uses the mysql driver (https://github.com/go-sql-driver/mysql) to connect to a mysql instance.
package main
import (
"database/sql"
"fmt"
"github.com/JamesStewy/go-mysqldump"
"github.com/go-sql-driver/mysql"
)
func main() {
config := mysql.NewConfig()
config.User = "your-user"
config.Passwd = "your-pw"
config.DBName = "your-db"
config.Net = "tcp"
config.Addr = "your-hostname:your-port"
dumpDir := "dumps" // you should create this directory
dumpFilenameFormat := fmt.Sprintf("%s-20060102T150405", dbname)
db, err := sql.Open("mysql", config.FormatDNS())
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, connected database and file stream.
dumper.Close()
}
package main
import (
"database/sql"
"fmt"
"github.com/JamesStewy/go-mysqldump"
"github.com/go-sql-driver/mysql"
)
func main() {
config := mysql.NewConfig()
config.User = "your-user"
config.Passwd = "your-pw"
config.DBName = "your-db"
config.Net = "tcp"
config.Addr = "your-hostname:your-port"
dumpDir := "dumps" // you should create this directory
dumpFilenameFormat := fmt.Sprintf("%s-20060102T150405", dbname)
db, err := sql.Open("mysql", config.FormatDNS())
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, connected database and file stream.
dumper.Close()
}
*/
package mysqldump
59 changes: 48 additions & 11 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ type recreate struct {

// Table struct contains variables for table template.
type Table struct {
Name string
Err error
Name string
Err error
IsView bool

data *Data
rows *sql.Rows
Expand Down Expand Up @@ -94,18 +95,22 @@ const footerTmpl = `/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
-- Dump completed on {{ .CompleteTime }}
`

// Takes a *Table.
const tableTmpl = `
--
-- Table structure for table {{ .NameEsc }}
--
{{ if .IsView }}
DROP VIEW IF EXISTS {{ .NameEsc }};
{{ else }}
DROP TABLE IF EXISTS {{ .NameEsc }};
{{ end }}
/*!40101 SET @saved_cs_client = @@character_set_client */;
SET character_set_client = utf8mb4 ;
{{ .CreateSQL }};
/*!40101 SET character_set_client = @saved_cs_client */;
{{ if not .IsView }}
--
-- Dumping data for table {{ .NameEsc }}
--
Expand All @@ -117,6 +122,7 @@ LOCK TABLES {{ .NameEsc }} WRITE;
{{ end -}}
/*!40000 ALTER TABLE {{ .NameEsc }} ENABLE KEYS */;
UNLOCK TABLES;
{{ end }}
`

// Takes a *recreate.
Expand Down Expand Up @@ -196,7 +202,7 @@ func (data *Data) Dump() error {
}

for _, name := range tables {
if err := data.dumpTable(name); err != nil {
if err = data.dumpTable(name); err != nil {
return err
}
}
Expand Down Expand Up @@ -232,7 +238,10 @@ func (data *Data) dumpTable(name string) error {
return data.err
}

table := data.CreateTable(name)
table, err := data.CreateTable(name)
if err != nil {
return err
}

return data.WriteTable(table)
}
Expand Down Expand Up @@ -317,28 +326,51 @@ func (meta *MetaData) UpdateServerVersion(data *Data) (err error) {
}

// CreateTable initializes a Table struct.
func (data *Data) CreateTable(name string) *Table {
return &Table{
func (data *Data) CreateTable(name string) (*Table, error) {
table := &Table{
Name: name,
data: data,
}

var tableType string

err := data.tx.QueryRow("SELECT table_type FROM information_schema.tables WHERE table_name = ?", name).Scan(&tableType)
if err != nil {
return nil, err
}

if tableType == "VIEW" {
table.IsView = true
}

return table, nil
}

func (table *Table) NameEsc() string {
return "`" + table.Name + "`"
}

func (table *Table) CreateSQL() (string, error) {
var tableReturn, tableSQL sql.NullString
if err := table.data.tx.QueryRow("SHOW CREATE TABLE "+table.NameEsc()).Scan(&tableReturn, &tableSQL); err != nil {
var (
nameReturn, createSQL sql.NullString
createSQLQuery string
)

if table.IsView {
createSQLQuery = "SHOW CREATE VIEW " + table.NameEsc()
} else {
createSQLQuery = "SHOW CREATE TABLE " + table.NameEsc()
}

if err := table.data.tx.QueryRow(createSQLQuery).Scan(&nameReturn, &createSQL); err != nil {
return "", err
}

if tableReturn.String != table.Name {
if nameReturn.String != table.Name {
return "", errUnexpectedTable
}

return tableSQL.String, nil
return createSQL.String, nil
}

func (table *Table) Init() (err error) {
Expand Down Expand Up @@ -501,3 +533,8 @@ func (table *Table) Stream() <-chan string {

return valueOut
}

// SetData assigns the data pointer to the table.
func (table *Table) SetData(data *Data) {
table.data = data
}
Loading

0 comments on commit 75bc230

Please sign in to comment.