@@ -98,10 +98,11 @@ type Source struct {
98
98
99
99
// DumpDefinition describes a database for dumping.
100
100
type 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
105
106
}
106
107
107
108
type dumpJobConfig struct {
@@ -435,13 +436,23 @@ func (d *DumpJob) cleanupDumpLocation(ctx context.Context, dumpContID string, db
435
436
}
436
437
437
438
func (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
+ }
440
448
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
+ }
443
452
}
444
453
454
+ log .Msg ("Running dump command: " , dumpCommand )
455
+
445
456
if output , err := d .performDumpCommand (ctx , dumpContID , types.ExecConfig {
446
457
Tty : true ,
447
458
Cmd : dumpCommand ,
@@ -488,7 +499,12 @@ func setupPGData(ctx context.Context, dockerClient *client.Client, dataDir strin
488
499
return nil
489
500
}
490
501
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 {
492
508
log .Dbg ("Stopping container to update configuration" )
493
509
494
510
tools .StopContainer (ctx , dockerClient , contID , cont .StopTimeout )
@@ -605,21 +621,38 @@ func (d *DumpJob) getExecEnvironmentVariables() []string {
605
621
return execEnvs
606
622
}
607
623
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 ))
615
634
}
616
635
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
+ }
618
639
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 {
620
649
dumpCmd = append (dumpCmd , "--table" , table )
621
650
}
622
651
652
+ for _ , table := range dump .ExcludeTables {
653
+ dumpCmd = append (dumpCmd , "--exclude-table" , table )
654
+ }
655
+
623
656
// Define if restore directly or export to dump location.
624
657
if d .DumpOptions .Restore .Enabled {
625
658
dumpCmd = append (dumpCmd , "--format" , customFormat )
@@ -652,18 +685,6 @@ func (d *DumpJob) buildLogicalRestoreCommand(dbName string) []string {
652
685
return restoreCmd
653
686
}
654
687
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
-
667
688
func (d * DumpJob ) markDatabaseData () error {
668
689
if err := d .dbMarker .CreateConfig (); err != nil {
669
690
return errors .Wrap (err , "failed to create a DBMarker config of the database" )
0 commit comments