Uploaded image for project: 'Blesta Core'
  1. Blesta Core
  2. CORE-4722

Remove unnecessary database transaction from Transactions::applyFromCredits()

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 5.5.0
    • Component/s: None
    • Labels:
      None

      Description

      Database transactions are not nestable (mostly) and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()

      Change app/models/transactions.php around line 875 from

              // Begin a transaction
              $this->begin();
      
              // Apply all credits
              foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                  $this->apply($transaction_id, ['amounts' => $trans_amounts]);
      
                  if (($errors = $this->errors())) {
                      // Roll back
                      $this->rollBack();
                      return;
                  }
              }
      
              // Commit transaction
              $this->commit();
      

      To

              // Apply all credits
              $last_transaction_id = null;
              $errors = null;
              foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                  $this->apply($transaction_id, ['amounts' => $trans_amounts]);
      
                  if (($errors = $this->errors())) {
                      break;
                  }
                  $last_transaction_id = $transaction_id;
              }
      
              if ($errors && $last_transaction_id) {
                  foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                      $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts);
                      $this->unapply($transaction_id, $invoice_ids);
      
                      if ($last_transaction_id == $transaction_id) {
                          break;
                      }
                  }
              }
      

        Activity

        jonathan Jonathan Reissmueller created issue -
        jonathan Jonathan Reissmueller made changes -
        Field Original Value New Value
        Rank Ranked higher
        jonathan Jonathan Reissmueller made changes -
        Description Database transactions are not nestable in MinPHP and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()

        Change

        {code:java}

                // Begin a transaction
                $this->begin();

                // Apply all credits
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        // Roll back
                        $this->rollBack();
                        return;
                    }
                }

                // Commit transaction
                $this->commit();
        {code}

        To

        {code:java}
                // Apply all credits
                $last_transaction_id = null;
                $errors = null;
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        break;
                    }
                    $last_transaction_id = $transaction_id;
                }

                if ($errors && $last_transaction_id) {
                    foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                        $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts);
                        $this->unapply($transaction_id, $invoice_ids);

                        if ($last_transaction_id == $transaction_id) {
                            break;
                        }
                    }
                }
        {code}
        Database transactions are not nestable in MinPHP and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()

        Change app/models/transactions.php around line

        {code:java}

                // Begin a transaction
                $this->begin();

                // Apply all credits
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        // Roll back
                        $this->rollBack();
                        return;
                    }
                }

                // Commit transaction
                $this->commit();
        {code}

        To

        {code:java}
                // Apply all credits
                $last_transaction_id = null;
                $errors = null;
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        break;
                    }
                    $last_transaction_id = $transaction_id;
                }

                if ($errors && $last_transaction_id) {
                    foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                        $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts);
                        $this->unapply($transaction_id, $invoice_ids);

                        if ($last_transaction_id == $transaction_id) {
                            break;
                        }
                    }
                }
        {code}
        jonathan Jonathan Reissmueller made changes -
        Description Database transactions are not nestable in MinPHP and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()

        Change app/models/transactions.php around line

        {code:java}

                // Begin a transaction
                $this->begin();

                // Apply all credits
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        // Roll back
                        $this->rollBack();
                        return;
                    }
                }

                // Commit transaction
                $this->commit();
        {code}

        To

        {code:java}
                // Apply all credits
                $last_transaction_id = null;
                $errors = null;
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        break;
                    }
                    $last_transaction_id = $transaction_id;
                }

                if ($errors && $last_transaction_id) {
                    foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                        $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts);
                        $this->unapply($transaction_id, $invoice_ids);

                        if ($last_transaction_id == $transaction_id) {
                            break;
                        }
                    }
                }
        {code}
        Database transactions are not nestable in MinPHP and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()

        Change app/models/transactions.php around line 875 from

        {code:java}

                // Begin a transaction
                $this->begin();

                // Apply all credits
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        // Roll back
                        $this->rollBack();
                        return;
                    }
                }

                // Commit transaction
                $this->commit();
        {code}

        To

        {code:java}
                // Apply all credits
                $last_transaction_id = null;
                $errors = null;
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        break;
                    }
                    $last_transaction_id = $transaction_id;
                }

                if ($errors && $last_transaction_id) {
                    foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                        $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts);
                        $this->unapply($transaction_id, $invoice_ids);

                        if ($last_transaction_id == $transaction_id) {
                            break;
                        }
                    }
                }
        {code}
        jonathan Jonathan Reissmueller made changes -
        Story Points 2
        jonathan Jonathan Reissmueller made changes -
        Sprint 5.5.0-b2 Sprint 1 [ 163 ]
        jonathan Jonathan Reissmueller made changes -
        Rank Ranked higher
        jonathan Jonathan Reissmueller made changes -
        Fix Version/s 5.5.0-b2 [ 11733 ]
        Fix Version/s 5.6.0-b1 [ 11730 ]
        jonathan Jonathan Reissmueller made changes -
        Description Database transactions are not nestable in MinPHP and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()

        Change app/models/transactions.php around line 875 from

        {code:java}

                // Begin a transaction
                $this->begin();

                // Apply all credits
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        // Roll back
                        $this->rollBack();
                        return;
                    }
                }

                // Commit transaction
                $this->commit();
        {code}

        To

        {code:java}
                // Apply all credits
                $last_transaction_id = null;
                $errors = null;
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        break;
                    }
                    $last_transaction_id = $transaction_id;
                }

                if ($errors && $last_transaction_id) {
                    foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                        $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts);
                        $this->unapply($transaction_id, $invoice_ids);

                        if ($last_transaction_id == $transaction_id) {
                            break;
                        }
                    }
                }
        {code}
        Database transactions are not nestable and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()

        Change app/models/transactions.php around line 875 from

        {code:java}

                // Begin a transaction
                $this->begin();

                // Apply all credits
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        // Roll back
                        $this->rollBack();
                        return;
                    }
                }

                // Commit transaction
                $this->commit();
        {code}

        To

        {code:java}
                // Apply all credits
                $last_transaction_id = null;
                $errors = null;
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        break;
                    }
                    $last_transaction_id = $transaction_id;
                }

                if ($errors && $last_transaction_id) {
                    foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                        $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts);
                        $this->unapply($transaction_id, $invoice_ids);

                        if ($last_transaction_id == $transaction_id) {
                            break;
                        }
                    }
                }
        {code}
        jonathan Jonathan Reissmueller made changes -
        Description Database transactions are not nestable and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()

        Change app/models/transactions.php around line 875 from

        {code:java}

                // Begin a transaction
                $this->begin();

                // Apply all credits
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        // Roll back
                        $this->rollBack();
                        return;
                    }
                }

                // Commit transaction
                $this->commit();
        {code}

        To

        {code:java}
                // Apply all credits
                $last_transaction_id = null;
                $errors = null;
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        break;
                    }
                    $last_transaction_id = $transaction_id;
                }

                if ($errors && $last_transaction_id) {
                    foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                        $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts);
                        $this->unapply($transaction_id, $invoice_ids);

                        if ($last_transaction_id == $transaction_id) {
                            break;
                        }
                    }
                }
        {code}
        Database transactions are not nestable (mostly) and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()

        Change app/models/transactions.php around line 875 from

        {code:java}

                // Begin a transaction
                $this->begin();

                // Apply all credits
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        // Roll back
                        $this->rollBack();
                        return;
                    }
                }

                // Commit transaction
                $this->commit();
        {code}

        To

        {code:java}
                // Apply all credits
                $last_transaction_id = null;
                $errors = null;
                foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                    $this->apply($transaction_id, ['amounts' => $trans_amounts]);

                    if (($errors = $this->errors())) {
                        break;
                    }
                    $last_transaction_id = $transaction_id;
                }

                if ($errors && $last_transaction_id) {
                    foreach ($apply_amounts as $transaction_id => $trans_amounts) {
                        $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts);
                        $this->unapply($transaction_id, $invoice_ids);

                        if ($last_transaction_id == $transaction_id) {
                            break;
                        }
                    }
                }
        {code}
        abdy Abdy Franco made changes -
        Assignee Abdy Franco [ abdy ]
        abdy Abdy Franco made changes -
        Status Open [ 1 ] In Progress [ 3 ]
        jonathan Jonathan Reissmueller made changes -
        Sprint 5.5.0-b2 Sprint 1 [ 163 ] 5.5.0 Sprint 1 [ 164 ]
        jonathan Jonathan Reissmueller made changes -
        Rank Ranked higher
        jonathan Jonathan Reissmueller made changes -
        Fix Version/s 5.5.0 [ 11734 ]
        Fix Version/s 5.5.0-b2 [ 11733 ]
        abdy Abdy Franco made changes -
        Remaining Estimate 0 minutes [ 0 ]
        Time Spent 58 minutes [ 3480 ]
        Worklog Id 15862 [ 15862 ]
        abdy Abdy Franco made changes -
        Status In Progress [ 3 ] In Review [ 5 ]
        Resolution Fixed [ 1 ]
        jonathan Jonathan Reissmueller made changes -
        Status In Review [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            abdy Abdy Franco
            Reporter:
            jonathan Jonathan Reissmueller
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Fix Release Date:
              8/Aug/22

              Time Tracking

              Estimated:
              Original Estimate - Not Specified
              Not Specified
              Remaining:
              Remaining Estimate - 0 minutes
              0m
              Logged:
              Time Spent - 58 minutes
              58m

                Agile