Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add BaseConnection::resetTransStatus() #8767

Merged
merged 4 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -134,6 +134,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();
Loading