github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/backup.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package tree 12 13 // DescriptorCoverage specifies whether or not a subset of descriptors were 14 // requested or if all the descriptors were requested, so all the descriptors 15 // are covered in a given backup. 16 type DescriptorCoverage int32 17 18 const ( 19 // RequestedDescriptors table coverage means that the backup is not 20 // guaranteed to have all of the cluster data. This can be accomplished by 21 // backing up a specific subset of tables/databases. Note that even if all 22 // of the tables and databases have been included in the backup manually, a 23 // backup is not said to have complete table coverage unless it was created 24 // by a `BACKUP TO` command. 25 RequestedDescriptors DescriptorCoverage = iota 26 // AllDescriptors table coverage means that backup is guaranteed to have all the 27 // relevant data in the cluster. These can only be created by running a 28 // full cluster backup with `BACKUP TO`. 29 AllDescriptors 30 ) 31 32 // Backup represents a BACKUP statement. 33 type Backup struct { 34 Targets TargetList 35 DescriptorCoverage DescriptorCoverage 36 To PartitionedBackup 37 IncrementalFrom Exprs 38 AsOf AsOfClause 39 Options KVOptions 40 } 41 42 var _ Statement = &Backup{} 43 44 // Format implements the NodeFormatter interface. 45 func (node *Backup) Format(ctx *FmtCtx) { 46 ctx.WriteString("BACKUP ") 47 if node.DescriptorCoverage == RequestedDescriptors { 48 ctx.FormatNode(&node.Targets) 49 } 50 ctx.WriteString(" TO ") 51 ctx.FormatNode(&node.To) 52 if node.AsOf.Expr != nil { 53 ctx.WriteString(" ") 54 ctx.FormatNode(&node.AsOf) 55 } 56 if node.IncrementalFrom != nil { 57 ctx.WriteString(" INCREMENTAL FROM ") 58 ctx.FormatNode(&node.IncrementalFrom) 59 } 60 if node.Options != nil { 61 ctx.WriteString(" WITH ") 62 ctx.FormatNode(&node.Options) 63 } 64 } 65 66 // Restore represents a RESTORE statement. 67 type Restore struct { 68 Targets TargetList 69 DescriptorCoverage DescriptorCoverage 70 From []PartitionedBackup 71 AsOf AsOfClause 72 Options KVOptions 73 } 74 75 var _ Statement = &Restore{} 76 77 // Format implements the NodeFormatter interface. 78 func (node *Restore) Format(ctx *FmtCtx) { 79 ctx.WriteString("RESTORE ") 80 if node.DescriptorCoverage == RequestedDescriptors { 81 ctx.FormatNode(&node.Targets) 82 } 83 ctx.WriteString(" FROM ") 84 for i := range node.From { 85 if i > 0 { 86 ctx.WriteString(", ") 87 } 88 ctx.FormatNode(&node.From[i]) 89 } 90 if node.AsOf.Expr != nil { 91 ctx.WriteString(" ") 92 ctx.FormatNode(&node.AsOf) 93 } 94 if node.Options != nil { 95 ctx.WriteString(" WITH ") 96 ctx.FormatNode(&node.Options) 97 } 98 } 99 100 // KVOption is a key-value option. 101 type KVOption struct { 102 Key Name 103 Value Expr 104 } 105 106 // KVOptions is a list of KVOptions. 107 type KVOptions []KVOption 108 109 // Format implements the NodeFormatter interface. 110 func (o *KVOptions) Format(ctx *FmtCtx) { 111 for i := range *o { 112 n := &(*o)[i] 113 if i > 0 { 114 ctx.WriteString(", ") 115 } 116 ctx.FormatNode(&n.Key) 117 if n.Value != nil { 118 ctx.WriteString(` = `) 119 ctx.FormatNode(n.Value) 120 } 121 } 122 } 123 124 // PartitionedBackup is a list of destination URIs for a single BACKUP. A single 125 // URI corresponds to the special case of a regular backup, and multiple URIs 126 // correspond to a partitioned backup whose locality configuration is 127 // specified by LOCALITY url params. 128 type PartitionedBackup []Expr 129 130 // Format implements the NodeFormatter interface. 131 func (node *PartitionedBackup) Format(ctx *FmtCtx) { 132 if len(*node) > 1 { 133 ctx.WriteString("(") 134 } 135 ctx.FormatNode((*Exprs)(node)) 136 if len(*node) > 1 { 137 ctx.WriteString(")") 138 } 139 }