alficles commented on a change in pull request #2364: Fix Traffic Ops Go to use 
transactions for deliveryservices, sslkeys, urisigning
URL: https://github.com/apache/trafficcontrol/pull/2364#discussion_r196160170
 
 

 ##########
 File path: 
traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv12.go
 ##########
 @@ -133,35 +96,45 @@ func getDSTenantIDByName(db *sql.DB, name string) (*int, 
bool, error) {
 }
 
 // GetXMLID loads the DeliveryService's xml_id from the database, from the ID. 
Returns whether the delivery service was found, and any error.
-func (ds *TODeliveryServiceV12) GetXMLID(db *sqlx.DB) (string, bool, error) {
+func (ds *TODeliveryServiceV12) GetXMLID(tx *sql.Tx) (string, bool, error) {
        if ds.ID == nil {
                return "", false, errors.New("missing ID")
        }
+       return GetXMLID(tx, *ds.ID)
+}
+
+// GetXMLID loads the DeliveryService's xml_id from the database, from the ID. 
Returns whether the delivery service was found, and any error.
+func GetXMLID(tx *sql.Tx, id int) (string, bool, error) {
        xmlID := ""
-       if err := db.QueryRow(`SELECT xml_id FROM deliveryservice where id = 
$1`, ds.ID).Scan(&xmlID); err != nil {
+       if err := tx.QueryRow(`SELECT xml_id FROM deliveryservice where id = 
$1`, id).Scan(&xmlID); err != nil {
                if err == sql.ErrNoRows {
                        return "", false, nil
                }
-               return "", false, fmt.Errorf("querying xml_id for delivery 
service ID '%v': %v", *ds.ID, err)
+               return "", false, fmt.Errorf("querying xml_id for delivery 
service ID '%v': %v", id, err)
        }
        return xmlID, true, nil
 }
 
 // IsTenantAuthorized checks that the user is authorized for both the delivery 
service's existing tenant, and the new tenant they're changing it to (if 
different).
-func (ds *TODeliveryServiceV12) IsTenantAuthorized(user auth.CurrentUser, db 
*sqlx.DB) (bool, error) {
-       return isTenantAuthorized(user, db, &ds.DeliveryServiceNullableV12)
+func (ds *TODeliveryServiceV12) IsTenantAuthorized(user *auth.CurrentUser, db 
*sqlx.DB) (bool, error) {
+       tx, err := db.DB.Begin() // must be last, MUST not return an error if 
this suceeds, without closing the tx
+       if err != nil {
+               return false, errors.New("beginning transaction: " + 
err.Error())
+       }
+       defer dbhelpers.FinishTx(tx, util.BoolPtr(true))
+       return isTenantAuthorized(user, tx, 
(*tc.DeliveryServiceNullableV12)(ds))
 }
 
-func isTenantAuthorized(user auth.CurrentUser, db *sqlx.DB, ds 
*tc.DeliveryServiceNullableV12) (bool, error) {
+func isTenantAuthorized(user *auth.CurrentUser, tx *sql.Tx, ds 
*tc.DeliveryServiceNullableV12) (bool, error) {
        if ds.ID == nil && ds.XMLID == nil {
                return false, errors.New("delivery service has no ID or XMLID")
        }
 
        existingID, err := (*int)(nil), error(nil)
 
 Review comment:
   I realize you didn't change this, but is this just a confusing way to say:
   
       var existingID *int
       var err error
   
   ?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to