Problem/Motivation

With a PostgreSQL backend, when you rename a table all the indexes also get renamed. When the renamed index has the same name as the old table name, the table renaming will fail.

Proposed resolution

Fix the bug and add testing.

Remaining tasks

None

User interface changes

None

API changes

An API-addition for the method Drupal\Core\Database\Schema::tableExists(). A second parameter is added named: $add_prefix, which defaults to TRUE. When the new parameter is not set, the method works the same as before the second parameter was added. When the second parameter is set to FALSE, the existance of the table is checked without adding its table prefix.

Data model changes

None

Release notes snippet

An API-addition for the method Drupal\Core\Database\Schema::tableExists(). A second parameter is added named: $add_prefix, which defaults to TRUE. When the new parameter is not set, the method works the same as before the second parameter was added. When the second parameter is set to FALSE, the existance of the table is checked without adding its table prefix.

Original bug report

I was upgrading from Drupal 8.5.11 to 8.8.5 and I have got this error: [error] Exception thrown while performing a schema update. SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "old_81ee22taxonomy_term_field_data____name" already exists: ALTER INDEX "idx_30852_taxonomy_term_field__name" RENAME TO old_81ee22taxonomy_term_field_data____name; Array.

I have read many threads related to this error but none patch worked in my case. In the end, I got this fixed with the attached patch.

More Information:

  1. I have checked for any suspicious term inside the table "taxonomy_term_field_data". All looked fine. No term with status set to '0'. All terms are having the valid name and mapped to correct and existing vocabularies.
  2. Checked for existing tables with pattern "old_%", found none.

More on the solution/patch:
On digging more into the issue i have found that in the function renameTable() of my Schema.php of PGSQL Driver (/core/lib/Drupal/Core/Database/Driver/pgsql/Install). The index_name was being passed empty and this caused the duplicate index issue. Extra if check makes sure "$index_name" is not empty or null.

Issue fork drupal-3159113

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

saxenaakansha30 created an issue. See original summary.

saxenaakansha30’s picture

StatusFileSize
new658 bytes

I faced the same issue on upgrading from 8.5.11 to 8.9.2. Attaching patch with similar fix.

cilefen’s picture

Component: taxonomy.module » postgresql db driver
Status: Active » Needs review
daffie’s picture

Version: 8.9.x-dev » 9.1.x-dev
Issue tags: +Bug Smash Initiative, +Needs issue summary update, +Needs change record
StatusFileSize
new7.21 KB

Created a fix for the bug including added tests.

To do that I also had to add an API-addition for the method Drupal\Core\Database\Schema::tableExists(). I added a second parameter to the method $add_prefix which defaults to TRUE. Also added testing for that.

Status: Needs review » Needs work

The last submitted patch, 4: 3159113-4.patch, failed testing. View results

daffie’s picture

Status: Needs work » Needs review
StatusFileSize
new1.34 KB
new7.13 KB
new8 KB

Fixing the failure for MySQL. Therefore added the internal method Drupal\Core\Database\Connection::getQuoteIdentifiers().

daffie’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

Updated the IS.

daffie’s picture

Issue tags: -Needs change record

Added a CR.

daffie’s picture

StatusFileSize
new1.39 KB
new7.18 KB
new8.05 KB

Updated the table names in the added test.

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.

quietone’s picture

Status: Needs review » Needs work

I have read the patch and this looks fine to me and the test shows it fixes the problem in the IS.

The patch still applies to 9.2.x.

My only comment is about readability. After applying the patch and reading the change in \Drupal\Core\Database\Driver\mysql\Schema::tableExists I found myself constantly checking the two queryRange statements to see the difference. So, I was thinking it help to modify that a bit.

+++ b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
@@ -680,7 +680,14 @@ public function tableExists($table) {
+        $this->connection->queryRange("SELECT 1 FROM {" . $table . "}", 0, 1);
...
+        $this->connection->queryRange('SELECT 1 FROM ' . $table, 0, 1);

Maybe modify this so that only the queryRange statement is inside the try. Something like $this->connection->queryRange("SELECT 1 FROM $name", 0, 1);

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.

quietone’s picture

Converted to an MR and hide patches.

There is a failing test, Drupal\Tests\migrate_drupal\Unit\MigrationConfigurationTraitTest.

quietone’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Small nitpicky stuff.

quietone’s picture

Status: Needs work » Needs review

@smustgrave, thanks. I should have done a self review and picked those up before setting to needs review.

I removed the method getQuoteIdentifiers which was in the patch but no longer used in the MR because there is no longer a tableExists method specific for mysql.

Known random failure on

    Drupal\Tests\settings_tray\FunctionalJavascript\SettingsTrayBlockFormTest::testBlocks
    WebDriver\Exception\CurlExec: Curl error thrown for http POST to

Therefore setting back to Needs review.

daffie’s picture

Status: Needs review » Needs work

Minor remark on the MR.

Could we run the pipeline for PostgreSQL for this MR?

quietone’s picture

Status: Needs work » Needs review

@daffie, thanks for the review.

I made the change requested and ran against MySQL, PostgreSQL and SQLite.

Unrelated failure in Drupal\Tests\field_ui\FunctionalJavascript\ManageFieldsTest

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Appears all feedback has been addressed.

quietone’s picture

Title: Cannot upgrade from Drupal 8.5.11 8.8 and higher versions: PostgreSQL Duplicate table error for Alter Index query » Cannot upgrade from Drupal 8.5.11: PostgreSQL Duplicate table error for Alter Index query

Simplify title a bit.

catch’s picture

Title: Cannot upgrade from Drupal 8.5.11: PostgreSQL Duplicate table error for Alter Index query » PostgreSQL Duplicate table error for alter index queries

Re-titling again since this isn't really about updating from 8.5

  • larowlan committed fdacae95 on 11.x
    Issue #3159113 by daffie, saxenaakansha30, quietone: PostgreSQL...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 11.x

Didn't backport to 10.2.x due to the risk of disruption from the signature change

Published change record

Thanks all

Status: Fixed » Closed (fixed)

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

jurgenhaas’s picture

This is unfortunately a breaking change for Drupal 10.3, isn't it?

bradjones1’s picture

Re: #31, no BC issue because the new parameter has a default value which preserves current behavior.

jurgenhaas’s picture

@bradjones1 for those calling the method, that's OK. But there is e.g. the sqlsrv module which extends that method, see \Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::tableExists and suddenly becomes incompatible.

As the abstract class \Drupal\Core\Database\Schema isn't marked internal, it feels as if extending it is what's supposed to be done.