Skip to content

Commit

Permalink
Change compress to no-compress & update how gz file is read (#50)
Browse files Browse the repository at this point in the history
* Change compress to no-compress & update how gz file is read

* Tick app version patch

Co-authored-by: Joseph Klemen <[email protected]>
  • Loading branch information
NcUltimate and jklemen authored Mar 13, 2020
1 parent 70ac6aa commit 598782f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {

app.Name = "pgmgr"
app.Usage = "manage your app's Postgres database"
app.Version = "1.1.0"
app.Version = "1.1.1"

var s []string

Expand Down Expand Up @@ -121,10 +121,10 @@ func main() {
Usage: "how to apply the migrations. supported options are pq (which will execute the migration as one statement) or psql (which will use the psql binary on your system to execute each line)",
EnvVar: "PGMGR_MIGRATION_DRIVER",
},
cli.BoolTFlag{
Name: "compress",
Usage: "whether to compress the database dump (t/f). If t compression is set to 9. See pg_dump -Z.",
EnvVar: "PGMGR_COMPRESS",
cli.BoolFlag{
Name: "no-compress",
Usage: "whether to skip compressing the database dump. See pg_dump -Z.",
EnvVar: "PGMGR_NO_COMPRESS",
},
cli.BoolFlag{
Name: "include-triggers",
Expand Down
22 changes: 12 additions & 10 deletions pgmgr/dump_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type DumpConfig struct {
IncludeTriggers bool `json:"include-triggers"`

// options
Compress bool `json:"compress"`
DumpFile string `json:"dump-file"`
NoCompress bool `json:"no-compress"`
DumpFile string `json:"dump-file"`
}

// GetDumpFileRaw returns the literal dump file name as configured
Expand All @@ -26,12 +26,17 @@ func (config DumpConfig) GetDumpFileRaw() string {
// GetDumpFile returns the true dump file name
// with or without the specified compression suffix
func (config DumpConfig) GetDumpFile() string {
if config.Compress {
if config.IsCompressed() {
return config.DumpFile + ".gz"
}
return config.DumpFile
}

// IsCompressed returns whether compression is set
func (config DumpConfig) IsCompressed() bool {
return !config.NoCompress
}

func (config *DumpConfig) applyArguments(ctx argumentContext) {
if sliceValuesGiven(ctx, "exclude-schemas") {
config.ExcludeSchemas = ctx.StringSlice("exclude-schemas")
Expand All @@ -42,8 +47,8 @@ func (config *DumpConfig) applyArguments(ctx argumentContext) {
if ctx.String("dump-file") != "" {
config.DumpFile = ctx.String("dump-file")
}
if !ctx.Bool("compress") {
config.Compress = false
if ctx.Bool("no-compress") {
config.NoCompress = true
}
if ctx.Bool("include-privileges") {
config.IncludePrivileges = true
Expand All @@ -53,17 +58,14 @@ func (config *DumpConfig) applyArguments(ctx argumentContext) {
}
if strings.HasSuffix(config.DumpFile, ".gz") {
config.DumpFile = config.DumpFile[0 : len(config.DumpFile)-3]
config.Compress = true
config.NoCompress = false
}
}

func (config *DumpConfig) applyDefaults() {
if config.DumpFile == "" {
config.DumpFile = "dump.sql"
}
config.IncludePrivileges = false
config.IncludeTriggers = false
config.Compress = true
}

func sliceValuesGiven(ctx argumentContext, key string) bool {
Expand All @@ -76,7 +78,7 @@ func (config DumpConfig) baseFlags() []string {
args = append(args, "-N", schema)
}

if config.Compress {
if config.IsCompressed() {
args = append(args, "-Z", "9")
}

Expand Down
15 changes: 7 additions & 8 deletions pgmgr/dump_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ func TestDumpFlags(test *testing.T) {
c := DumpConfig{
IncludeTables: []string{"iTable1", "iTable2"},
ExcludeSchemas: []string{},
Compress: true,
}

flags := strings.Join(c.baseFlags(), " ")
Expand All @@ -19,17 +18,17 @@ func TestDumpFlags(test *testing.T) {
}
}
if !strings.Contains(flags, "-Z 9") {
test.Fatal("Dump flags should set compression level to 9 when compressed is 't'")
test.Fatal("Dump flags should set compression level to 9 when NoCompress is 'f'")
}
if !strings.Contains(flags, "-x") {
test.Fatal("Dump flags should set -x when IncludePrivileges is 'f'")
}

c.Compress = false
c.NoCompress = true
c.IncludePrivileges = true
flags = strings.Join(c.baseFlags(), " ")
if strings.Contains(flags, "-Z 9") {
test.Fatal("Dump flags should not set compression level to 9 when compressed is 'f'")
test.Fatal("Dump flags should not set compression level to 9 when NoCompress is 't'")
}

if strings.Contains(flags, "-x") {
Expand Down Expand Up @@ -69,8 +68,8 @@ func TestDumpDefaults(t *testing.T) {
t.Fatal("dump config's dump-file should default to 'dump.sql', but was ", c.DumpConfig.DumpFile)
}

if !c.DumpConfig.Compress {
t.Fatal("dump config's compression should default to 't', but was ", c.DumpConfig.Compress)
if c.DumpConfig.NoCompress {
t.Fatal("dump config's NoCompress should default to 'f', but was ", c.DumpConfig.NoCompress)
}

if c.DumpConfig.IncludePrivileges {
Expand All @@ -91,8 +90,8 @@ func TestDumpDefaults(t *testing.T) {
if c.DumpConfig.DumpFile != "dump.file.sql" {
t.Fatal("dump config should strip '.gz' suffix, but was ", c.DumpConfig.DumpFile)
}
if !c.DumpConfig.Compress {
t.Fatal("dump config should set Compress='t' if '.gz' suffix is present, but was ", c.DumpConfig.Compress)
if c.DumpConfig.NoCompress {
t.Fatal("dump config should set NoCompress='f' if '.gz' suffix is present, but was ", c.DumpConfig.NoCompress)
}
}

Expand Down
16 changes: 14 additions & 2 deletions pgmgr/pgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,21 @@ func Load(c *Config) error {
return err
}

if c.DumpConfig.Compress {
sh("gunzip", []string{"-c", c.DumpConfig.GetDumpFile(), ">", c.DumpConfig.GetDumpFileRaw()})
if c.DumpConfig.IsCompressed() {
dumpSQL, err := shRead("gunzip", []string{"-c", c.DumpConfig.GetDumpFile()})
if err != nil {
return err
}

defer func() { sh("rm", []string{"-f", c.DumpConfig.GetDumpFileRaw()}) }()

file, err := os.OpenFile(c.DumpConfig.GetDumpFileRaw(), os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
if err != nil {
return err
}

file.Write(*dumpSQL)
file.Close()
}

return sh("psql", []string{"-d", c.Database, "-f", c.DumpConfig.GetDumpFileRaw()})
Expand Down
2 changes: 1 addition & 1 deletion pgmgr/pgmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func globalConfig() *Config {
MigrationFolder: migrationFolder,
MigrationTable: "schema_migrations",
SslMode: "disable",
DumpConfig: DumpConfig{DumpFile: dumpFile, Compress: false},
DumpConfig: DumpConfig{DumpFile: dumpFile, NoCompress: true},
}
}

Expand Down

0 comments on commit 598782f

Please sign in to comment.