Improperly deployed *missing* entity table breaks module uninstall

Created on 5 September 2015, about 9 years ago
Updated 19 July 2024, 4 months ago

Problem/Motivation

ContentEntityStorage base has a hasData() method that checks if any entities exist at all. This method is called during the uninstall process of a module to warn the user that data will be lost if the module is uninstalled.

When a module has deployment issues and the entity storage table has not been created, the hasData() method will throw an exception, completely blocking the uninstall process. So basically you end up with a broken module, that you cannot uninstall.

Proposed resolution

hasData() should return FALSE when the database table does not exist.

Remaining tasks

Address #18:
Determine how to know when hasData is failing specifically due to a missing database table and not for some other reason.
Suggested solution is to override the query since we can't assume a database backend.
Is it possible to examine the exception instead and return false for the specific missing table exception and throw the error otherwise

User interface changes

None

API changes

None

Data model changes

None

Original report

1. Create a content entity, and mess it up so much that the module that defines the entity installs but the entity table is not created.

(or just delete the entity table for a custom defined entity)

2. Try to uninstall the module... the uninstall page is all broken with a message such as this one:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid object name 'powerlog'.: SELECT TOP(1) base_table.[id] AS [id], base_table.[id] AS [base_table_id] FROM powerlog base_table; Array ( ) : SELECT TOP(1) base_table.[id] AS [id], base_table.[id] AS [base_table_id] FROM powerlog base_table; Array ( ) in Drupal\Core\Entity\ContentEntityStorageBase->hasData() (line 74 of core\lib\Drupal\Core\Entity\ContentEntityStorageBase.php).
Drupal\Driver\Database\sqlsrv\Statement->execute(Array, Array)
Drupal\Driver\Database\sqlsrv\Connection->query('SELECT TOP(1) base_table.[id] AS [id], base_table.[id] AS [base_table_id]
FROM
{powerlog} base_table', Array, Array)
Drupal\Core\Database\Query\Select->execute()
Drupal\Core\Entity\Query\Sql\Query->result()
Drupal\Core\Entity\Query\Sql\Query->execute()
Drupal\Core\Entity\ContentEntityStorageBase->hasData()
Drupal\Core\Entity\ContentUninstallValidator->validate('powerlog')
Drupal\Core\Extension\ModuleInstaller->validateUninstall(Array)
Drupal\Core\ProxyClass\Extension\ModuleInstaller->validateUninstall(Array)
Drupal\system\Form\ModulesUninstallForm->buildForm(Array, Object)

Make the uninstall process more fault tolerant for entity management so that if these are crippled, it won't crash.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
EntityΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡ͺπŸ‡ΈSpain david_garcia

Live updates comments and jobs are added and updated live.
  • Triaged core major

    There is consensus among core maintainers that this is a major issue. Only core committers should add this tag.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡±Netherlands mariskath

    #57 worked for me! Thanks!

    Best,
    Mariska

  • πŸ‡³πŸ‡¬Nigeria chike Nigeria

    I installed Drupal 10.2.4 and started building the site, just regular site building stuff - enabling contrib modules and creating pages. Then I decided to uninstall the 'contact' module and clicking /admin/modules/uninstall threw this error,

    Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'custom_theme.media' doesn't exist: SELECT "base_table"."vid" AS "vid", "base_table"."mid" AS "mid" FROM "media" "base_table" GROUP BY "base_table"."vid", "base_table"."mid" LIMIT 1 OFFSET 0; Array ( ) in Drupal\Core\Entity\Query\Sql\Query->result() (line 271 of C:\wamp64\www\custom-theme\public_html\core\lib\Drupal\Core\Entity\Query\Sql\Query.php).

    The 'media' module was auto-installed as a dependency for 'bootstrap_layout_builder' module. I have not added a media item yet.

    Anyways patch #57 got me back to the Uninstall page and I was able to uninstall the 'contact' module.

    Thanks @_utsavsharma

  • πŸ‡³πŸ‡¬Nigeria chike Nigeria

    My issue wasn't solved. What happened in my case was somehow 'media' module did not install completely or something cos I found the media types were not available on the site and visiting 'admin/content/media' threw an error. So I rolled back the site till I uninstalled 'media' module. I removed patch #57 and re-installed the media module and it was now properly installed. I re-installed back all the other modules and site came back normal.

    The thing is, without patch #57 I wouldn't have been able to access the Uninstall page to uninstall the media module and the other modules that required it.

  • First commit to issue fork.
  • Merge request !7259Convert 57 to MR β†’ (Open) created by nicxvan
  • Pipeline finished with Failed
    8 months ago
    Total: 182s
    #134024
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    I've converted this to an MR and reviewed it. It looks like #2 was taken care of in the latest patch.

    I fixed some phpcs issues and updated the issue summary remaining tasks.
    If someone can share some direction on #18 I'm happy to make an attempt.

  • Pipeline finished with Success
    8 months ago
    Total: 524s
    #134030
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    I wonder if rather than just overriding the request and trying to narrow the scope, maybe in the catch we need to examine the exception and return false if it's due to a missing table, and throw the reason otherwise?

  • Status changed to Needs review 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States Greg Boggs Portland Oregon

    I don't see code in the current changes that assume a database. So, perhaps the comment in #18 is about surrounding code outside of these changes. I am going to mark this needs review again until it's more clear what work needs to be done on this MR.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    So believe #18 was referring to the original fix at that time around ContentEntityStorageBase so would say may not be relevant with new approach?

    Will leave in review but believe proposed solution may need to be tweaked.

  • Status changed to Needs work 4 months ago
  • The Needs Review Queue Bot β†’ tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

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

Production build 0.71.5 2024