Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/scripts/random-tests-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Config
Cookie
# DataCaster
# DataConverter
# Database
Database
# Debug
Email
# Encryption
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/test-random-execution.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ jobs:
- Postgre
- SQLSRV
- SQLite3
- Oracle
# Oracle is excluded: OCI8 uses a single shared schema via DSN
# and cannot be isolated with per-component databases, causing
# ORA-00955 collisions when components run in parallel.
# - Oracle

services:
mysql:
Expand Down Expand Up @@ -177,7 +180,7 @@ jobs:
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # 2.37.2
with:
php-version: ${{ matrix.php-version }}
extensions: gd, curl, iconv, json, mbstring, openssl, sodium
extensions: gd, curl, iconv, json, mbstring, openssl, sodium, mysqli, oci8, pgsql, sqlsrv, sqlite3
ini-values: opcache.enable_cli=0
coverage: none

Expand Down
21 changes: 17 additions & 4 deletions system/Database/OCI8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use ErrorException;
use stdClass;

defined('OCI_COMMIT_ON_SUCCESS') || define('OCI_COMMIT_ON_SUCCESS', 32);

/**
* Connection for OCI8
*
Expand Down Expand Up @@ -150,6 +152,17 @@ public function connect(bool $persistent = false)
: $func($this->username, $this->password, $this->DSN, $this->charset);
}

public function initialize()
{
parent::initialize();

if ($this->connID) {
$this->simpleQuery("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'");
$this->simpleQuery("ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS'");
$this->simpleQuery("ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS'");
}
}

/**
* Close the database connection.
*
Expand Down Expand Up @@ -288,11 +301,11 @@ protected function _listTables(bool $prefixLimit = false, ?string $tableName = n
$sql = 'SELECT "TABLE_NAME" FROM "USER_TABLES"';

if ($tableName !== null) {
return $sql . ' WHERE "TABLE_NAME" LIKE ' . $this->escape($tableName);
return $sql . ' WHERE "TABLE_NAME" LIKE ' . $this->escape(strtoupper($tableName));
}

if ($prefixLimit && $this->DBPrefix !== '') {
return $sql . ' WHERE "TABLE_NAME" LIKE \'' . $this->escapeLikeString($this->DBPrefix) . "%' "
return $sql . ' WHERE "TABLE_NAME" LIKE \'' . $this->escapeLikeString(strtoupper($this->DBPrefix)) . "%' "
. sprintf($this->likeEscapeStr, $this->likeEscapeChar);
}

Expand Down Expand Up @@ -397,7 +410,7 @@ protected function _indexData(string $table): array
$sql = 'SELECT AIC.INDEX_NAME, UC.CONSTRAINT_TYPE, AIC.COLUMN_NAME '
. ' FROM ALL_IND_COLUMNS AIC '
. ' LEFT JOIN USER_CONSTRAINTS UC ON AIC.INDEX_NAME = UC.CONSTRAINT_NAME AND AIC.TABLE_NAME = UC.TABLE_NAME '
. 'WHERE AIC.TABLE_NAME = ' . $this->escape(strtolower($table)) . ' '
. 'WHERE AIC.TABLE_NAME = ' . $this->escape(strtoupper($table)) . ' '
. 'AND AIC.TABLE_OWNER = ' . $this->escape(strtoupper($owner)) . ' '
. ' ORDER BY UC.CONSTRAINT_TYPE, AIC.COLUMN_POSITION';

Expand All @@ -422,7 +435,7 @@ protected function _indexData(string $table): array
$retVal[$row->INDEX_NAME] = new stdClass();
$retVal[$row->INDEX_NAME]->name = $row->INDEX_NAME;
$retVal[$row->INDEX_NAME]->fields = [$row->COLUMN_NAME];
$retVal[$row->INDEX_NAME]->type = $constraintTypes[$row->CONSTRAINT_TYPE] ?? 'INDEX';
$retVal[$row->INDEX_NAME]->type = $constraintTypes[$row->CONSTRAINT_TYPE ?? ''] ?? 'INDEX';
}

return $retVal;
Expand Down
20 changes: 18 additions & 2 deletions system/Database/Postgre/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PgSql\Result as PgSqlResult;
use stdClass;
use Stringable;
use Throwable;

/**
* Connection for Postgre
Expand Down Expand Up @@ -149,15 +150,30 @@ private function convertDSN()
*/
protected function _close()
{
pg_close($this->connID);
if ($this->connID !== false) {
try {
pg_close($this->connID);
} catch (Throwable) {
} finally {
$this->connID = false;
}
}
}

