forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
84751: sql,externalconn: introduce DROP EXTERNAL CONNECTION r=benbardin a=adityamaru This change introduces `DROP EXTERNAL CONNECTION` that can be used to drop an existing ExternalConnection object from the `system.external_connections` table. Fixes: cockroachdb#84226 Release note (sql change): `DROP EXTERNAL CONNECTION` can be used to drop a previously created External Connection object. 84872: importer: clean up importResumer.dropTables r=adityamaru a=msbutler Previously, importResumer.DropTables() assumed that IMPORT INTO could act upon multiple tables, which isn't actually the case, leading to overly complex code. This pr is a simple refactor, in preparation for more import rollback work in cockroachdb#76722 and cockroachdb#70428. Release note: none Co-authored-by: Aditya Maru <[email protected]> Co-authored-by: Michael Butler <[email protected]>
- Loading branch information
Showing
19 changed files
with
260 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
drop_external_connection_stmt ::= | ||
'DROP' 'EXTERNAL' 'CONNECTION' string_or_placeholder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ drop_stmt ::= | |
| drop_type_stmt | ||
| drop_role_stmt | ||
| drop_schedule_stmt | ||
| drop_external_connection_stmt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package sql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
const dropExternalConnectionOp = "DROP EXTERNAL CONNECTION" | ||
|
||
type dropExternalConnectionNode struct { | ||
n *tree.DropExternalConnection | ||
} | ||
|
||
// DropExternalConnection represents a DROP EXTERNAL CONNECTION statement. | ||
func (p *planner) DropExternalConnection( | ||
_ context.Context, n *tree.DropExternalConnection, | ||
) (planNode, error) { | ||
return &dropExternalConnectionNode{n: n}, nil | ||
} | ||
|
||
func (c *dropExternalConnectionNode) startExec(params runParams) error { | ||
return params.p.dropExternalConnection(params, c.n) | ||
} | ||
|
||
type dropExternalConnectionEval struct { | ||
externalConnectionName func() (string, error) | ||
} | ||
|
||
func (p *planner) makeDropExternalConnectionEval( | ||
ctx context.Context, n *tree.DropExternalConnection, | ||
) (*dropExternalConnectionEval, error) { | ||
var err error | ||
eval := &dropExternalConnectionEval{} | ||
eval.externalConnectionName, err = p.TypeAsString(ctx, n.ConnectionLabel, externalConnectionOp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return eval, err | ||
} | ||
|
||
func (p *planner) dropExternalConnection(params runParams, n *tree.DropExternalConnection) error { | ||
// TODO(adityamaru): Check that the user has `DROP` privileges on the External | ||
// Connection once we add support for it. Remove admin only check. | ||
hasAdmin, err := params.p.HasAdminRole(params.ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if !hasAdmin { | ||
return pgerror.New( | ||
pgcode.InsufficientPrivilege, | ||
"only users with the admin role are allowed to DROP EXTERNAL CONNECTION") | ||
} | ||
|
||
// TODO(adityamaru): Add some metrics to track DROP EXTERNAL CONNECTION | ||
// usage. | ||
|
||
eval, err := p.makeDropExternalConnectionEval(params.ctx, n) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
name, err := eval.externalConnectionName() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to resolve External Connection name") | ||
} | ||
|
||
if _ /* rows */, err = params.extendedEvalCtx.ExecCfg.InternalExecutor.ExecEx( | ||
params.ctx, | ||
dropExternalConnectionOp, | ||
params.p.Txn(), | ||
sessiondata.InternalExecutorOverride{User: params.p.User()}, | ||
`DELETE FROM system.external_connections WHERE connection_name = $1`, name, | ||
); err != nil { | ||
return errors.Wrapf(err, "failed to delete external connection") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *dropExternalConnectionNode) Next(_ runParams) (bool, error) { return false, nil } | ||
func (c *dropExternalConnectionNode) Values() tree.Datums { return nil } | ||
func (c *dropExternalConnectionNode) Close(_ context.Context) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.