Repository navigation
sql,externalconn: add ALTER EXTERNAL CONNECTION - #146829
craig[bot] merged 1 commit into
Conversation
ba53baf to
8d269a4
Compare
890218e to
ae19f56
Compare
There was a problem hiding this comment.
i don't think the IF NOT EXISTS clause makes sense here. You can't alter an external connection that does not exist.
You should add an IF EXISTS clause instead. As an analogy, we got CREATE TABLE IF NOT EXISTS and ALTER TABLE IF EXISTS. To implement, I dont think you can use the labelSpec.
There was a problem hiding this comment.
Gotcha, I will update here and the implementation.
There was a problem hiding this comment.
Looks like the comment here still needs a quick amend
ae19f56 to
f1ff80c
Compare
|
|
||
| // ExternalConnectionExist loads an mutableExternalConnection from system table. Allow owner to modifiy the URI. | ||
| func ExternalConnectionExist(ctx context.Context, name string, ownerID oid.Oid, txn isql.Txn) error { | ||
| row, _, err := txn.QueryRowExWithCols(ctx, "lookup-schedule", txn.KV(), |
There was a problem hiding this comment.
Hey @msbutler,
I am not sure if I should use lookup-schedule or something else?
There was a problem hiding this comment.
the name is fine, but I don't think you need this method at all -- you can just use LoadExternalConnection and then see what you get.
| // CREATE EXTERNAL CONNECTION is only allowed for users with the | ||
| // `EXTERNALCONNECTION` system privilege. We run the query as `node` | ||
| // since the user might not have `INSERT` on the system table. | ||
| createQuery := "UPSERT INTO system.external_connections (%s) VALUES (%s) RETURNING connection_name" |
There was a problem hiding this comment.
Should I use the standard PostgreSQL syntax here—e.g., INSERT … ON CONFLICT DO UPDATE? Can I assume the internal SQL planner still uses CRDB?
CC: @msbutler
There was a problem hiding this comment.
Do we want to insert here? should we be using UPDATE instead since we are being told to alter an existing connection, not make a new one?
| return err | ||
| } | ||
|
|
||
| // CREATE EXTERNAL CONNECTION is only allowed for users with the |
There was a problem hiding this comment.
oh no, I will update the comment here.
There was a problem hiding this comment.
Shouldn't we need to be an owner of the connection?
There was a problem hiding this comment.
Also, this is a somewhat unusual place to do priv checks: typically we do our priv checks in the planner methods and if we have a helper like this one that handles CRUD ops, it just does the CRUD ops and we we use it after doing the priv check in the planner method.
| if err != nil { | ||
| // If we see a UniqueViolation it means that we are attempting to create an | ||
| // External Connection with a `connection_name` that already exists. | ||
| if pgerror.GetPGCode(err) == pgcode.UniqueViolation { |
There was a problem hiding this comment.
I see that CREATE EXTERNAL CONNECTION already enforces uniqueness. Can we skip that check for ALTER EXTERNAL CONNECTION?
There was a problem hiding this comment.
Looks like the comment here still needs a quick amend
| return err | ||
| } | ||
|
|
||
| // CREATE EXTERNAL CONNECTION is only allowed for users with the |
There was a problem hiding this comment.
Shouldn't we need to be an owner of the connection?
| // CREATE EXTERNAL CONNECTION is only allowed for users with the | ||
| // `EXTERNALCONNECTION` system privilege. We run the query as `node` | ||
| // since the user might not have `INSERT` on the system table. | ||
| createQuery := "UPSERT INTO system.external_connections (%s) VALUES (%s) RETURNING connection_name" |
There was a problem hiding this comment.
Do we want to insert here? should we be using UPDATE instead since we are being told to alter an existing connection, not make a new one?
| // `EXTERNALCONNECTION` system privilege. We run the query as `node` | ||
| // since the user might not have `INSERT` on the system table. | ||
| createQuery := "UPSERT INTO system.external_connections (%s) VALUES (%s) RETURNING connection_name" | ||
| row, retCols, err := txn.QueryRowExWithCols(ctx, "ExternalConnection.Create", txn.KV(), |
There was a problem hiding this comment.
I believe opName convention is lower-case-with-dashes?
| return errors.Wrap(err, "failed to log and sanitize External Connection") | ||
| } | ||
|
|
||
| ex := externalconn.NewMutableExternalConnection() |
There was a problem hiding this comment.
I expected, since we're talking about a conn that already exists, we'd probably use externalconn.LoadExternalConnection(name) vs making an empty structure and setting all the fields up manually?
| return e.InitFromDatums(row, retCols) | ||
| } | ||
|
|
||
| func (e *MutableExternalConnection) Update(ctx context.Context, txn isql.Txn) error { |
There was a problem hiding this comment.
alternatively, what about having this API be "UpdateURI(ctx, newURI)" that takes the new URI but otherwise assumes it is being called on an existing, fully initialized MutableExternalConnection that represents an existing db-stored external conn? Then this method is just a thin wrapper around db.Exec(UPDATE system..., e.ConnectionName, newURI)`
| return err | ||
| } | ||
|
|
||
| // CREATE EXTERNAL CONNECTION is only allowed for users with the |
There was a problem hiding this comment.
Also, this is a somewhat unusual place to do priv checks: typically we do our priv checks in the planner methods and if we have a helper like this one that handles CRUD ops, it just does the CRUD ops and we we use it after doing the priv check in the planner method.
|
|
||
| // ExternalConnectionExist loads an mutableExternalConnection from system table. Allow owner to modifiy the URI. | ||
| func ExternalConnectionExist(ctx context.Context, name string, ownerID oid.Oid, txn isql.Txn) error { | ||
| row, _, err := txn.QueryRowExWithCols(ctx, "lookup-schedule", txn.KV(), |
There was a problem hiding this comment.
the name is fine, but I don't think you need this method at all -- you can just use LoadExternalConnection and then see what you get.
f1ff80c to
9760a2c
Compare
|
@edwardguo-crl looks like the build failed here. As i mentioned before, could you split this PR in 2: one for the syntax change, and then a follow up PR with the logic in the externalconn pkg? This will allow us to merge the syntax change quickly in a somewhat hot file (sql.y), and avoid future merge conflicts. |
6a63572 to
0966a1a
Compare
Add AST parser support for the new SQL command ALTER EXTERNAL CONNECTION, allowing users to modify existing external connections. Fixes cockroachdb#98610
0966a1a to
85546d5
Compare
| 'ALTER' 'JOB' a_expr 'OWNER' 'TO' role_spec | ||
|
|
||
| label_spec ::= | ||
| string_or_placeholder |
There was a problem hiding this comment.
nit: stray diff with the delete block below
|
TFTR! bors r+ |
Summary
Add AST parser support for the new SQL command
ALTER EXTERNAL CONNECTION, allowing users to modify existing external connections.Fixes #98610