Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option to not include the dump date in the output file #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea/*
vendor/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
}

// Register database with mysqldump
dumper, err := mysqldump.Register(db, dumpDir, dumpFilenameFormat)
dumper, err := mysqldump.Register(db, dumpDir, dumpFilenameFormat, true)
if err != nil {
fmt.Println("Error registering databse:", err)
return
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This example uses the mymysql driver (example 7 https://github.com/ziutek/mymysq
}

// Register database with mysqldump
dumper, err := mysqldump.Register(db, "dumps", time.ANSIC)
dumper, err := mysqldump.Register(db, "dumps", time.ANSIC, true)
if err != nil {
fmt.Println("Error registering databse:", err)
return
Expand Down
9 changes: 6 additions & 3 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type dump struct {
CompleteTime string
}

const version = "0.2.2"
const version = "0.3.0"

const tmpl = `-- Go SQL Dump {{ .DumpVersion }}
--
Expand Down Expand Up @@ -64,8 +64,9 @@ INSERT INTO {{ .Name }} VALUES {{ .Values }};
/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
UNLOCK TABLES;
{{ end }}
{{ if .CompleteTime }}
-- Dump completed on {{ .CompleteTime }}
`
{{ end }}`

// Creates a MYSQL Dump based on the options supplied through the dumper.
func (d *Dumper) Dump() (string, error) {
Expand Down Expand Up @@ -112,7 +113,9 @@ func (d *Dumper) Dump() (string, error) {
}

// Set complete time
data.CompleteTime = time.Now().String()
if d.includeDumpDate {
data.CompleteTime = time.Now().String()
}

// Write dump to file
t, err := template.New("mysqldump").Parse(tmpl)
Expand Down
9 changes: 5 additions & 4 deletions dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,10 @@ func TestDumpOk(t *testing.T) {
mock.ExpectQuery("^SELECT (.+) FROM Test_Table$").WillReturnRows(createTableValueRows)

dumper := &Dumper{
db: db,
format: "test_format",
dir: "/tmp/",
db: db,
format: "test_format",
dir: "/tmp/",
includeDumpDate: false,
}

path, err := dumper.Dump()
Expand All @@ -288,7 +289,7 @@ func TestDumpOk(t *testing.T) {
t.Errorf("error was not expected while reading the file: %s", err)
}

result := strings.Replace(strings.Split(string(f), "-- Dump completed")[0], "`", "\\", -1)
result := strings.Replace(string(f), "`", "\\", -1)

expected := `-- Go SQL Dump ` + version + `
--
Expand Down
17 changes: 10 additions & 7 deletions mysqldump.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

// Dumper represents a database.
type Dumper struct {
db *sql.DB
format string
dir string
db *sql.DB
format string
dir string
includeDumpDate bool
}

/*
Expand All @@ -19,16 +20,18 @@ Creates a new dumper.
db: Database that will be dumped (https://golang.org/pkg/database/sql/#DB).
dir: Path to the directory where the dumps will be stored.
format: Format to be used to name each dump file. Uses time.Time.Format (https://golang.org/pkg/time/#Time.Format). format appended with '.sql'.
includeDumpDate: Whether the dump date should be included in the output.
*/
func Register(db *sql.DB, dir, format string) (*Dumper, error) {
func Register(db *sql.DB, dir, format string, includeDumpDate bool) (*Dumper, error) {
if !isDir(dir) {
return nil, errors.New("Invalid directory")
}

return &Dumper{
db: db,
format: format,
dir: dir,
db: db,
format: format,
dir: dir,
includeDumpDate: includeDumpDate,
}, nil
}

Expand Down