Skip to content

Commit

Permalink
Merge #50724
Browse files Browse the repository at this point in the history
50724: sql: Add 'DETACHED" option to the list of allowed BACKUP options r=miretskiy a=miretskiy

Informs #47539

Add `DETACHED` option to the list of allowed BACKUP options.
This option requests the backup to run in detached mode: that is,
backup statement should not block, and instead simply return the job id.

Release Notes : None


Co-authored-by: Yevgeniy Miretskiy <[email protected]>
  • Loading branch information
craig[bot] and Yevgeniy Miretskiy committed Jun 29, 2020
2 parents fde6a51 + 62d21e7 commit 4d6e2ec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ unreserved_keyword ::=
| 'DELETE'
| 'DEFAULTS'
| 'DEFERRED'
| 'DETACHED'
| 'DISCARD'
| 'DOMAIN'
| 'DOUBLE'
Expand Down Expand Up @@ -1419,6 +1420,7 @@ role_options ::=
backup_options ::=
'ENCRYPTION_PASSPHRASE' '=' string_or_placeholder
| 'REVISION_HISTORY'
| 'DETACHED'

changefeed_targets ::=
single_table_pattern_list
Expand Down
12 changes: 9 additions & 3 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ func TestParse(t *testing.T) {
{`RESTORE DATABASE foo FROM ($1, $2), ($3, $4)`},
{`RESTORE DATABASE foo FROM ($1, $2), ($3, $4) AS OF SYSTEM TIME '1'`},

{`BACKUP TABLE foo TO 'bar' WITH revision_history`},
{`BACKUP TABLE foo TO 'bar' WITH revision_history, detached`},
{`RESTORE TABLE foo FROM 'bar' WITH key1, key2 = 'value'`},

{`IMPORT TABLE foo CREATE USING 'nodelocal://0/some/file' CSV DATA ('path/to/some/file', $1) WITH temp = 'path/to/temp'`},
Expand Down Expand Up @@ -2138,8 +2138,8 @@ $function$`,
`RESTORE TABLE foo, baz FROM 'bar' AS OF SYSTEM TIME '1'`},
{`BACKUP foo TO 'bar' WITH ENCRYPTION_PASSPHRASE = 'secret', revision_history`,
`BACKUP TABLE foo TO 'bar' WITH revision_history, encryption_passphrase='secret'`},
{`BACKUP foo TO 'bar' WITH OPTIONS (ENCRYPTION_PASSPHRASE = 'secret', revision_history)`,
`BACKUP TABLE foo TO 'bar' WITH revision_history, encryption_passphrase='secret'`},
{`BACKUP foo TO 'bar' WITH OPTIONS (detached, ENCRYPTION_PASSPHRASE = 'secret', revision_history)`,
`BACKUP TABLE foo TO 'bar' WITH revision_history, encryption_passphrase='secret', detached`},
{`RESTORE foo FROM 'bar' WITH key1, key2 = 'value'`,
`RESTORE TABLE foo FROM 'bar' WITH key1, key2 = 'value'`},
{`CREATE CHANGEFEED FOR foo INTO 'sink'`, `CREATE CHANGEFEED FOR TABLE foo INTO 'sink'`},
Expand Down Expand Up @@ -2992,6 +2992,12 @@ DETAIL: source SQL:
BACKUP foo TO 'bar' WITH revision_history, revision_history
^`,
},
{`BACKUP foo TO 'bar' WITH detached, revision_history, detached`,
`at or near "detached": syntax error: detached option specified multiple times
DETAIL: source SQL:
BACKUP foo TO 'bar' WITH detached, revision_history, detached
^`,
},
}
for _, d := range testData {
t.Run(d.sql, func(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ func (u *sqlSymUnion) alterTypeAddValuePlacement() *tree.AlterTypeAddValuePlacem
%token <str> CURRENT_USER CYCLE

%token <str> DATA DATABASE DATABASES DATE DAY DEC DECIMAL DEFAULT DEFAULTS
%token <str> DEALLOCATE DECLARE DEFERRABLE DEFERRED DELETE DESC
%token <str> DEALLOCATE DECLARE DEFERRABLE DEFERRED DELETE DESC DETACHED
%token <str> DISCARD DISTINCT DO DOMAIN DOUBLE DROP

%token <str> ELSE ENCODING ENCRYPTION_PASSPHRASE END ENUM ESCAPE EXCEPT EXCLUDE EXCLUDING
Expand Down Expand Up @@ -2020,6 +2020,7 @@ alter_attribute_action:
// Options:
// revision_history: enable revision history
// encryption_passphrase="secret": encrypt backups
// detached: execute backup job asynchronously, without waiting for its completion.
//
// %SeeAlso: RESTORE, WEBDOCS/backup.html
backup_stmt:
Expand Down Expand Up @@ -2093,6 +2094,10 @@ backup_options:
{
$$.val = &tree.BackupOptions{CaptureRevisionHistory: true}
}
| DETACHED
{
$$.val = &tree.BackupOptions{Detached: true}
}

// %Help: RESTORE - restore data from external storage
// %Category: CCL
Expand Down Expand Up @@ -10349,6 +10354,7 @@ unreserved_keyword:
| DELETE
| DEFAULTS
| DEFERRED
| DETACHED
| DISCARD
| DOMAIN
| DOUBLE
Expand Down
26 changes: 23 additions & 3 deletions pkg/sql/sem/tree/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
type BackupOptions struct {
CaptureRevisionHistory bool
EncryptionPassphrase Expr
Detached bool
}

var _ NodeFormatter = &BackupOptions{}
Expand Down Expand Up @@ -151,17 +152,28 @@ func (node *PartitionedBackup) Format(ctx *FmtCtx) {

// Format implements the NodeFormatter interface
func (o *BackupOptions) Format(ctx *FmtCtx) {
var addSep bool
maybeAddSep := func() {
if addSep {
ctx.WriteString(", ")
}
addSep = true
}
if o.CaptureRevisionHistory {
ctx.WriteString("revision_history")
addSep = true
}

if o.EncryptionPassphrase != nil {
if o.CaptureRevisionHistory {
ctx.WriteString(", ")
}
maybeAddSep()
ctx.WriteString("encryption_passphrase=")
o.EncryptionPassphrase.Format(ctx)
}

if o.Detached {
maybeAddSep()
ctx.WriteString("detached")
}
}

// CombineWith merges other backup options into this backup options struct.
Expand All @@ -181,6 +193,14 @@ func (o *BackupOptions) CombineWith(other *BackupOptions) error {
return errors.New("encryption_passphrase specified multiple times")
}

if o.Detached {
if other.Detached {
return errors.New("detached option specified multiple times")
}
} else {
o.Detached = other.Detached
}

return nil
}

Expand Down

0 comments on commit 4d6e2ec

Please sign in to comment.