|
14 | 14 | namespace CodeIgniter\Database\Live; |
15 | 15 |
|
16 | 16 | use CodeIgniter\Database\BaseConnection; |
| 17 | +use CodeIgniter\Database\Exceptions\DatabaseException; |
17 | 18 | use CodeIgniter\Database\Exceptions\RetryableTransactionException; |
18 | 19 | use CodeIgniter\Test\CIUnitTestCase; |
19 | 20 | use CodeIgniter\Test\DatabaseTestTrait; |
@@ -102,6 +103,25 @@ public function testTransactionRetriesRetryableExceptionAndCommits(): void |
102 | 103 | $this->seeInDatabase('job', ['name' => 'Retried Job 2']); |
103 | 104 | } |
104 | 105 |
|
| 106 | + public function testTransactionScopedTransExceptionRestoresAfterRetryAttempts(): void |
| 107 | + { |
| 108 | + $attempts = 0; |
| 109 | + |
| 110 | + $result = $this->db->transaction(static function () use (&$attempts): string { |
| 111 | + $attempts++; |
| 112 | + |
| 113 | + if ($attempts === 1) { |
| 114 | + throw new RetryableTransactionException('Deadlock found when trying to get lock.', 1213); |
| 115 | + } |
| 116 | + |
| 117 | + return 'committed'; |
| 118 | + }, attempts: 2, transException: true); |
| 119 | + |
| 120 | + $this->assertSame('committed', $result); |
| 121 | + $this->assertSame(2, $attempts); |
| 122 | + $this->assertFalse($this->getPrivateProperty($this->db, 'transException')); |
| 123 | + } |
| 124 | + |
105 | 125 | public function testRollbackCallbacksRunForFailedRetryAttempts(): void |
106 | 126 | { |
107 | 127 | $attempts = 0; |
@@ -243,6 +263,121 @@ public function testTransactionReturnsFalseAndRollsBackWhenTransactionStatusFail |
243 | 263 | $this->enableDBDebug(); |
244 | 264 | } |
245 | 265 |
|
| 266 | + public function testTransactionScopedTransExceptionRestoresAfterSuccess(): void |
| 267 | + { |
| 268 | + $result = $this->db->transaction(static function (BaseConnection $db): string { |
| 269 | + $db->table('job')->insert([ |
| 270 | + 'name' => 'Scoped Exception Mode Job', |
| 271 | + 'description' => 'The transaction should commit.', |
| 272 | + ]); |
| 273 | + |
| 274 | + return 'committed'; |
| 275 | + }, transException: true); |
| 276 | + |
| 277 | + $this->assertSame('committed', $result); |
| 278 | + $this->assertFalse($this->getPrivateProperty($this->db, 'transException')); |
| 279 | + } |
| 280 | + |
| 281 | + public function testTransactionScopedTransExceptionRestoresAfterQueryFailure(): void |
| 282 | + { |
| 283 | + try { |
| 284 | + $this->db->transaction(static function (BaseConnection $db): void { |
| 285 | + $db->table('job')->insert([ |
| 286 | + 'id' => 1, |
| 287 | + 'name' => 'Duplicate Job', |
| 288 | + 'description' => 'This should fail.', |
| 289 | + ]); |
| 290 | + }, transException: true); |
| 291 | + $this->fail('Expected database exception.'); |
| 292 | + } catch (DatabaseException) { |
| 293 | + // The scoped transaction exception mode should be restored after failure. |
| 294 | + } |
| 295 | + |
| 296 | + $this->assertFalse($this->getPrivateProperty($this->db, 'transException')); |
| 297 | + } |
| 298 | + |
| 299 | + public function testTransactionScopedTransExceptionCanTemporarilyDisableExistingMode(): void |
| 300 | + { |
| 301 | + $this->db->transException(true); |
| 302 | + |
| 303 | + $result = $this->db->transaction(static function (BaseConnection $db): string { |
| 304 | + $db->table('job')->insert([ |
| 305 | + 'id' => 1, |
| 306 | + 'name' => 'Duplicate Job', |
| 307 | + 'description' => 'This should fail.', |
| 308 | + ]); |
| 309 | + |
| 310 | + return 'not returned'; |
| 311 | + }, transException: false); |
| 312 | + |
| 313 | + $this->assertFalse($result); |
| 314 | + $this->assertTrue($this->getPrivateProperty($this->db, 'transException')); |
| 315 | + } |
| 316 | + |
| 317 | + public function testTransactionResetTransStatusRestartsAfterStrictModeFailure(): void |
| 318 | + { |
| 319 | + $this->disableDBDebug(); |
| 320 | + |
| 321 | + $failed = $this->db->transaction(static function (BaseConnection $db): string { |
| 322 | + $db->table('job')->insert([ |
| 323 | + 'id' => 1, |
| 324 | + 'name' => 'Duplicate Job', |
| 325 | + 'description' => 'This should fail.', |
| 326 | + ]); |
| 327 | + |
| 328 | + return 'not returned'; |
| 329 | + }); |
| 330 | + |
| 331 | + $this->assertFalse($failed); |
| 332 | + $this->assertFalse($this->db->transStatus()); |
| 333 | + |
| 334 | + $result = $this->db->transaction(static function (BaseConnection $db): string { |
| 335 | + $db->table('job')->insert([ |
| 336 | + 'name' => 'Restarted Job', |
| 337 | + 'description' => 'The transaction should commit.', |
| 338 | + ]); |
| 339 | + |
| 340 | + return 'committed'; |
| 341 | + }, resetTransStatus: true); |
| 342 | + |
| 343 | + $this->assertSame('committed', $result); |
| 344 | + $this->assertTrue($this->db->transStatus()); |
| 345 | + $this->seeInDatabase('job', ['name' => 'Restarted Job']); |
| 346 | + |
| 347 | + $this->enableDBDebug(); |
| 348 | + } |
| 349 | + |
| 350 | + public function testTransactionWithoutResetTransStatusPreservesStrictModeFailure(): void |
| 351 | + { |
| 352 | + $this->disableDBDebug(); |
| 353 | + |
| 354 | + $failed = $this->db->transaction(static function (BaseConnection $db): string { |
| 355 | + $db->table('job')->insert([ |
| 356 | + 'id' => 1, |
| 357 | + 'name' => 'Duplicate Job', |
| 358 | + 'description' => 'This should fail.', |
| 359 | + ]); |
| 360 | + |
| 361 | + return 'not returned'; |
| 362 | + }); |
| 363 | + |
| 364 | + $this->assertFalse($failed); |
| 365 | + |
| 366 | + $result = $this->db->transaction(static function (BaseConnection $db): string { |
| 367 | + $db->table('job')->insert([ |
| 368 | + 'name' => 'Still Rolled Back Job', |
| 369 | + 'description' => 'The strict-mode failure should still apply.', |
| 370 | + ]); |
| 371 | + |
| 372 | + return 'not returned'; |
| 373 | + }); |
| 374 | + |
| 375 | + $this->assertFalse($result); |
| 376 | + $this->dontSeeInDatabase('job', ['name' => 'Still Rolled Back Job']); |
| 377 | + |
| 378 | + $this->enableDBDebug(); |
| 379 | + } |
| 380 | + |
246 | 381 | public function testTransactionCallbackExceptionDoesNotPreventNonStrictStatusReset(): void |
247 | 382 | { |
248 | 383 | $this->disableDBDebug(); |
@@ -412,6 +547,52 @@ public function testNestedTransactionCallbackExceptionMarksOuterTransactionForRo |
412 | 547 | $this->dontSeeInDatabase('job', ['name' => 'Inner Job']); |
413 | 548 | } |
414 | 549 |
|
| 550 | + public function testNestedTransactionResetTransStatusDoesNotClearOuterFailure(): void |
| 551 | + { |
| 552 | + $this->disableDBDebug(); |
| 553 | + |
| 554 | + $this->db->transStart(); |
| 555 | + $this->db->table('job')->insert([ |
| 556 | + 'id' => 1, |
| 557 | + 'name' => 'Duplicate Job', |
| 558 | + 'description' => 'This should fail.', |
| 559 | + ]); |
| 560 | + |
| 561 | + $result = $this->db->transaction(static fn (): string => 'not returned', resetTransStatus: true); |
| 562 | + |
| 563 | + $this->assertFalse($result); |
| 564 | + $this->assertFalse($this->db->transStatus()); |
| 565 | + |
| 566 | + $this->db->transComplete(); |
| 567 | + |
| 568 | + $this->enableDBDebug(); |
| 569 | + } |
| 570 | + |
| 571 | + public function testNestedTransactionScopedTransExceptionQueryFailureRollsBackOuterTransaction(): void |
| 572 | + { |
| 573 | + $this->db->transStart(); |
| 574 | + $this->db->table('job')->insert([ |
| 575 | + 'name' => 'Outer Job', |
| 576 | + 'description' => 'The outer transaction should roll back.', |
| 577 | + ]); |
| 578 | + |
| 579 | + try { |
| 580 | + $this->db->transaction(static function (BaseConnection $db): void { |
| 581 | + $db->table('job')->insert([ |
| 582 | + 'id' => 1, |
| 583 | + 'name' => 'Duplicate Job', |
| 584 | + 'description' => 'This should fail.', |
| 585 | + ]); |
| 586 | + }, transException: true); |
| 587 | + $this->fail('Expected database exception.'); |
| 588 | + } catch (DatabaseException) { |
| 589 | + // Existing transaction exception handling rolls back the full stack. |
| 590 | + } |
| 591 | + |
| 592 | + $this->assertSame(0, $this->db->transDepth); |
| 593 | + $this->dontSeeInDatabase('job', ['name' => 'Outer Job']); |
| 594 | + } |
| 595 | + |
415 | 596 | public function testNestedTransactionRetryAttemptsRunOnce(): void |
416 | 597 | { |
417 | 598 | $attempts = 0; |
|
0 commit comments