/**
* Ping the database connection.
*/
protected function _ping(): bool
{
return pg_ping($this->connID);
if ($this->connID === false) {
return false;
}

try {
return pg_ping($this->connID);
} catch (Throwable) {
return false;
}
}

/**
Expand Down
67 changes: 66 additions & 1 deletion tests/_support/Config/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

namespace Tests\Support\Config;

use mysqli;
use PDO;
use Throwable;

/**
* Class Registrar
*
Expand Down Expand Up @@ -137,7 +141,68 @@ public static function Database(): array
// so that we can test against multiple databases.
$group = env('DB', 'SQLite3');

$config['tests'] = self::$dbConfig[$group] ?? [];
if ($group === 'Oracle') {
$group = 'OCI8';
}

$dbParams = self::$dbConfig[$group] ?? [];

if (! empty($dbParams) && ! in_array($group, ['SQLite3', 'OCI8'], true)) {
$componentName = '';

foreach ($_SERVER['argv'] ?? [] as $arg) {
if (str_contains($arg, 'tests/system/')) {
$parts = explode('tests/system/', $arg);
if (isset($parts[1])) {
$componentName = explode('/', $parts[1])[0];
break;
}
}
}

if ($componentName !== '') {
$dbParams['database'] = 'test_' . strtolower($componentName);

try {
if ($group === 'MySQLi') {
$conn = new mysqli(
$dbParams['hostname'],
$dbParams['username'],
$dbParams['password'],
'',
(int) $dbParams['port'],
);
if (! $conn->connect_error) {
$conn->query('CREATE DATABASE IF NOT EXISTS ' . $conn->real_escape_string($dbParams['database']));
$conn->close();
}
} elseif ($group === 'Postgre') {
$dsn = 'pgsql:host=' . $dbParams['hostname'] . ';port=' . $dbParams['port'] . ';user=' . $dbParams['username'] . ';password=' . $dbParams['password'];
$pdo = new PDO($dsn);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('SELECT 1 FROM pg_database WHERE datname = ?');
$stmt->execute([$dbParams['database']]);
if (! $stmt->fetchColumn()) {
$dbName = str_replace('"', '""', $dbParams['database']);
$pdo->exec('CREATE DATABASE "' . $dbName . '"');
}
} elseif ($group === 'SQLSRV') {
$dsn = 'sqlsrv:Server=' . $dbParams['hostname'] . ',' . $dbParams['port'] . ';Encrypt=False;TrustServerCertificate=True';
$pdo = new PDO($dsn, $dbParams['username'], $dbParams['password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('SELECT 1 FROM sys.databases WHERE name = ?');
$stmt->execute([$dbParams['database']]);
if (! $stmt->fetchColumn()) {
$pdo->exec('CREATE DATABASE [' . str_replace(']', ']]', $dbParams['database']) . '] COLLATE Latin1_General_100_CS_AS_SC_UTF8');
}
}
} catch (Throwable) {
// Ignore any error and let the connection fail naturally
}
}
}

$config['tests'] = $dbParams;

return $config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Tests\Support\Database\Migrations;

use CodeIgniter\Database\Migration;
use Throwable;

class Migration_Create_test_tables extends Migration
{
Expand Down Expand Up @@ -183,6 +184,7 @@ public function down(): void
$this->forge->dropTable('user', true);
$this->forge->dropTable('job', true);
$this->forge->dropTable('misc', true);
$this->forge->dropTable('team_members', true);
$this->forge->dropTable('type_test', true);
$this->forge->dropTable('empty', true);
$this->forge->dropTable('secondary', true);
Expand All @@ -196,9 +198,25 @@ public function down(): void
}

if ($this->db->DBDriver === 'OCI8') {
$this->db->query('DROP PROCEDURE one');
$this->db->query('DROP PROCEDURE plus');
$this->db->query('DROP PACKAGE BODY calculator');
try {
$this->db->query('DROP PROCEDURE one');
} catch (Throwable) {
}

try {
$this->db->query('DROP PROCEDURE plus');
} catch (Throwable) {
}

try {
$this->db->query('DROP PACKAGE BODY calculator');
} catch (Throwable) {
}

try {
$this->db->query('DROP PACKAGE calculator');
} catch (Throwable) {
}
}
}
}
15 changes: 12 additions & 3 deletions tests/system/Database/Live/ConnectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,28 @@ protected function setUp(): void
$this->group2['DBDriver'] = 'Postgre';
}

protected function tearDown(): void
{
parent::tearDown();
$this->setPrivateProperty(Database::class, 'instances', []);
}

public function testConnectWithMultipleCustomGroups(): void
{
$this->group1['DBPrefix'] = uniqid('g1_', true);
$this->group2['DBPrefix'] = uniqid('g2_', true);

// We should have our test database connection already.
$instances = $this->getPrivateProperty(Database::class, 'instances');
$this->assertCount(1, $instances);
$instances = $this->getPrivateProperty(Database::class, 'instances');
$initialCount = count($instances);

$db1 = Database::connect($this->group1);
$db2 = Database::connect($this->group2);

$this->assertNotSame($db1, $db2);

$instances = $this->getPrivateProperty(Database::class, 'instances');
$this->assertCount(3, $instances);
$this->assertCount($initialCount + 2, $instances);
}

public function testConnectReturnsProvidedConnection(): void
Expand Down
15 changes: 11 additions & 4 deletions tests/system/Database/Live/ExecuteLogMessageFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testLogMessageWhenExecuteFailsShowFullStructuredBacktrace(): voi
$db->query($sql, [3, 'live', 'Rick']);

$pattern = match ($db->DBDriver) {
'MySQLi' => '/Table \'test\.some_table\' doesn\'t exist/',
'MySQLi' => '/Table \'' . preg_quote($db->database, '/') . '\.some_table\' doesn\'t exist/',
'Postgre' => '/pg_query\(\): Query failed: ERROR: relation "some_table" does not exist/',
'SQLite3' => '/Unable to prepare statement:\s(\d+,\s)?no such table: some_table/',
'OCI8' => '/oci_execute\(\): ORA-00942: table or view "ORACLE"\."SOME_TABLE" does not exist/',
Expand All @@ -60,11 +60,18 @@ public function testLogMessageWhenExecuteFailsShowFullStructuredBacktrace(): voi

if ($db->DBDriver === 'Postgre') {
$messageFromLogs = array_slice($messageFromLogs, 2);
} elseif ($db->DBDriver === 'OCI8') {
$messageFromLogs = array_slice($messageFromLogs, 1);
}

$this->assertMatchesRegularExpression('/^in \S+ on line \d+\.$/', array_shift($messageFromLogs));
$inLine = null;

while (($line = array_shift($messageFromLogs)) !== null) {
if (preg_match('/^in \S+ on line \d+\.$/', $line)) {
$inLine = $line;
break;
}
}

$this->assertNotNull($inLine, 'Could not find "in ... on line ..." in log message');

foreach ($messageFromLogs as $line) {
$this->assertMatchesRegularExpression('/^\s*\d* .+(?:\(\d+\))?: \S+(?:(?:\->|::)\S+)?\(.*\)$/', $line);
Expand Down
Loading
Loading