Skip to content

Commit

Permalink
Merge pull request #8767 from kenjis/feat-resetTransStatus
Browse files Browse the repository at this point in the history
feat: add BaseConnection::resetTransStatus()
  • Loading branch information
kenjis authored Jun 30, 2024
2 parents 85434df + 723d9e7 commit 3dffbc9
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
10 changes: 10 additions & 0 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,16 @@ public function transRollback(): bool
return false;
}

/**
* Reset transaction status - to restart transactions after strict mode failure
*/
public function resetTransStatus(): static
{
$this->transStatus = true;

return $this;
}

/**
* Begin Transaction
*/
Expand Down
48 changes: 48 additions & 0 deletions tests/system/Database/Live/TransactionDBDebugTrueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,54 @@ public function testTransStrictFalse(): void
$this->seeInDatabase('job', ['name' => 'Comedian']);
}

public function testTransStrictTrueAndResetTransStatus(): void
{
$builder = $this->db->table('job');

// The first transaction group
$this->db->transStart();

$jobData = [
'name' => 'Grocery Sales',
'description' => 'Discount!',
];
$builder->insert($jobData);

$this->assertTrue($this->db->transStatus());

// Duplicate entry '1' for key 'PRIMARY'
$jobData = [
'id' => 1,
'name' => 'Comedian',
'description' => 'Theres something in your teeth',
];
$builder->insert($jobData);

$this->assertFalse($this->db->transStatus());

$this->db->transComplete();

$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);

// Resets TransStatus
$this->db->resetTransStatus();

// The second transaction group
$this->db->transStart();

$jobData = [
'name' => 'Comedian',
'description' => 'Theres something in your teeth',
];
$builder->insert($jobData);

$this->assertTrue($this->db->transStatus());

$this->db->transComplete();

$this->seeInDatabase('job', ['name' => 'Comedian']);
}

public function testTransBegin(): void
{
$builder = $this->db->table('job');
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ Others
------

- Added a new configuration ``foundRows`` for MySQLi to use ``MYSQLI_CLIENT_FOUND_ROWS``.
- Added the ``BaseConnection::resetTransStatus()`` method to reset the transaction
status. See :ref:`transactions-resetting-transaction-status` for details.

Model
=====
Expand Down
29 changes: 23 additions & 6 deletions user_guide_src/source/database/transactions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ transactions.

.. contents::
:local:
:depth: 2
:depth: 3

CodeIgniter's Approach to Transactions
======================================
Expand Down Expand Up @@ -52,16 +52,33 @@ or failure of any given query.
Strict Mode
===========

By default, CodeIgniter runs all transactions in Strict Mode. When strict
mode is enabled, if you are running multiple groups of transactions, if
one group fails all subsequent groups will be rolled back. If strict mode is
disabled, each group is treated independently, meaning a failure of one
group will not affect any others.
By default, CodeIgniter runs all transactions in Strict Mode.

When strict mode is enabled, if you are running multiple groups of transactions,
if one group fails all subsequent groups will be rolled back.

If strict mode is disabled, each group is treated independently, meaning a failure
of one group will not affect any others.

Strict Mode can be disabled as follows:

.. literalinclude:: transactions/002.php

.. _transactions-resetting-transaction-status:

Resetting Transaction Status
----------------------------

.. versionadded:: 4.6.0

When strict mode is enabled, if one transaction fails, all subsequent transactions
will be rolled back.

If you wan to restart transactions after a failure, you can reset the transaction
status:

.. literalinclude:: transactions/009.php

.. _transactions-managing-errors:

Managing Errors
Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/database/transactions/009.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$this->db->resetTransStatus();

0 comments on commit 3dffbc9

Please sign in to comment.