Skip to content

Commit 83f78ec

Browse files
authored
Merge pull request #897 from utopia-php/fix/remove-postgres-transaction-override
refactor(transaction): inherit postgres startTransaction
2 parents fff9f0e + a3ca087 commit 83f78ec

2 files changed

Lines changed: 6 additions & 39 deletions

File tree

src/Database/Adapter/Postgres.php

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,44 +31,6 @@
3131
class Postgres extends SQL
3232
{
3333
public const MAX_IDENTIFIER_NAME = 63;
34-
/**
35-
* @inheritDoc
36-
*/
37-
public function startTransaction(): bool
38-
{
39-
try {
40-
if ($this->inTransaction === 0) {
41-
try {
42-
if ($this->getPDO()->inTransaction()) {
43-
$this->getPDO()->rollBack();
44-
} else {
45-
// If no active transaction, this has no effect.
46-
$this->getPDO()->prepare('ROLLBACK')->execute();
47-
}
48-
} catch (PDOException) {
49-
// A pooled connection can report a transaction it no longer
50-
// holds after a reconnect (e.g. Swoole PDOProxy keeps its own
51-
// counter), making this cleanup rollback throw. It is best
52-
// effort; swallow it and begin a fresh transaction below.
53-
}
54-
55-
$result = $this->getPDO()->beginTransaction();
56-
} else {
57-
$this->getPDO()->exec('SAVEPOINT transaction' . $this->inTransaction);
58-
$result = true;
59-
}
60-
} catch (PDOException $e) {
61-
throw new TransactionException('Failed to start transaction: ' . $e->getMessage(), $e->getCode(), $e);
62-
}
63-
64-
if (!$result) {
65-
throw new TransactionException('Failed to start transaction');
66-
}
67-
68-
$this->inTransaction++;
69-
70-
return $result;
71-
}
7234

7335
/**
7436
* @inheritDoc

tests/unit/SQLTransactionTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use PDOException;
66
use PHPUnit\Framework\TestCase;
7+
use ReflectionMethod;
78
use Utopia\Database\Adapter\MySQL;
89
use Utopia\Database\Adapter\Postgres;
10+
use Utopia\Database\Adapter\SQL;
911
use Utopia\Database\Exception\Transaction as TransactionException;
1012

11-
class SQLTransactionTest extends TestCase
13+
final class SQLTransactionTest extends TestCase
1214
{
1315
/**
1416
* A pooled connection (e.g. Swoole PDOProxy) can keep its own transaction
@@ -61,6 +63,9 @@ public function testStartTransactionDoesNotMaskBeginFailureAfterDesyncedRollback
6163

6264
public function testPostgresStartTransactionRecoversFromDesyncedRollback(): void
6365
{
66+
$method = new ReflectionMethod(Postgres::class, 'startTransaction');
67+
$this->assertSame(SQL::class, $method->getDeclaringClass()->getName());
68+
6469
$pdo = $this->getMockBuilder(\PDO::class)
6570
->disableOriginalConstructor()
6671
->getMock();

0 commit comments

Comments
 (0)