-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add structured trace for all DB drivers
- Loading branch information
1 parent
427eeeb
commit 196c605
Showing
7 changed files
with
129 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
82 changes: 82 additions & 0 deletions
82
tests/system/Database/Live/ExecuteLogMessageFormatTest.php
This file contains 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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* 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\Test\CIUnitTestCase; | ||
use CodeIgniter\Test\DatabaseTestTrait; | ||
use CodeIgniter\Test\TestLogger; | ||
use Config\Database; | ||
use PHPUnit\Framework\Attributes\Group; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[Group('DatabaseLive')] | ||
final class ExecuteLogMessageFormatTest extends CIUnitTestCase | ||
{ | ||
use DatabaseTestTrait; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$utils = Database::utils(); | ||
$forge = Database::forge(); | ||
|
||
foreach (['foobar', 'test_com.sitedb.web'] as $database) { | ||
if ($utils->databaseExists($database)) { | ||
$forge->dropDatabase($database); | ||
} | ||
} | ||
|
||
$this->disableDBDebug(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->enableDBDebug(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testLogMessageWhenExecuteFailsShowFullStructuredBacktrace(): void | ||
{ | ||
$sql = 'SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?'; | ||
$this->db->query($sql, [3, 'live', 'Rick']); | ||
|
||
$message = match ($this->db->DBDriver) { | ||
'MySQLi' => 'Table \'test.some_table\' doesn\'t exist', | ||
'Postgre' => 'pg_query(): Query failed: ERROR: relation "some_table" does not exist', | ||
'SQLite3' => 'Unable to prepare statement: no such table: some_table', | ||
'OCI8' => 'oci_execute(): ORA-00942: table or view does not exist', | ||
'SQLSRV' => '[Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Invalid object name \'some_table\'. SQLSTATE: 42S02, code: 208', | ||
default => 'Unknown DB error', | ||
}; | ||
$messageFromLogs = explode("\n", $this->getPrivateProperty(TestLogger::class, 'op_logs')[0]['message']); | ||
|
||
$this->assertSame($message, array_shift($messageFromLogs)); | ||
|
||
if ($this->db->DBDriver === 'Postgre') { | ||
$messageFromLogs = array_slice($messageFromLogs, 2); | ||
} elseif ($this->db->DBDriver === 'OCI8') { | ||
$messageFromLogs = array_slice($messageFromLogs, 1); | ||
} | ||
|
||
$this->assertMatchesRegularExpression('/^in \S+ on line \d+\.$/', array_shift($messageFromLogs)); | ||
|
||
foreach ($messageFromLogs as $line) { | ||
$this->assertMatchesRegularExpression('/^\s*\d* .+(?:\(\d+\))?: \S+(?:(?:\->|::)\S+)?\(.*\)$/', $line); | ||
} | ||
} | ||
} |
This file contains 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