-
Notifications
You must be signed in to change notification settings - Fork 578
/
pgdumpoptions.go
296 lines (266 loc) · 12.3 KB
/
pgdumpoptions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package backupoptions
/*
Copyright 2019 - 2020 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"errors"
"strings"
)
var pgDumpRestoreOptsDenyList = []string{
"--binary-upgrade",
"--dbname",
"--host",
"--no-password",
"--no-reconnect",
"--password",
"--port",
"--username",
"--version",
}
var pgDumpRestoreOptsDenyListShort = []string{
"-R",
"-d",
"-h",
"-p",
"-U",
"-w",
"-W",
"-V",
}
type pgDumpOptions struct {
DataOnly bool `flag:"data-only" flag-short:"a"`
Blobs bool `flag:"blobs" flag-short:"b"`
NoBlobs bool `flag:"no-blobs" flag-short:"B"`
Clean bool `flag:"clean" flag-short:"c"`
Create bool `flag:"create" flag-short:"C"`
Encoding string `flag:"encoding" flag-short:"E"`
Jobs int `flag:"jobs" flag-short:"j"`
Format string `flag:"format" flag-short:"F"`
Schema []string `flag:"schema" flag-short:"n"`
ExcludeSchema string `flag:"exclude-schema" flag-short:"N"`
Oids bool `flag:"oids" flag-short:"o"`
NoOwner bool `flag:"no-owner" flag-short:"O"`
SchemaOnly bool `flag:"schema-only" flag-short:"s"`
SuperUser string `flag:"superuser" flag-short:"S"`
Table []string `flag:"table" flag-short:"t"`
ExcludeTable string `flag:"exclude-table" flag-short:"T"`
Verbose bool `flag:"verbose" flag-short:"v"`
NoPrivileges bool `flag:"no-privileges" flag-short:"x"`
NoACL bool `flag:"no-acl"`
Compress int `flag:"compress" flag-short:"Z"`
ColumnInserts bool `flag:"column-inserts"`
AttributeInserts bool `flag:"attribute-inserts"`
DisableDollarQuoting bool `flag:"disable-dollar-quoting"`
DisableTriggers bool `flag:"disable-triggers"`
EnableRowSecurity bool `flag:"exclude-row-security"`
ExcludeTableData string `flag:"exclude-table-data"`
IfExists bool `flag:"if-exists"`
Inserts bool `flag:"inserts"`
LockWaitTimeout string `flag:"lock-wait-timeout"`
LoadViaPartitionRoot bool `flag:"load-via-partition-root"`
NoComments bool `flag:"no-comments"`
NoPublications bool `flag:"no-publications"` // PG 10+
NoSecurityLabels bool `flag:"no-security-labels"`
NoSubscriptions bool `flag:"no-subscriptions"` // PG 10+
NoSync bool `flag:"no-sync"`
NoTableSpaces bool `flag:"no-tablespaces"`
NoUnloggedTableData bool `flag:"no-unlogged-table-data"`
QuoteAllIdentifiers bool `flag:"quote-all-identifiers"`
Section []string `flag:"section"`
SerializableDeferrable bool `flag:"serializable-deferrable"`
Snapshot string `flag:"snapshot"`
StrictNames string `flag:"strict-names"` // PG 9.6+
UseSetSessionAuthorization bool `flag:"use-set-session-authorization"`
Role string `flag:"role"`
}
type pgDumpAllOptions struct {
DataOnly bool `flag:"data-only" flag-short:"a"`
Clean bool `flag:"clean" flag-short:"c"`
Encoding string `flag:"encoding" flag-short:"E"`
GlobalsOnly bool `flag:"globals-only" flag-short:"g"`
Oids bool `flag:"oids" flag-short:"o"`
NoOwner bool `flag:"no-owner" flag-short:"O"`
RolesOnly bool `flag:"roles-only" flag-short:"r"`
SchemaOnly bool `flag:"schema-only" flag-short:"s"`
SuperUser string `flag:"superuser" flag-short:"S"`
TablespacesOnly bool `flag:"tablespaces-only" flag-short:"t"`
Verbose bool `flag:"verbose" flag-short:"v"`
NoPrivileges bool `flag:"no-privileges" flag-short:"x"`
NoACL bool `flag:"no-acl"`
ColumnInserts bool `flag:"column-inserts"`
AttributeInserts bool `flag:"attribute-inserts"`
DisableDollarQuoting bool `flag:"disable-dollar-quoting"`
DisableTriggers bool `flag:"disable-triggers"`
IfExists bool `flag:"if-exists"`
Inserts bool `flag:"inserts"`
LockWaitTimeout string `flag:"lock-wait-timeout"`
LoadViaPartitionRoot bool `flag:"load-via-partition-root"`
NoComments bool `flag:"no-comments"`
NoPublications bool `flag:"no-publications"` // PG 10+
NoRolePasswords bool `flag:"no-role-passwords"`
NoSecurityLabels bool `flag:"no-security-labels"`
NoSubscriptions bool `flag:"no-subscriptions"` // PG 10+
NoSync bool `flag:"no-sync"`
NoTableSpaces bool `flag:"no-tablespaces"`
NoUnloggedTableData bool `flag:"no-unlogged-table-data"`
QuoteAllIdentifiers bool `flag:"quote-all-identifiers"`
UseSetSessionAuthorization bool `flag:"use-set-session-authorization"`
Role string `flag:"role"`
DumpAll bool `flag:"dump-all"` // custom pgo backup opt used for pg_dumpall
}
type pgRestoreOptions struct {
DataOnly bool `flag:"data-only" flag-short:"a"`
Clean bool `flag:"clean" flag-short:"c"`
Create bool `flag:"create" flag-short:"C"`
ExitOnError bool `flag:"exit-on-error" flag-short:"e"`
Filename string `flag:"filename" flag-short:"f"`
Format string `flag:"format" flag-short:"F"`
Index []string `flag:"index" flag-short:"I"`
Jobs int `flag:"jobs" flag-short:"j"`
List bool `flag:"list" flag-short:"l"`
UseList bool `flag:"useList" flag-short:"L"`
Schema string `flag:"schema" flag-short:"n"`
ExcludeSchema string `flag:"exclude-schema" flag-short:"N"`
NoOwner bool `flag:"no-owner" flag-short:"O"`
Function []string `flag:"function" flag-short:"P"`
SchemaOnly bool `flag:"schema-only" flag-short:"s"`
SuperUser string `flag:"superuser" flag-short:"S"`
Table string `flag:"table" flag-short:"t"`
Trigger []string `flag:"trigger" flag-short:"T"`
Verbose bool `flag:"verbose" flag-short:"v"`
NoPrivileges bool `flag:"no-privileges" flag-short:"x"`
NoACL bool `flag:"no-acl"`
SingleTransaction bool `flag:"single-transaction" flag-short:"1"`
DisableTriggers bool `flag:"disable-triggers"`
EnableRowSecurity bool `flag:"enable-row-security"`
IfExists bool `flag:"if-exists"`
NoComments bool `flag:"no-comments"`
NoDataForFailedTables bool `flag:"no-data-for-failed-tables"`
NoPublications bool `flag:"no-publications"` // PG 10+
NoSecurityLabels bool `flag:"no-security-labels"`
NoSubscriptions bool `flag:"no-subscriptions"` // PG 10+
NoTableSpaces bool `flag:"no-tablespaces"`
Section []string `flag:"section"`
StrictNames string `flag:"strict-names"` // PG 9.6+
UseSetSessionAuthorization bool `flag:"use-set-session-authorization"`
Role string `flag:"role"`
}
func (dumpOpts pgDumpOptions) validate(setFlagFieldNames []string) error {
var errstrings []string
for _, setFlag := range setFlagFieldNames {
switch setFlag {
case "Format":
if !isValidValue([]string{"p", "plain", "c", "custom", "t", "tar"}, dumpOpts.Format) {
err := errors.New("Invalid format provided for pg_dump backup")
errstrings = append(errstrings, err.Error())
}
case "SuperUser":
if !dumpOpts.DisableTriggers {
err := errors.New("The --superuser option is only applicable for a pg_dump backup if the " +
"--disable-triggers option has also been specified")
errstrings = append(errstrings, err.Error())
}
case "Compress":
if !isValidCompressLevel(dumpOpts.Compress) {
err := errors.New("Invalid compress level for pg_dump backup")
errstrings = append(errstrings, err.Error())
} else if dumpOpts.Format == "tar" {
err := errors.New("Compress level is not supported when using the tar format for a pg_dump backup")
errstrings = append(errstrings, err.Error())
}
case "IfExists":
if !dumpOpts.Clean {
err := errors.New("The --if-exists option is only valid for a pg_dump backup if the --clean option is " +
"also specified")
errstrings = append(errstrings, err.Error())
}
case "Section":
for _, currSection := range dumpOpts.Section {
if !isValidValue([]string{"pre-data", "data", "post-data"}, currSection) {
err := errors.New("Invalid section provided for pg_dump backup")
errstrings = append(errstrings, err.Error())
}
}
}
}
if len(errstrings) > 0 {
return errors.New(strings.Join(errstrings, "\n"))
}
return nil
}
func (dumpAllOpts pgDumpAllOptions) validate(setFlagFieldNames []string) error {
var errstrings []string
for _, setFlag := range setFlagFieldNames {
switch setFlag {
case "SuperUser":
if !dumpAllOpts.DisableTriggers {
err := errors.New("The --superuser option is only applicable for a pg_dumpall backup if the " +
"--disable-triggers option has also been specified")
errstrings = append(errstrings, err.Error())
}
case "IfExists":
if !dumpAllOpts.Clean {
err := errors.New("The --if-exists option is only valid for a pg_dumpall backup if the --clean option is " +
"also specified")
errstrings = append(errstrings, err.Error())
}
}
}
if len(errstrings) > 0 {
return errors.New(strings.Join(errstrings, "\n"))
}
return nil
}
func (restoreOpts pgRestoreOptions) validate(setFlagFieldNames []string) error {
var errstrings []string
for _, setFlag := range setFlagFieldNames {
switch setFlag {
case "Format":
if !isValidValue([]string{"p", "plain", "c", "custom", "t", "tar"}, restoreOpts.Format) {
err := errors.New("Invalid format provided for pg_restore restore")
errstrings = append(errstrings, err.Error())
}
case "SuperUser":
if !restoreOpts.DisableTriggers {
err := errors.New("The --superuser option is only applicable for a pg_restore restore if the " +
"--disable-triggers option has also been specified")
errstrings = append(errstrings, err.Error())
}
case "IfExists":
if !restoreOpts.Clean {
err := errors.New("The --if-exists option is only valid for a pg_restore restore if the --clean option is " +
"also specified")
errstrings = append(errstrings, err.Error())
}
case "Section":
for _, currSection := range restoreOpts.Section {
if !isValidValue([]string{"pre-data", "data", "post-data"}, currSection) {
err := errors.New("Invalid section provided for pg_restore restore")
errstrings = append(errstrings, err.Error())
}
}
}
}
if len(errstrings) > 0 {
return errors.New(strings.Join(errstrings, "\n"))
}
return nil
}
func (dumpOpts pgDumpOptions) getDenyListFlags() ([]string, []string) {
return pgDumpRestoreOptsDenyList, pgDumpRestoreOptsDenyListShort
}
func (dumpAllOpts pgDumpAllOptions) getDenyListFlags() ([]string, []string) {
return pgDumpRestoreOptsDenyList, pgDumpRestoreOptsDenyListShort
}
func (restoreOpts pgRestoreOptions) getDenyListFlags() ([]string, []string) {
return pgDumpRestoreOptsDenyList, pgDumpRestoreOptsDenyListShort
}