Problem/Motivation

Steps to reproduce:

  1. Have two Drupal database connections keyed 'default' and 'other_db' provided in $settings.php.
  2. Calling $table_info = $this->getPrefixInfo('system'); will result in $table_info['database'] being set to 'default' and not 'other_db'. ($this in this Context is the DatabaseConnection class itself).

This was a problem in both Drupal 7 and Drupal 8.

A patch with a solution has been committed to Drupal 8 and recently to 7, the current issue adds tests.

Proposed resolution

Get the information for the right connection.

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Comments

dave reid’s picture

Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new611 bytes

Patch against D7 without a test.

dave reid’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to 7.x
StatusFileSize
new731 bytes

Confirmed this does also affect Drupal 8. I'm not sure at all where to start writing tests for the mysql schema class...

Crell’s picture

Status: Needs review » Reviewed & tested by the community

And this is why calling globals inside objects is bad. :-) Thanks, Dave!

webchick’s picture

Status: Reviewed & tested by the community » Needs work

No tests.

superspring’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new2.22 KB

Same D8 patch with a test added.

Status: Needs review » Needs work

The last submitted patch, 5: 2223073-mysql-schema-prefix-database-D8-5.patch, failed testing.

superspring’s picture

That'll teach me for just copying existing tests and editing.

superspring’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 7: 2223073-mysql-schema-prefix-database-D8-7.patch, failed testing.

Jalandhar’s picture

Status: Needs work » Needs review
StatusFileSize
new2.22 KB

Updating the clean patch with reroll. Please review.

penyaskito’s picture

Status: Needs review » Needs work

Looks quite good, however:

+++ b/core/tests/Drupal/Tests/Core/Database/MultiplePrefixTest.php
@@ -0,0 +1,43 @@
+    $this->assertNotIdentical($info1['prefix'], $info2['prefix'], 'Each target connection has a different prefix.');

I would assert that those prefixes are the expected ones. That they are different probably is enough info for assuring this bug is gone, but this way we could avoid different bugs in the future.

Also, it would be awesome to upload a patch with only the test assuring that it test the actual bug. See "Upload a test case that fails" in https://drupal.org/core-gates#testing

superspring’s picture

Status: Needs work » Needs review
StatusFileSize
new2.38 KB
new1.65 KB

Here is the same patch as #12 with a little more testing and the patch with _only_ the tests to show the fault in the original code base.

Status: Needs review » Needs work

The last submitted patch, 12: drupal8-2223073-12-onlytests.patch, failed testing.

The last submitted patch, 12: drupal8-2223073-12.patch, failed testing.

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.

daffie’s picture

Status: Needs work » Closed (duplicate)
Issue tags: -
dave reid’s picture

Except this issue was clear that it was a bug that also affected Drupal 7 and needed to be fixed there, while that issue was just closed.

daffie’s picture

@Dave Reid: It is going to be fixed for Drupal 7 in #2311305: Schema::getPrefixInfo() return value refers to the wrong database.

David_Rothstein’s picture

Title: Calling DatabaseSchema::getPrefixInfo() on a non-default connection returns the wrong database » Calling DatabaseSchema::getPrefixInfo() on a non-default connection returns the wrong database - write tests
Category: Bug report » Task
Status: Closed (duplicate) » Needs work
Issue tags: +Needs backport to D7

Looks like both those issues should have originally been closed as duplicates of this one, but a little late for that unfortunately.

The Drupal 8 patch was committed without tests, so the Drupal 7 backport that's RTBC in the other issue doesn't have tests either. The patch here has tests, so I guess the simplest thing to do is repurpose it to be about adding the tests to both Drupal 8 and 7...

stefan.r’s picture

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

Re-roll

Status: Needs review » Needs work

The last submitted patch, 20: 2223073-20.patch, failed testing.

The last submitted patch, 20: 2223073-20.patch, failed testing.

stefan.r’s picture

Status: Needs work » Needs review
StatusFileSize
new1.51 KB
fabianx’s picture

Status: Needs review » Reviewed & tested by the community

RTBC for the tests, a test-only patch (e.g. with the fix reverted) would be great, too.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 23: 2223073-23.patch, failed testing.

stefan.r’s picture

Status: Needs work » Needs review
StatusFileSize
new3.21 KB
new2.53 KB
stefan.r’s picture

The last submitted patch, 26: 2223073-26-revertedfix.patch, failed testing.

fabianx’s picture

Status: Needs review » Reviewed & tested by the community

RTBC - looks good to me

stefan.r’s picture

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 30: 2223073-30-revertedfix.patch, failed testing.

fabianx’s picture

Status: Needs work » Reviewed & tested by the community

Still RTBC

stefan.r’s picture

Issue summary: View changes

The last submitted patch, 30: 2223073-30.patch, failed testing.

chx’s picture

Edit: deleted.

chx’s picture

fabianx’s picture

Issue summary: View changes

The last submitted patch, 30: 2223073-30.patch, failed testing.

