Skip to content

Fix CreateDatabase leaving altered database config in connection #6856

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

Conversation

paulbalandan
Copy link
Member

Description
When running vendor/bin/phpunit --group DatabaseLive either on GA or local, MigrationRunnerTest fails:

On GA:

PHPUnit 9.5.26 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.32
Configuration: /home/runner/work/CodeIgniter4/CodeIgniter4/phpunit.xml.dist

..S............................................................  63 / 516 ( 12%)
............S.S...S...........S................................ 126 / 516 ( 24%)
...................................................SSSSSSS..... 189 / 516 ( 36%)
............................................................... 252 / 516 ( 48%)
.............F................................................. 315 / 516 ( 61%)
............................................................... 378 / 516 ( 73%)
............................................................... 441 / 516 ( 85%)
..........SSSSSSSSSSSS......................................... 504 / 516 ( 97%)
............                                                    516 / 516 (100%)

Time: 00:[20](https://github.com/codeigniter4/CodeIgniter4/actions/runs/3456144117/jobs/5768709007#step:11:21).493, Memory: 102.00 MB

There was 1 failure:

1) CodeIgniter\Database\Migrations\MigrationRunnerTest::testLoadsDefaultDatabaseWhenNoneSpecified
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'/home/runner/work/CodeIgniter4/CodeIgniter4/writable/database.db'
+'/home/runner/work/CodeIgniter4/CodeIgniter4/writable/foobar.db'

/home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Database/Migrations/MigrationRunnerTest.php:89
phpvfscomposer:///home/runner/work/CodeIgniter4/CodeIgniter4/vendor/phpunit/phpunit/phpunit:97

On local: (forgive the irrelevant failures)

PHPUnit 9.5.26 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.12
Configuration: C:\Users\P\Desktop\Web Dev\CodeIgniter4\phpunit.xml.dist

..S.....FFFF.FFFF..........................................................S.S...S...........S.................. 112 / 516 ( 21%)
.................................................................SSSSSSS........................................ 224 / 516 ( 43%)
.........................................F...................................................................... 336 / 516 ( 65%)
................................................................................................................ 448 / 516 ( 86%)
...SSSSSSSSSSSSSSSSSSS..............................................                                             516 / 516 (100%)

Nexus\PHPUnit\Extension\Tachycardia identified this sole slow test:
⚠  Took 1.86s from 0.50s limit to run CodeIgniter\\Database\\Live\\GroupTest::testOrNotHavingLike


Time: 01:20.643, Memory: 108.00 MB

There were 9 failures:

...

9) CodeIgniter\Database\Migrations\MigrationRunnerTest::testLoadsDefaultDatabaseWhenNoneSpecified
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-':memory:'
+'C:\Users\P\Desktop\Web Dev\CodeIgniter4\writable\foobar.db'

After this PR:
In local:

PHPUnit 9.5.26 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.12
Configuration: C:\Users\P\Desktop\Web Dev\CodeIgniter4\phpunit.xml.dist

..S.....FFFF.FFFF..........................................................S.S...S...........S.................. 112 / 516 ( 21%)
.................................................................SSSSSSS........................................ 224 / 516 ( 43%)
................................................................................................................ 336 / 516 ( 65%)
................................................................................................................ 448 / 516 ( 86%)
...SSSSSSSSSSSSSSSSSSS..............................................                                             516 / 516 (100%)

Time: 00:13.382, Memory: 108.00 MB

The reason for this is that all database live tests uses the shared DB connection (from Database::connect()). When CreateDatabaseTest runs for SQLite3, the tests alter the database to use foobar.db in the Config\Database instance. On cleanup, the altered config is reset. However, the protected Database::$instances array contains the shared DB connection which still uses the altered config. Running Database::connect(null, false) will effectively flush the old shared connection and create a new one using the fresh config.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@paulbalandan paulbalandan added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 14, 2022
@kenjis kenjis added testing Pull requests that changes tests only and removed testing Pull requests that changes tests only labels Nov 14, 2022
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

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

Thanks for the investigation and thorough explanation! Glad to see one more of these local-versus-pipeline mysteries cleared up.

@paulbalandan paulbalandan merged commit e33a00e into codeigniter4:develop Nov 14, 2022
@paulbalandan paulbalandan deleted the refactor-create-db-test branch November 14, 2022 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants