diff --git a/pkg/cloud/externalconn/record.go b/pkg/cloud/externalconn/record.go index 1d5a9552e699..b08d83337ff2 100644 --- a/pkg/cloud/externalconn/record.go +++ b/pkg/cloud/externalconn/record.go @@ -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 } @@ -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 @@ -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) diff --git a/pkg/sql/create_external_connection.go b/pkg/sql/create_external_connection.go index e6fc0bd6536d..63c2b4f6c32b 100644 --- a/pkg/sql/create_external_connection.go +++ b/pkg/sql/create_external_connection.go @@ -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") }