Skip to content

Commit

Permalink
feat: add zstd support for mysqldump
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Sep 29, 2023
1 parent b2ac0c4 commit a254e8f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dist/
/*.zip
/FroshTools
/result
dump.sql*

# Devenv
.devenv*
Expand Down
20 changes: 20 additions & 0 deletions cmd/project/project_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package project
import (
"compress/gzip"
"database/sql"
"fmt"
"io"
"os"
"strings"

"github.com/doutorfinancas/go-mad/core"
"github.com/doutorfinancas/go-mad/database"
"github.com/doutorfinancas/go-mad/generator"
"github.com/klauspost/compress/zstd"
"github.com/spf13/cobra"
"go.uber.org/zap"

Expand All @@ -31,6 +33,11 @@ var projectDatabaseDumpCmd = &cobra.Command{
skipLockTables, _ := cmd.Flags().GetBool("skip-lock-tables")
anonymize, _ := cmd.Flags().GetBool("anonymize")
gzipEnabled, _ := cmd.Flags().GetBool("gzip")
zstdEnabled, _ := cmd.Flags().GetBool("zstd")

if gzipEnabled && zstdEnabled {
return fmt.Errorf("only one compression method can be used at same time")
}

cfg := database.NewConfig(username, password, host, port, args[0])

Expand Down Expand Up @@ -156,6 +163,10 @@ var projectDatabaseDumpCmd = &cobra.Command{
output += ".gz"
}

if zstdEnabled {
output += ".zstd"
}

var w io.Writer
if w, err = os.Create(output); err != nil {
return err
Expand All @@ -165,6 +176,14 @@ var projectDatabaseDumpCmd = &cobra.Command{
w = gzip.NewWriter(w)
}

if zstdEnabled {
w, err = zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.SpeedBestCompression))

if err != nil {
return err
}
}

if err = dumper.Dump(w); err != nil {
return err
}
Expand Down Expand Up @@ -192,4 +211,5 @@ func init() {
projectDatabaseDumpCmd.Flags().Bool("skip-lock-tables", false, "Skips locking the tables")
projectDatabaseDumpCmd.Flags().Bool("anonymize", false, "Anonymize customer data")
projectDatabaseDumpCmd.Flags().Bool("gzip", false, "Gzip the whole dump")
projectDatabaseDumpCmd.Flags().Bool("zstd", false, "Zstd the whole dump")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/gorilla/css v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jaswdr/faker v1.19.0 // indirect
github.com/klauspost/compress v1.17.0
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jaswdr/faker v1.19.0 h1:QUF2faV6vpxQOmrxG+DCPy/fxTomu74jmMNeVbc1LhM=
github.com/jaswdr/faker v1.19.0/go.mod h1:x7ZlyB1AZqwqKZgyQlnqEG8FDptmHlncA5u2zY/yi6w=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down

0 comments on commit a254e8f

Please sign in to comment.