@@ -98,10 +98,11 @@ type Source struct {
9898
9999// DumpDefinition describes a database for dumping.
100100type DumpDefinition struct {
101- Tables []string `yaml:"tables"`
102- Format string `yaml:"format"`
103- Compression compressionType `yaml:"compression"`
104- dbName string
101+ Tables []string `yaml:"tables"`
102+ ExcludeTables []string `yaml:"excludeTables"`
103+ Format string `yaml:"format"`
104+ Compression compressionType `yaml:"compression"`
105+ dbName string
105106}
106107
107108type dumpJobConfig struct {
@@ -435,13 +436,23 @@ func (d *DumpJob) cleanupDumpLocation(ctx context.Context, dumpContID string, db
435436}
436437
437438func (d * DumpJob ) dumpDatabase (ctx context.Context , dumpContID , dbName string , dumpDefinition DumpDefinition ) error {
438- dumpCommand := d .buildLogicalDumpCommand (dbName , dumpDefinition .Tables )
439- log .Msg ("Running dump command: " , dumpCommand )
439+ dumpCommand := d .buildLogicalDumpCommand (dbName , dumpDefinition )
440+
441+ if len (dumpDefinition .Tables ) > 0 ||
442+ len (dumpDefinition .ExcludeTables ) > 0 {
443+ log .Msg ("Partial dump" )
444+
445+ if len (dumpDefinition .Tables ) > 0 {
446+ log .Msg ("Including tables: " , strings .Join (dumpDefinition .Tables , ", " ))
447+ }
440448
441- if len (dumpDefinition .Tables ) > 0 {
442- log .Msg ("Partial dump will be run. Tables for dumping: " , strings .Join (dumpDefinition .Tables , ", " ))
449+ if len (dumpDefinition .ExcludeTables ) > 0 {
450+ log .Msg ("Excluding tables: " , strings .Join (dumpDefinition .ExcludeTables , ", " ))
451+ }
443452 }
444453
454+ log .Msg ("Running dump command: " , dumpCommand )
455+
445456 if output , err := d .performDumpCommand (ctx , dumpContID , types.ExecConfig {
446457 Tty : true ,
447458 Cmd : dumpCommand ,
@@ -488,7 +499,12 @@ func setupPGData(ctx context.Context, dockerClient *client.Client, dataDir strin
488499 return nil
489500}
490501
491- func updateConfigs (ctx context.Context , dockerClient * client.Client , dataDir , contID string , configs map [string ]string ) error {
502+ func updateConfigs (
503+ ctx context.Context ,
504+ dockerClient * client.Client ,
505+ dataDir , contID string ,
506+ configs map [string ]string ,
507+ ) error {
492508 log .Dbg ("Stopping container to update configuration" )
493509
494510 tools .StopContainer (ctx , dockerClient , contID , cont .StopTimeout )
@@ -605,21 +621,38 @@ func (d *DumpJob) getExecEnvironmentVariables() []string {
605621 return execEnvs
606622}
607623
608- func (d * DumpJob ) buildLogicalDumpCommand (dbName string , tables []string ) []string {
609- optionalArgs := map [string ]string {
610- "--host" : d .config .db .Host ,
611- "--port" : strconv .Itoa (d .config .db .Port ),
612- "--username" : d .config .db .Username ,
613- "--dbname" : dbName ,
614- "--jobs" : strconv .Itoa (d .DumpOptions .ParallelJobs ),
624+ func (d * DumpJob ) buildLogicalDumpCommand (dbName string , dump DumpDefinition ) []string {
625+ // don't use map here, it creates inconsistency in the order of arguments
626+ dumpCmd := []string {"pg_dump" , "--create" }
627+
628+ if d .config .db .Host != "" {
629+ dumpCmd = append (dumpCmd , "--host" , d .config .db .Host )
630+ }
631+
632+ if d .config .db .Port > 0 {
633+ dumpCmd = append (dumpCmd , "--port" , strconv .Itoa (d .config .db .Port ))
615634 }
616635
617- dumpCmd := append ([]string {"pg_dump" , "--create" }, prepareCmdOptions (optionalArgs )... )
636+ if d .config .db .Username != "" {
637+ dumpCmd = append (dumpCmd , "--username" , d .config .db .Username )
638+ }
618639
619- for _ , table := range tables {
640+ if dbName != "" {
641+ dumpCmd = append (dumpCmd , "--dbname" , dbName )
642+ }
643+
644+ if d .DumpOptions .ParallelJobs > 0 {
645+ dumpCmd = append (dumpCmd , "--jobs" , strconv .Itoa (d .DumpOptions .ParallelJobs ))
646+ }
647+
648+ for _ , table := range dump .Tables {
620649 dumpCmd = append (dumpCmd , "--table" , table )
621650 }
622651
652+ for _ , table := range dump .ExcludeTables {
653+ dumpCmd = append (dumpCmd , "--exclude-table" , table )
654+ }
655+
623656 // Define if restore directly or export to dump location.
624657 if d .DumpOptions .Restore .Enabled {
625658 dumpCmd = append (dumpCmd , "--format" , customFormat )
@@ -652,18 +685,6 @@ func (d *DumpJob) buildLogicalRestoreCommand(dbName string) []string {
652685 return restoreCmd
653686}
654687
655- func prepareCmdOptions (options map [string ]string ) []string {
656- cmdOptions := []string {}
657-
658- for optionKey , optionValue := range options {
659- if optionValue != "" {
660- cmdOptions = append (cmdOptions , optionKey , optionValue )
661- }
662- }
663-
664- return cmdOptions
665- }
666-
667688func (d * DumpJob ) markDatabaseData () error {
668689 if err := d .dbMarker .CreateConfig (); err != nil {
669690 return errors .Wrap (err , "failed to create a DBMarker config of the database" )
0 commit comments