Skip to content

Bug: Unshared DB instance affects subsequent shared DB instances #9678

@paulbalandan

Description

@paulbalandan

PHP Version

8.4

CodeIgniter4 Version

develop, 4.7

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli

Database

SQLite3

What happened?

When you retrieve a non-shared DB instance via Database::connect(getShared: false) and then modifying that instance, all subsequent shared instances would use that non-shared instance if given the same DB group.

Steps to Reproduce

<?php

namespace App\Controllers;

use CodeIgniter\Test\ReflectionHelper;
use Config\Database;

class Home extends BaseController
{
    use ReflectionHelper;

    public function index(): string
    {
        $sharedDb1 = Database::connect('tests');

        $unsharedDb = Database::connect('tests', false);
        self::setPrivateProperty($unsharedDb, 'DBDebug', false);

        $sharedDb2 = Database::connect('tests');

        return sprintf(
            '<pre>
                <table border="1" cellpadding="4" cellspacing="0">
                    <tr>
                        <th>Connection</th>
                        <th>Debug Type</th>
                    </tr>
                    <tr>
                        <td>Shared DB 1</td>
                        <td>%s</td>
                    </tr>
                    <tr>
                        <td>Unshared DB</td>
                        <td>%s</td>
                    </tr>
                    <tr>
                        <td>Shared DB 2</td>
                        <td>%s</td>
                    </tr>
                </table>',
            var_export(self::getPrivateProperty($sharedDb1, 'DBDebug'), true),
            var_export(self::getPrivateProperty($unsharedDb, 'DBDebug'), true),
            var_export(self::getPrivateProperty($sharedDb2, 'DBDebug'), true),
        );
    }
}

Running php spark serve produces:

    
Connection Debug Type
Shared DB 1 true
Unshared DB false
Shared DB 2 false

Expected Output

I expect $sharedDb1 and $sharedDb2 to have the same DBDebug value as they are "shared".

    
Connection Debug Type
Shared DB 1 true
Unshared DB false
Shared DB 2 true

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions