There are several places in core that use this pattern to implement lazy loading (this snippet from core/lib/Drupal/Core/Batch/BatchStorage.php):

  public function create(array $batch) {
    // Ensure that a session is started before using the CSRF token generator.
    $this->session->start();
    $try_again = FALSE;
    try {
      // The batch table might not yet exist.
      $this->doCreate($batch);
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.
      if (!$try_again = $this->ensureTableExists()) {
        // If the exception happened for other reason than the missing table,
        // propagate the exception.
        throw $e;
      }
    }
    // Now that the table has been created, try again if necessary.
    if ($try_again) {
      $this->doCreate($batch);
    }
  }

I think that is a little more complicated than necessary, and I think it is worth cleaning this up, since exception handling should be as transparent as possible. I would like to replace the above code with this, eliminating the $try_again variable:

  public function create(array $batch) {
    // Ensure that a session is started before using the CSRF token generator.
    $this->session->start();
    try {
      // The batch table might not yet exist.
      $this->doCreate($batch);
    }
    catch (\Exception $e) {
      // If there was an exception, then try to create the table.
      if ($this->ensureTableExists()) {
        // Now that the table has been created, try again.
        $this->doCreate($batch);
      }
      else {
        // If the exception happened for other reason than the missing table,
        // then propagate the exception.
        throw $e;
      }
    }
  }

I first noticed this pattern when reviewing #2664322: key_value table is only used by a core service but it depends on system install, but @dawehner pointed out this this pattern is already used elsewhere in core. In that issue, the pattern is slightly different: the foo() method does the work itself, and calls itself from the catch block, instead of having a separate method doFoo() that does the actual work.

I believe that the code is functionally identical, so this should not make any API changes.

Comments

benjifisher created an issue. See original summary.

benjifisher’s picture

A related suggestion: maybe it deserves a separate issue? The lazy-loading pattern uses the ensureTableExists () method, so maybe that should be moved to a separate trait for consistency. This pattern also uses the catchException() method, which could also be added to that trait. If we are already using these two methods in five different places, maybe it is time to derive them from a common source.

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

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now 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.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

Assigned: Unassigned » dawehner

I'm having a look at that.

dawehner’s picture

Status: Active » Needs review
StatusFileSize
new6.55 KB

Here is a patch which adds a trait and uses it in two places.

johnwebdev’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
    @@ -0,0 +1,75 @@
    + * ¶
    

    whitespace

  2. +++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
    @@ -0,0 +1,75 @@
    +  abstract function schemaDefinition();
    

    missing visibility

  3. +++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
    @@ -0,0 +1,75 @@
    +      // If another process has already created the semaphore table, attempting to
    

    the indentation seems off

+++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php
@@ -396,44 +399,10 @@ public function removeBin() {
-  protected function ensureBinExists() {

+++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
@@ -0,0 +1,75 @@
+  protected function ensureTableExists() {

Can we really delete that method? Shouldn't it call ensureTableExists?

Should we have a follow- up for all other occurrences, or fix them in this issue?

johnwebdev’s picture

+++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
@@ -0,0 +1,75 @@
+  protected function getDatabase() {
+    return $this->database;
+  }

Should this be abstract? I think $this->connection is the property holding the connection is called often though. But I'm not sure whetever a trait should assume properties exists?

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new33.07 KB
new2.14 KB

@johndevman Thank you for your super quick review! I hope I addressed all the points.

benjifisher’s picture

Status: Needs review » Needs work

It looks as though the patch in #12 includes some unwanted parts. For example, it removes core/lib/Drupal/Component/Plugin/ConfigurableTrait.php, so I am surprised that the testbot does not simply report that the patch fails to apply. (Does it try to reverse the patch in this situation?) My guess is that you diff'ed two different feature branches instead of your current branch and core.

Note that the patch in #10 is 6.55 KB, but the one in #13 is 33.07 KB.

johnwebdev’s picture

Not sure what happened in the last patch, but I found some more nits.

  1. +++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
    @@ -0,0 +1,75 @@
    + * Reusable trait  which allows to lazy creates database tables.
    

    Two spaces

  2. +++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
    @@ -0,0 +1,75 @@
    +   * @return \Drupal\Core\Database\Connection
    

    Misses the summary.

  3. +++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
    @@ -0,0 +1,75 @@
    +  protected function getTableName() {
    

    Needs docblock.

  4. +++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
    @@ -0,0 +1,75 @@
    +   * Provides the database table schem which is created lazyily.
    

    Misspelled lazily.

  5. +++ b/core/lib/Drupal/Core/Database/LazyTableCreationTrait.php
    @@ -0,0 +1,75 @@
    +   * Check if the semaphore table exists and create it if not.
    

    Check(s) and remove /semaphore/
    Should probably have a proper docblock as well.

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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.

bhanu951’s picture

Assigned: dawehner » Unassigned
Issue tags: +Needs reroll
vsujeetkumar’s picture

StatusFileSize
new8.73 KB
new5.86 KB

Re-roll created, Please have a look

vsujeetkumar’s picture

StatusFileSize
new9 KB
new2.16 KB

Fixed CCF issue, Addressed #15, Please have a look.

vsujeetkumar’s picture

StatusFileSize
new9.12 KB
new522 bytes

Fixed the CCF issue.

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.