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
8 changes: 7 additions & 1 deletion system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,13 @@ public function listTables(bool $constrainByPrefix = false)
public function tableExists(string $tableName, bool $cached = true): bool
{
if ($cached) {
return in_array($this->protectIdentifiers($tableName, true, false, false), $this->listTables(), true);
$tableName = $this->protectIdentifiers($tableName, true, false, false);

return in_array(
strtolower($tableName),
array_map(strtolower(...), $this->listTables()),
true,
);
}

if (false === ($sql = $this->_listTables(false, $tableName))) {
Expand Down
12 changes: 10 additions & 2 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,16 @@ public function createTable(string $table, bool $ifNotExists = false, array $att
$sql = $this->_createTable($table, false, $attributes);

if (($result = $this->db->query($sql)) !== false) {
if (isset($this->db->dataCache['table_names']) && ! in_array($table, $this->db->dataCache['table_names'], true)) {
$this->db->dataCache['table_names'][] = $table;
if (isset($this->db->dataCache['table_names'])) {
$exists = in_array(
strtolower($table),
array_map(strtolower(...), $this->db->dataCache['table_names']),
true,
);

if (! $exists) {
$this->db->dataCache['table_names'][] = $table;
}
}

// Most databases don't support creating indexes from within the CREATE TABLE statement
Expand Down
24 changes: 21 additions & 3 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 @@ -155,6 +157,22 @@ 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.
*
* @return void
*/
protected function _close()
{
if (is_resource($this->cursorId)) {
Expand Down Expand Up @@ -264,7 +282,7 @@ public function parseInsertTableName(string $sql): string
return '';
}

preg_match('/(?is)\b(?:into)\s+("?\w+"?)/', $commentStrippedSql, $match);
preg_match('/(?is)\b(?:into)\s+(?:\"?\w+\"?\.)?(\"?\w+\"?)/', $commentStrippedSql, $match);
$tableName = $match[1] ?? '';

return str_starts_with($tableName, '"') ? trim($tableName, '"') : strtoupper($tableName);
Expand Down Expand Up @@ -404,7 +422,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 Expand Up @@ -620,7 +638,7 @@ public function insertID(): int
}

$primaryColumnName = $this->protectIdentifiers($index->fields[0], false, false);
$primaryColumnType = $columnTypeList[$primaryColumnName];
$primaryColumnType = $columnTypeList[$primaryColumnName] ?? $columnTypeList[strtoupper($primaryColumnName)] ?? null;

if ($primaryColumnType !== 'NUMBER') {
$primaryColumnName = '';
Expand Down
18 changes: 15 additions & 3 deletions system/Database/Postgre/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,25 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
$constraints = $this->QBOptions['constraints'] ?? [];

if ($constraints === []) {
$allIndexes = array_filter($this->db->getIndexData($table), static function ($index) use ($fieldNames): bool {
$tableIndexes = $this->db->getIndexData($table);

$uniqueIndexes = array_filter($tableIndexes, static function ($index) use ($fieldNames): bool {
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);

return ($index->type === 'UNIQUE' || $index->type === 'PRIMARY') && $hasAllFields;
return $index->type === 'PRIMARY' && $hasAllFields;
});

foreach ($allIndexes as $index) {
// if no primary found then look for unique - since indexes have no order
if ($uniqueIndexes === []) {
$uniqueIndexes = array_filter($tableIndexes, static function ($index) use ($fieldNames): bool {
$hasAllFields = count(array_intersect($index->fields, $fieldNames)) === count($index->fields);

return $index->type === 'UNIQUE' && $hasAllFields;
});
}

// only take first index
foreach ($uniqueIndexes as $index) {
$constraints = $index->fields;
break;
}
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 @@ -159,15 +160,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) {
}
}
}
}
14 changes: 14 additions & 0 deletions tests/system/Database/BaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,18 @@ protected function getDriverFunctionPrefix(): string

$this->assertTrue($db->callFunction('contains', 'CodeIgniter', 'Ignite'));
}

public function testTableExistsIsCaseInsensitiveForCachedTables(): void
{
$db = new class ($this->options) extends MockConnection {
public function listTables(bool $constrainByPrefix = false): array
{
return ['test_USER', 'test_JOB'];
}
};

$this->assertTrue($db->tableExists('user', true));
$this->assertTrue($db->tableExists('USER', true));
$this->assertTrue($db->tableExists('test_user', true));
}
}
15 changes: 12 additions & 3 deletions tests/system/Database/Live/ConnectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,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
Loading
Loading