Skip to content

Commit

Permalink
Refactor low-level transactions to `DatabaseTransaction/Repository->t…
Browse files Browse the repository at this point in the history
…ransaction()` API to handle nested transactions

- Important: It is recommended to update transactions to use new `DatabaseTransaction` API to ensure correct transaction handling within nested transactions.
- Introduced new `DatabaseTransaction` API usable directly via DI or via `Repository->getTransaction()`

remp/crm#3330
  • Loading branch information
burithetech committed Nov 15, 2024
1 parent 6367563 commit 1d39b7c
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Models/Auth/Sso/SsoUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function matchOrCreateUser(
): ActiveRow {
// Transaction may cause hermes event handlers to fail occasionally because the data is not committed into db at the time of event handling.
// TODO: Return transaction with remp/crm#2218 implementation
// $this->dbContext->beginTransaction();
// $this->connectedAccountsRepository->getTransaction()->start();
// try {
if ($loggedUserId) {
$connectedAccount = $this->connectedAccountsRepository->getByExternalId($type, $externalId);
Expand Down Expand Up @@ -81,10 +81,10 @@ public function matchOrCreateUser(
);
}
// } catch (\Exception $e) {
// $this->dbContext->rollBack();
// $this->connectedAccountsRepository->getTransaction()->rollback();
// throw $e;
// }
// $this->dbContext->commit();
// $this->connectedAccountsRepository->getTransaction()->commit();

return $user;
}
Expand Down
1 change: 0 additions & 1 deletion src/Repositories/AccessTokensRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function __construct(
DataProviderManager $dataProviderManager
) {
parent::__construct($database);
$this->database = $database;
$this->emitter = $emitter;
$this->userMetaRepository = $userMetaRepository;
$this->dataProviderManager = $dataProviderManager;
Expand Down
1 change: 0 additions & 1 deletion src/Repositories/UserConnectedAccountsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function __construct(
AuditLogRepository $auditLogRepository
) {
parent::__construct($database);
$this->database = $database;
$this->auditLogRepository = $auditLogRepository;
}

Expand Down
1 change: 0 additions & 1 deletion src/Repositories/UsersRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public function __construct(
Passwords $passwords
) {
parent::__construct($database);
$this->database = $database;
$this->emitter = $emitter;
$this->auditLogRepository = $auditLogRepository;
$this->hermesEmitter = $hermesEmmiter;
Expand Down
3 changes: 3 additions & 0 deletions src/Tests/Repositories/CountriesRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Crm\UsersModule\Tests\Repositories;

use Crm\ApplicationModule\Database\DatabaseTransaction;
use Crm\ApplicationModule\Seeders\CountriesSeeder;
use Crm\ApplicationModule\Tests\DatabaseTestCase;
use Crm\UsersModule\Repositories\CountriesRepository;
Expand Down Expand Up @@ -41,6 +42,7 @@ public function testDefaultCountrySuccess()
// default country set while initializing repository
$testCountry = 'CZ';
$countriesRepository = new CountriesRepository($testCountry, $this->inject(Explorer::class));
$countriesRepository->setTransaction($this->inject(DatabaseTransaction::class));
$country = $countriesRepository->defaultCountry();
$this->assertEquals($testCountry, $country->iso_code);
}
Expand All @@ -61,6 +63,7 @@ public function testDefaultCountryCountryIsoUnknownFailure()
new \Exception("Unable to load default country from provided ISO code [UNKNOWN].")
);
$countriesRepository = new CountriesRepository($testCountry, $this->inject(Explorer::class));
$countriesRepository->setTransaction($this->inject(DatabaseTransaction::class));
$countriesRepository->defaultCountry();
}
}

0 comments on commit 1d39b7c

Please sign in to comment.