-
Notifications
You must be signed in to change notification settings - Fork 1.9k
docs: update DB transactions #6886
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9f8c846
docs: update description
kenjis 43c1803
test: add DB transaction tests
kenjis e18cb8d
test: add tests for transStrict()
kenjis 41fd753
test: add assertions
kenjis 84d2b11
test: fix setUp()
kenjis 3d3bf64
docs: add notes for DBDebug
kenjis f9658b8
docs: add comment
kenjis 58cfbd1
docs: fix typo
kenjis 821913c
docs: add about DB transactions in changelog
kenjis 523d14e
test: add assertion
kenjis 338345f
docs: fix description
kenjis 5d6a907
docs: add exception class name
kenjis b2e90d7
docs: add sample code when DBDebug is true
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Database\Live; | ||
|
||
use CodeIgniter\Database\Exceptions\DatabaseException; | ||
use CodeIgniter\Test\CIUnitTestCase; | ||
use CodeIgniter\Test\DatabaseTestTrait; | ||
use Config\Database; | ||
use Tests\Support\Database\Seeds\CITestSeeder; | ||
|
||
/** | ||
* @group DatabaseLive | ||
* | ||
* @internal | ||
*/ | ||
final class TransactionTest extends CIUnitTestCase | ||
{ | ||
use DatabaseTestTrait; | ||
|
||
protected $refresh = true; | ||
protected $seed = CITestSeeder::class; | ||
|
||
protected function setUp(): void | ||
{ | ||
// Reset connection instance. | ||
$this->db = Database::connect($this->DBGroup, false); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* Sets $DBDebug to false. | ||
* | ||
* WARNING: this value will persist! take care to roll it back. | ||
*/ | ||
protected function disableDBDebug(): void | ||
{ | ||
$this->setPrivateProperty($this->db, 'DBDebug', false); | ||
} | ||
|
||
/** | ||
* Sets $DBDebug to true. | ||
*/ | ||
protected function enableDBDebug(): void | ||
{ | ||
$this->setPrivateProperty($this->db, 'DBDebug', true); | ||
} | ||
|
||
public function testTransStartDBDebugTrue() | ||
{ | ||
$builder = $this->db->table('job'); | ||
$e = null; | ||
|
||
try { | ||
$this->db->transStart(); | ||
|
||
$jobData = [ | ||
'name' => 'Grocery Sales', | ||
'description' => 'Discount!', | ||
]; | ||
$builder->insert($jobData); | ||
|
||
// Duplicate entry '1' for key 'PRIMARY' | ||
$jobData = [ | ||
'id' => 1, | ||
'name' => 'Comedian', | ||
'description' => 'Theres something in your teeth', | ||
]; | ||
$builder->insert($jobData); | ||
|
||
$this->db->transComplete(); | ||
} catch (DatabaseException $e) { | ||
// Do nothing. | ||
} | ||
|
||
$this->assertInstanceOf(DatabaseException::class, $e); | ||
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']); | ||
} | ||
|
||
public function testTransStartDBDebugFalse() | ||
{ | ||
$this->disableDBDebug(); | ||
|
||
$builder = $this->db->table('job'); | ||
|
||
$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); | ||
|
||
kenjis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->assertFalse($this->db->transStatus()); | ||
|
||
$this->db->transComplete(); | ||
|
||
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']); | ||
|
||
$this->enableDBDebug(); | ||
} | ||
|
||
public function testTransStrictTrueAndDBDebugFalse() | ||
{ | ||
$this->disableDBDebug(); | ||
|
||
$builder = $this->db->table('job'); | ||
|
||
// The first transaction group | ||
$this->db->transStart(); | ||
|
||
$jobData = [ | ||
'name' => 'Grocery Sales', | ||
'description' => 'Discount!', | ||
]; | ||
$builder->insert($jobData); | ||
|
||
kenjis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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); | ||
|
||
kenjis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->assertFalse($this->db->transStatus()); | ||
|
||
$this->db->transComplete(); | ||
|
||
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']); | ||
|
||
// The second transaction group | ||
$this->db->transStart(); | ||
|
||
$jobData = [ | ||
'name' => 'Comedian', | ||
'description' => 'Theres something in your teeth', | ||
]; | ||
$builder->insert($jobData); | ||
|
||
kenjis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->assertFalse($this->db->transStatus()); | ||
|
||
$this->db->transComplete(); | ||
|
||
$this->dontSeeInDatabase('job', ['name' => 'Comedian']); | ||
|
||
$this->enableDBDebug(); | ||
} | ||
|
||
public function testTransStrictFalseAndDBDebugFalse() | ||
{ | ||
$this->disableDBDebug(); | ||
|
||
$builder = $this->db->table('job'); | ||
|
||
$this->db->transStrict(false); | ||
|
||
// The first transaction group | ||
$this->db->transStart(); | ||
|
||
$jobData = [ | ||
'name' => 'Grocery Sales', | ||
'description' => 'Discount!', | ||
]; | ||
$builder->insert($jobData); | ||
|
||
kenjis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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); | ||
|
||
kenjis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->assertFalse($this->db->transStatus()); | ||
|
||
$this->db->transComplete(); | ||
|
||
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']); | ||
|
||
// The second transaction group | ||
$this->db->transStart(); | ||
|
||
$jobData = [ | ||
'name' => 'Comedian', | ||
'description' => 'Theres something in your teeth', | ||
]; | ||
$builder->insert($jobData); | ||
|
||
kenjis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->assertTrue($this->db->transStatus()); | ||
|
||
$this->db->transComplete(); | ||
|
||
$this->seeInDatabase('job', ['name' => 'Comedian']); | ||
|
||
$this->enableDBDebug(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
// When DBDebug in the Database Config is true. | ||
|
||
use CodeIgniter\Database\Exceptions\DatabaseException; | ||
|
||
try { | ||
$this->db->transStart(); | ||
$this->db->query('AN SQL QUERY...'); | ||
$this->db->query('ANOTHER QUERY...'); | ||
$this->db->query('AND YET ANOTHER QUERY...'); | ||
$this->db->transComplete(); | ||
} catch (DatabaseException $e) { | ||
// Automatically rolled back already. | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still feel uncomfortable with this code. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.