Currently, class Drupal\Core\Database\Database has a lot of static stuff going on.
Initialized database connections, the pool of connection info arrays, and some meta stuff are all managed as static properties, and accessed via static methods.

As part of #2354475: [meta] Refactor the installer, (multi)site management, and pre-container bootstrap, I would like to turn the entire thing into a service, managed in the newly introduced CoreContainer.
This means, we need to have an actual object behind the static methods.
We still need to keep the static methods around for BC, but the logic needs to happen in the object.

Proposed solution

Phase 1

  1. Introduce class DatabaseManager, with real object methods for all the static methods of class Database.
  2. Have a protected Database::$databaseManager instanceof DatabaseManager.
    For phase 1, this will be lazy-instantiated the first time it is needed.
  3. Let the static methods of class Database call the respective method in self::$databaseManager.
    Only parseConnectionInfo() will remain static, because it's a pure function.

Phase 2

  1. Introduce class ConnectionInfo to wrap the connection info for a single database.
    Methods will be getDriverClass() and openConnection(), and also toArray() for BC.
  2. Optionally, introduce class ConnectionInfoPool to wrap the connection info of all databases.
    The benefit is limited because all the manipulators (e.g. renameConnection()) still need to touch both the ConnectionInfoPool and the DatabaseManager.

So far, the changes are all invisible to the public API. In general, components are still forced to use the static methods like Database::getConnection(). The DatabaseManager object is not exposed.

However, unit tests can now test the DatabaseManager, ConnectionInfo and ConnectionInfoPool directly, without having to mess with the global state of the static properties. This is already a benefit.

Phase 3 (follow-up)

This phase will only happen when the CoreContainer / MiniContainer is introduced, as proposed in #2354475: [meta] Refactor the installer, (multi)site management, and pre-container bootstrap

  1. Introduce the CoreContainer / MiniContainer architecture (other issue).
    This also brings a mechanic to initialize global state.
  2. Introduce the core service $coreContainer->DatabaseManager.
  3. Replace the lazy instantiation of Database::$databaseManager with an explicit Database::setDatabaseManager(), to be called from a CoreContainer mechanic.
  4. Refactor unit tests, so that methods like Database::renameConnection() are no longer needed.

I think this last phase is what will make the changes worthwhile.
But the prior phases 1 and 2 are a good preparation, and I think it is a good idea to keep them out of the CoreContainer scope.

Comments

The last submitted patch, 2: D8-2363315-2-DatabaseManager-002.patch, failed testing.

The last submitted patch, 2: D8-2363315-2-DatabaseManager-003.patch, failed testing.

The last submitted patch, 2: D8-2363315-2-DatabaseManager-004.patch, failed testing.

The last submitted patch, 2: D8-2363315-2-DatabaseManager-005.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 2: D8-2363315-2-DatabaseManager-006.patch, failed testing.

donquixote’s picture

I made the patches with format-patch like this:
git format-patch --stdout 8.0.x..cd1995a > ***/D8-2363315-2-DatabaseManager-005.patch

Unfortunately this does not seem to work except for the first case. So I'm doing it again, this time with git diff.
Please look at the github link for interdiffs / the individual commits.
https://github.com/donquixote/drupal/compare/D8-DatabaseManager-2363315-2

Status: Needs review » Needs work

The last submitted patch, 8: D8-2363315-8-DatabaseManager-006.patch, failed testing.

donquixote’s picture

Status: Needs work » Needs review
StatusFileSize
new34.6 KB

Status: Needs review » Needs work

The last submitted patch, 10: D8-2363315-10-DatabaseManager-006-corrected.patch, failed testing.

donquixote’s picture

Status: Needs work » Needs review
StatusFileSize
new34.6 KB

https://github.com/donquixote/drupal/compare/D8-DatabaseManager-2363315-12

Pretty obvious fix:
https://github.com/donquixote/drupal/commit/43e4e8e5da0b64b32090260da9c8...

   public function setActiveConnection($key = 'default') {
-    if (!$this->connectionInfoPool->keyExists($key)) {
+    if ($this->connectionInfoPool->keyExists($key)) {
fabianx’s picture

Looks pretty good already.

I would love to see a static initialization instead of the getDatabaseHandler() override in every method call.

But given this is a BC layer if we can convert enough things to not use the Database:: static class, but instead the injected Database Manager directly, then it would be okay.

donquixote’s picture

We could introduce a Database::initStaticProperties() for Database::$databaseManager, like

donquixote’s picture

StatusFileSize
new34.83 KB

It's been a while.
Reroll, just in case.

https://github.com/donquixote/drupal/compare/D8-DatabaseManager-2363315-15

I still don't know if we should got for the ConnectionInfoPool here. Does not add so much benefit for now.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Following up if this still relevant issue?

andypost’s picture

Version: 9.4.x-dev » 10.1.x-dev
Assigned: donquixote » Unassigned
Status: Needs review » Needs work
Issue tags: +Needs reroll, +Needs subsystem maintainer review

Changes looks handy, needs DB mantainers to check proper namespaces

ravi.shankar’s picture

Assigned: Unassigned » ravi.shankar

Working on this...will upload the patch soon.

ravi.shankar’s picture

Assigned: ravi.shankar » Unassigned
StatusFileSize
new22.61 KB
new13.85 KB

Tried to add reroll of patch #15 on Drupal 10.1.x, there is lots of changes since last patch, please review the patch.

Keeping the status needs work for now.

ajaypratapsingh’s picture

Status: Needs work » Needs review
StatusFileSize
new41.7 KB

just Rerolled patch against drupal 9.5.x

borisson_’s picture

The last reroll needs phpcs fixes, but phpstan also seems to fail, that might be more difficult to fix.

I agree with #28, we should get a DB maintainer in to have a look at this before going much further?

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

vsujeetkumar’s picture

StatusFileSize
new43.8 KB

Re-roll patch created for 11.x. Keeps as in "Needs work" because of unaddressed comment #33.

vsujeetkumar’s picture

StatusFileSize
new44.05 KB
new5.11 KB

Fixed CCF issues.

vsujeetkumar’s picture

StatusFileSize
new42.96 KB
new2.29 KB

Fixed the CCF issue by removing the deprecated method. Please have a look

vsujeetkumar’s picture

StatusFileSize
new43.68 KB

Fixed the CCF issue.

borisson_’s picture

Status: Needs work » Needs review

Setting to needs review to make the testbot run the patch

vsujeetkumar’s picture

StatusFileSize
new2.49 KB

Adding missing interdiff. Please have a look.

borisson_’s picture

Status: Needs review » Needs work

Test Result (6,266 failures / ±0)

Back to needs work

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.