@@ -105,6 +105,7 @@ type RestoreOptions struct {
105
105
Configs map [string ]string `yaml:"configs"`
106
106
QueryPreprocessing query.PreprocessorCfg `yaml:"queryPreprocessing"`
107
107
CustomOptions []string `yaml:"customOptions"`
108
+ SkipPolicies bool `yaml:"skipPolicies"`
108
109
}
109
110
110
111
// Partial defines tables and rules for a partial logical restore.
@@ -501,7 +502,48 @@ func (r *RestoreJob) restoreDB(ctx context.Context, contID, dbName string, dbDef
501
502
}
502
503
}
503
504
504
- restoreCommand := r .buildLogicalRestoreCommand (dbName , dbDefinition )
505
+ var (
506
+ tmpListFile * os.File
507
+ err error
508
+ )
509
+
510
+ if r .RestoreOptions .SkipPolicies {
511
+ tmpListFile , err = os .CreateTemp (r .getDumpLocation (dbDefinition .Format , dbDefinition .dbName ), dbName + "-list-file*" )
512
+ if err != nil {
513
+ return fmt .Errorf ("failed to create temporary list file: %w" , err )
514
+ }
515
+
516
+ defer func () {
517
+ if err := os .Remove (tmpListFile .Name ()); err != nil {
518
+ log .Dbg ("Cannot remove temporary file:" , err )
519
+ }
520
+ }()
521
+ defer func () { _ = tmpListFile .Close () }()
522
+
523
+ dumpLocation := r .getDumpLocation (dbDefinition .Format , dbDefinition .dbName )
524
+
525
+ if dbDefinition .Format != directoryFormat {
526
+ dumpLocation = r .RestoreOptions .DumpLocation
527
+ }
528
+
529
+ preCmd := []string {"bash" , "-c" , `pg_restore -l ` + dumpLocation + " | grep -v POLICY > " + tmpListFile .Name ()}
530
+
531
+ log .Msg ("Running preparatory command to create list file for " + dbName , preCmd )
532
+
533
+ output , err := tools .ExecCommandWithOutput (ctx , r .dockerClient , contID , types.ExecConfig {
534
+ Tty : true ,
535
+ Cmd : preCmd ,
536
+ Env : []string {"PGAPPNAME=" + dleRetrieval },
537
+ })
538
+
539
+ if err != nil {
540
+ log .Dbg (output )
541
+
542
+ return fmt .Errorf ("failed to perform preparatory command: %w" , err )
543
+ }
544
+ }
545
+
546
+ restoreCommand := r .buildLogicalRestoreCommand (dbName , dbDefinition , tmpListFile )
505
547
log .Msg ("Running restore command for " + dbName , restoreCommand )
506
548
507
549
output , err := tools .ExecCommandWithOutput (ctx , r .dockerClient , contID , types.ExecConfig {
@@ -699,12 +741,12 @@ func (r *RestoreJob) updateDataStateAt() {
699
741
r .fsPool .SetDSA (dsaTime )
700
742
}
701
743
702
- func (r * RestoreJob ) buildLogicalRestoreCommand (dumpName string , definition DumpDefinition ) []string {
744
+ func (r * RestoreJob ) buildLogicalRestoreCommand (dumpName string , definition DumpDefinition , listFile * os. File ) []string {
703
745
if definition .Format == plainFormat {
704
746
return r .buildPlainTextCommand (dumpName , definition )
705
747
}
706
748
707
- return r .buildPGRestoreCommand (dumpName , definition )
749
+ return r .buildPGRestoreCommand (dumpName , definition , listFile )
708
750
}
709
751
710
752
func (r * RestoreJob ) buildPlainTextCommand (dumpName string , definition DumpDefinition ) []string {
@@ -729,7 +771,7 @@ func (r *RestoreJob) buildPlainTextCommand(dumpName string, definition DumpDefin
729
771
}
730
772
}
731
773
732
- func (r * RestoreJob ) buildPGRestoreCommand (dumpName string , definition DumpDefinition ) []string {
774
+ func (r * RestoreJob ) buildPGRestoreCommand (dumpName string , definition DumpDefinition , listFile * os. File ) []string {
733
775
// Using the default database name because the database for connection must exist.
734
776
restoreCmd := []string {"pg_restore" , "--username" , r .globalCfg .Database .User (), "--dbname" , defaults .DBName }
735
777
@@ -750,7 +792,25 @@ func (r *RestoreJob) buildPGRestoreCommand(dumpName string, definition DumpDefin
750
792
751
793
restoreCmd = append (restoreCmd , r .getDumpLocation (definition .Format , dumpName ))
752
794
753
- restoreCmd = append (restoreCmd , r .RestoreOptions .CustomOptions ... )
795
+ customOptions := r .RestoreOptions .CustomOptions
796
+
797
+ // Skip policies: https://gitlab.com/postgres-ai/database-lab/-/merge_requests/769
798
+ if listFile != nil {
799
+ restoreCmd = append (restoreCmd , fmt .Sprintf ("--use-list=%s" , listFile .Name ()))
800
+
801
+ customOptions = []string {}
802
+
803
+ for _ , customOption := range r .RestoreOptions .CustomOptions {
804
+ // Exclude -L (--use-list) in customOptions to avoid conflicts if skipping policies is enabled.
805
+ if strings .HasPrefix (customOption , "-L" ) || strings .HasPrefix (customOption , "--use-list" ) {
806
+ continue
807
+ }
808
+
809
+ customOptions = append (customOptions , customOption )
810
+ }
811
+ }
812
+
813
+ restoreCmd = append (restoreCmd , customOptions ... )
754
814
755
815
return restoreCmd
756
816
}
0 commit comments