alexpott’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Committed and pushed 2c2caccd45da80ec1aecdfa8905bd7d8765b1c2f to 8.2.x and 8bf7d7c to 8.1.x. Thanks!

Setting to patch to ported until the d7 porting issue exists.

diff --git a/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php b/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php
index 8032dd2..94c3dd1 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php
@@ -14,7 +14,7 @@ class PrefixInfoTest extends DatabaseTestBase {
   /**
    * Tests that DatabaseSchema::getPrefixInfo() returns the right database.
    */
-  function testGetPrefixInfo() {
+  public function testGetPrefixInfo() {
     $connection_info = Database::getConnectionInfo('default');
     // Copy the default connection info to the 'extra' key.
     Database::addConnectionInfo('extra', 'default', $connection_info['default']);
@@ -54,4 +54,5 @@ function testGetPrefixInfo() {
 
     Database::removeConnection('extra');
   }
+
 }

A couple of CS fixes on commit.

  • alexpott committed 2c2cacc on 8.2.x
    Issue #2223073 by stefan.r, superspring, Dave Reid, Jalandhar: Calling...

  • alexpott committed 8bf7d7c on 8.1.x
    Issue #2223073 by stefan.r, superspring, Dave Reid, Jalandhar: Calling...
bzrudi71’s picture

Status: Patch (to be ported) » Needs work

  • alexpott committed 67b76a0 on 8.2.x
    Revert "Issue #2223073 by stefan.r, superspring, Dave Reid, Jalandhar:...

  • alexpott committed 52c2985 on 8.1.x
    Revert "Issue #2223073 by stefan.r, superspring, Dave Reid, Jalandhar:...
alexpott’s picture

@bzrudi71 yep - weird this had been testing on postgres and sqlite... reverted.

daffie’s picture

Status: Needs work » Needs review
StatusFileSize
new3.62 KB
new2.94 KB
new4.57 KB

The problem that the tests for PostgreSQL and SQLite are failing is that we are testing the return array from the method \Drupal\Core\Database\Driver\mysql\Schema::getPrefixInfo(). This return array is a keyed array with info about amongst other things the database. The problem is that only for MySQL the key "database" is set and not for the other two by Drupal core supported databases. So testing for the other two databases will fail.

The last submitted patch, 46: 2223073-46-revertedfix.patch, failed testing.

The last submitted patch, 46: 2223073-46-revertedfix.patch, failed testing.

daffie’s picture

Ready for a good review.

fabianx’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me and passes on Postgres / SQlite npw. Thanks daffie!

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

Hmmm.... okay but then what code can rely on this having the database information? Why does mysql have this info and the other dbs do not?

daffie’s picture

The database information is added because it is used in Drupal\Core\Database\Driver\mysql\Schema::buildTableNameCondition. That method can easily be rewritten so that it does not need it any more. Or we can fix it the other way around and have all getPrefixInfo method set the database information. @alexpott: What do you think is the best solution.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community

@daffie yeah I looked into it and agree with your analysis of why - I guess what we have in #46 is the right way to go because otherwise it'd be a change for the mysql implementation. It is a shame these things have got out-of-sync because it makes working with the APIs tricker than it should be.

alexpott’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Committed and pushed 32fa9448c41f0c88e92d35cbb6265d7b7013b1b4 to 8.2.x and d34b1a2 to 8.1.x. Thanks!

FILE: ...l/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 65 | ERROR | [x] The closing brace for the class must have an empty
    |       |     line before it
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

diff --git a/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php b/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php
index 15c89d9..131fb7a 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php
@@ -62,4 +62,5 @@ function testGetPrefixInfo() {
       Database::removeConnection('extra');
     }
   }
+
 }

Fixed on commit.

Setting to patch to ported until the d7 porting issue exists.

  • alexpott committed 32fa944 on 8.2.x
    Issue #2223073 by stefan.r, superspring, daffie, Dave Reid, Jalandhar:...

  • alexpott committed d34b1a2 on 8.1.x
    Issue #2223073 by stefan.r, superspring, daffie, Dave Reid, Jalandhar:...
daffie’s picture

  • alexpott committed 2c2cacc on 8.3.x
    Issue #2223073 by stefan.r, superspring, Dave Reid, Jalandhar: Calling...
  • alexpott committed 32fa944 on 8.3.x
    Issue #2223073 by stefan.r, superspring, daffie, Dave Reid, Jalandhar:...
  • alexpott committed 67b76a0 on 8.3.x
    Revert "Issue #2223073 by stefan.r, superspring, Dave Reid, Jalandhar:...

  • alexpott committed 2c2cacc on 8.3.x
    Issue #2223073 by stefan.r, superspring, Dave Reid, Jalandhar: Calling...
  • alexpott committed 32fa944 on 8.3.x
    Issue #2223073 by stefan.r, superspring, daffie, Dave Reid, Jalandhar:...
  • alexpott committed 67b76a0 on 8.3.x
    Revert "Issue #2223073 by stefan.r, superspring, Dave Reid, Jalandhar:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.