Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: simplify V23_1ExternalConnectionsTableHasOwnerIDColumn gating #98739

Merged
merged 1 commit into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions pkg/cloud/externalconn/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,8 @@ func generatePlaceholders(n int) string {
// Only the values initialized in the receiver are persisted in the system
// table. If an error is returned, it is callers responsibility to handle it
// (e.g. rollback transaction).
func (e *MutableExternalConnection) Create(
ctx context.Context, txn isql.Txn, excludedCols map[string]bool,
) error {
cols, qargs, err := e.marshalChanges(excludedCols)
func (e *MutableExternalConnection) Create(ctx context.Context, txn isql.Txn) error {
cols, qargs, err := e.marshalChanges()
if err != nil {
return err
}
Expand Down Expand Up @@ -335,17 +333,11 @@ func (e *MutableExternalConnection) Create(

// marshalChanges marshals all changes in the in-memory representation and returns
// the names of the columns and marshaled values.
func (e *MutableExternalConnection) marshalChanges(
excludedCols map[string]bool,
) ([]string, []interface{}, error) {
func (e *MutableExternalConnection) marshalChanges() ([]string, []interface{}, error) {
var cols []string
var qargs []interface{}

for col := range e.dirty {
if excludedCols[col] {
continue
}

var arg tree.Datum
var err error

Expand All @@ -359,6 +351,9 @@ func (e *MutableExternalConnection) marshalChanges(
case `owner`:
arg = tree.NewDString(e.rec.Owner.Normalized())
case `owner_id`:
if e.rec.OwnerID == 0 {
continue
}
arg = tree.NewDOid(e.rec.OwnerID)
default:
return nil, nil, errors.Newf("cannot marshal column %q", col)
Expand Down
26 changes: 12 additions & 14 deletions pkg/sql/create_external_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,22 @@ func (p *planner) createExternalConnection(
ex.SetConnectionDetails(*exConn.ConnectionProto())
ex.SetConnectionType(exConn.ConnectionType())
ex.SetOwner(p.User())
row, err := txn.QueryRowEx(params.ctx, `get-user-id`, txn.KV(),
sessiondata.NodeUserSessionDataOverride,
`SELECT user_id FROM system.users WHERE username = $1`,
p.User(),
)
if err != nil {
return errors.Wrap(err, "failed to get owner ID for External Connection")
if p.ExecCfg().Settings.Version.IsActive(params.ctx, clusterversion.V23_1ExternalConnectionsTableHasOwnerIDColumn) {
row, err := txn.QueryRowEx(params.ctx, `get-user-id`, txn.KV(),
sessiondata.NodeUserSessionDataOverride,
`SELECT user_id FROM system.users WHERE username = $1`,
p.User(),
)
if err != nil {
return errors.Wrap(err, "failed to get owner ID for External Connection")
}
ownerID := tree.MustBeDOid(row[0]).Oid
ex.SetOwnerID(ownerID)
}
ownerID := tree.MustBeDOid(row[0]).Oid
ex.SetOwnerID(ownerID)

// Create the External Connection and persist it in the
// `system.external_connections` table.
excludedCols := make(map[string]bool)
if !p.ExecCfg().Settings.Version.IsActive(params.ctx, clusterversion.V23_1ExternalConnectionsTableHasOwnerIDColumn) {
excludedCols["owner_id"] = true
}
if err := ex.Create(params.ctx, txn, excludedCols); err != nil {
if err := ex.Create(params.ctx, txn); err != nil {
return errors.Wrap(err, "failed to create external connection")
}

Expand Down