When purging a field definition, don't fail if field storage no longer exists

Created on 14 May 2013, over 11 years ago
Updated 15 December 2023, about 1 year ago

I ran into a problem where field_purge_batch() was called during cron, and expecting to delete all data related to field_xyz. The problem was that the table field_data_field_xyz did not exist on my site, so an exception was thrown.

Here are some symptoms:

(1) field_purge_batch(10) returns an exception

(2) field_read_instances(array('deleted' => 1), array('include_deleted' => 1)) returns field_xyz, but field_xyz's corresponding field_data_field_xyz table does not exist.

(3)

$ drush cron;

WD cron: PDOException: SQLSTATE[42S02]: Base table or view not found:[error]
1146 Table 'field_data_field_xyz' doesn't exist:
SELECT DISTINCT field_data_field_xyz0.entity_type AS
entity_type, field_data_field_xyz0.entity_id AS
entity_id, field_data_field_xyz0.revision_id AS
revision_id, field_data_field_xyz0.bundle AS bundle
FROM 
{field_data_field_xyz}
field_data_field_xyz0
WHERE  (field_data_field_xyz0.deleted =
:db_condition_placeholder_0) AND
(field_data_field_xyz0.bundle =
:db_condition_placeholder_1) 
LIMIT 10 OFFSET 0; Array
(
    [:db_condition_placeholder_0] => 1
    [:db_condition_placeholder_1] => stm_tarif_page
)
 in field_sql_storage_field_storage_query() (line 585 of
/path/to/drupal/modules/field/modules/field_sql_storage/field_sql_storage.module).


Table 'stm.field_data_field_stm_tarifs_lignes' doesn't exist

I have no idea why field_data_field_xyz did not exist, perhaps because of an interrupted previous cleanup attempt, but at this point if the table does not exist, might it be OK to simply assume that there is no data?

✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
FieldΒ  β†’

Last updated 2 days ago

Created by

πŸ‡¨πŸ‡¦Canada alberto56

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

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

  • πŸ‡­πŸ‡ΊHungary nagy.balint

    It seems that it is also possible that the storage itself is missing and getFieldStorageDefinition throws a FieldException

    So I added an extra try catch block there, as if there is no storage, then also nothing to do I guess.

    for now the patch is for 9.5.

  • πŸ‡΅πŸ‡ΉPortugal ricardofaria

    Adding a patch for drupal 10.1.x

  • πŸ‡ΊπŸ‡ΈUnited States mglaman WI, USA

    This happened to me where the field storage was purged even though the deleted fields repository was tracking a field definition. I don't know the root cause. It may be due to cron running at the same time in the background and processing field_cron while the UI is also handling the field purge as well.

    There is no lock used for this process to prevent duplicate purge runs on specific fields.

Production build 0.71.5 2024