Skip to content

test(Database): fix state leaks and random-order test determinism for MySQLi, PostgreSQL, and OCI8#10377

Open
gr8man wants to merge 6 commits into
codeigniter4:developfrom
gr8man:fix-postgres-tests
Open

test(Database): fix state leaks and random-order test determinism for MySQLi, PostgreSQL, and OCI8#10377
gr8man wants to merge 6 commits into
codeigniter4:developfrom
gr8man:fix-postgres-tests

Conversation

@gr8man

@gr8man gr8man commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Fixes database tests to be deterministic under random execution order
for MySQLi, PostgreSQL, and OCI8 drivers.
Ref: #9968

MySQLi (commits cd8f5dc, 8cc20d3)

  • ConnectTest: use correct host/port for connection assertions.
  • ForgeTest: access addField before usage in testDropTableSuccess.
  • WorkerModeTest: clear history between runs.
  • FoundRowsTest, NumberNativeTest: remove setPrefix() calls that leaked
    prefix state to subsequent tests.

PostgreSQL (commit 24d5a44)

  • ForgeTest::testDropTableSuccess: verify table existence with correct
    table name after creation.
  • ConnectTest: skip charset assertion when not applicable.

OCI8 (commit fc9e325)

  • Connection::initialize() – sets NLS_DATE_FORMAT, NLS_TIMESTAMP_FORMAT,
    and NLS_TIMESTAMP_TZ_FORMAT via ALTER SESSION after connecting.
    Prevents ORA-01843 "invalid month" when NLS env vars are not set.
  • _indexData() – wraps $row->CONSTRAINT_TYPE ?? '' to avoid PHP 8.1+
    deprecation "Using null as an array offset" for indexes without a
    matching constraint.
  • MetadataTest – captures prefix before createExtraneousTable() so the
    original prefix is preserved even if an exception is thrown.
  • ExecuteLogMessageFormatTest – removes stale OCI8-specific array_slice
    that caused a backtrace line pattern mismatch.

All 859 OCI8 database tests pass with 0 failures in both default and
randomised execution order. MySQLi and PostgreSQL tests also pass
deterministically under random order.

gr8man added 5 commits July 4, 2026 23:02
- Connection::initialize() sets NLS session formats via ALTER SESSION,
  preventing ORA-01843 when env vars are not set.
- _indexData() handles null CONSTRAINT_TYPE to avoid deprecated null
  array offset error.
- MetadataTest captures prefix before createExtraneousTable() to prevent
  prefix leak on exception.
- ExecuteLogMessageFormatTest removes stale OCI8-specific array_slice.
@gr8man

gr8man commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

IMPORTANT

Note to reviewers and developers: Please ensure that tests are run regularly with the --order-by=random flag during development. Identifying test pollution and global state leakage bugs at a later stage is extremely difficult and time-consuming, as these failures only manifest under very specific test suite execution orders.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves determinism of the database test suite when executed in randomized order by removing shared connection state leaks (notably DB prefix/config caching), adding stronger cleanup between tests, and hardening OCI8 behavior/configuration to avoid environment-dependent failures.

Changes:

  • Avoid shared DB connection/config state in several live driver tests by connecting with getShared = false and resetting cached instances where needed.
  • Add/adjust cleanup to prevent cross-test state leakage (table cleanup, cache resets, preserved prefixes).
  • OCI8: set session NLS date/timestamp formats during initialization and harden index metadata handling to avoid PHP 8.1+ deprecations.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/system/Database/Migrations/MigrationRunnerTest.php Adjusts teardown cleanup for migration runner tests
tests/system/Database/Live/WorkerModeTest.php Minor teardown formatting cleanup
tests/system/Database/Live/Postgre/ConnectTest.php Uses non-shared connection to avoid state leakage during charset test
tests/system/Database/Live/MySQLi/NumberNativeTest.php Uses non-shared connections to avoid config/prefix bleed between tests
tests/system/Database/Live/MySQLi/FoundRowsTest.php Uses non-shared connections to avoid config/prefix bleed between tests
tests/system/Database/Live/MetadataTest.php Preserves original prefix reliably even on exceptions
tests/system/Database/Live/GetVersionTest.php Relaxes version assertion pattern
tests/system/Database/Live/ForgeTest.php Centralizes mock-table cleanup and improves DB existence handling in tests
tests/system/Database/Live/ExecuteLogMessageFormatTest.php Makes backtrace line detection more robust across drivers
tests/system/Database/Live/ConnectTest.php Resets cached Database instances; makes instance-count assertion resilient
system/Database/OCI8/Connection.php Sets OCI8 session NLS formats and hardens index metadata mapping

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 68 to 73
protected function tearDown(): void
{
parent::tearDown();

// To delete data with `$this->regressDatabase()`, set it true.
$this->migrate = true;
$this->regressDatabase();
$this->resetTables();
}
Comment on lines +79 to +84
protected function tearDown(): void
{
parent::tearDown();
$this->dropAllMockTables();
self::$doneMigration = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants