github.com/tuhaihe/gpbackup@v1.0.3/backup/predata_shared.go (about) 1 package backup 2 3 /* 4 * This file contains structs and functions related to backing up metadata shared 5 * among many or all object types (privileges, owners, and comments) on the 6 * coordinator that needs to be restored before data is restored. 7 */ 8 9 import ( 10 "github.com/tuhaihe/gp-common-go-libs/gplog" 11 "github.com/tuhaihe/gpbackup/toc" 12 "github.com/tuhaihe/gpbackup/utils" 13 "github.com/pkg/errors" 14 ) 15 16 /* 17 * There's no built-in function to generate constraint definitions like there is for other types of 18 * metadata, so this function constructs them. 19 */ 20 func PrintConstraintStatement(metadataFile *utils.FileWithByteCount, toc *toc.TOC, constraint Constraint, conMetadata ObjectMetadata) { 21 alterStr := "\n\nALTER %s %s ADD CONSTRAINT %s %s;\n" 22 start := metadataFile.ByteCount 23 // ConIsLocal should always return true from GetConstraints because we filter out constraints that are inherited using the INHERITS clause, or inherited from a parent partition table. This field only accurately reflects constraints in GPDB6+ because check constraints on parent tables must propogate to children. For GPDB versions 5 or lower, this field will default to false. 24 objStr := "TABLE ONLY" 25 if constraint.IsPartitionParent || (constraint.ConType == "c" && constraint.ConIsLocal) { 26 objStr = "TABLE" 27 } 28 metadataFile.MustPrintf(alterStr, objStr, constraint.OwningObject, constraint.Name, constraint.Def.String) 29 30 section, entry := constraint.GetMetadataEntry() 31 toc.AddMetadataEntry(section, entry, start, metadataFile.ByteCount) 32 PrintObjectMetadata(metadataFile, toc, conMetadata, constraint, constraint.OwningObject) 33 } 34 35 func PrintCreateSchemaStatements(metadataFile *utils.FileWithByteCount, toc *toc.TOC, schemas []Schema, schemaMetadata MetadataMap) { 36 for _, schema := range schemas { 37 start := metadataFile.ByteCount 38 metadataFile.MustPrintln() 39 if schema.Name != "public" { 40 metadataFile.MustPrintf("\nCREATE SCHEMA %s;", schema.Name) 41 } 42 section, entry := schema.GetMetadataEntry() 43 toc.AddMetadataEntry(section, entry, start, metadataFile.ByteCount) 44 PrintObjectMetadata(metadataFile, toc, schemaMetadata[schema.GetUniqueID()], schema, "") 45 } 46 } 47 48 func PrintAccessMethodStatements(metadataFile *utils.FileWithByteCount, toc *toc.TOC, accessMethods []AccessMethod, accessMethodMetadata MetadataMap) { 49 for _, method := range accessMethods { 50 start := metadataFile.ByteCount 51 methodTypeStr := "" 52 switch method.Type { 53 case "t": 54 methodTypeStr = "TABLE" 55 case "i": 56 methodTypeStr = "INDEX" 57 default: 58 gplog.Fatal(errors.Errorf("Invalid access method type: expected 't' or 'i', got '%s'\n", method.Type), "") 59 } 60 metadataFile.MustPrintf("\n\nCREATE ACCESS METHOD %s TYPE %s HANDLER %s;", method.Name, methodTypeStr, method.Handler) 61 section, entry := method.GetMetadataEntry() 62 toc.AddMetadataEntry(section, entry, start, metadataFile.ByteCount) 63 PrintObjectMetadata(metadataFile, toc, accessMethodMetadata[method.GetUniqueID()], method, "") 64 } 65 }