Properly Create/Update field indexes when importing updated field config

Created on 23 August 2022, almost 3 years ago
Updated 16 February 2023, over 2 years ago

Problem/Motivation

When you have a custom field, you might want to index the value column. There are currently several issue that prevents this.

1. If you attempt to modify an existing field config yaml file and run config import, and error is thrown in updateDedicatedTableSchema, because `dropIndex` (and later `addIndex`) is called on the revision table even if the field is not revisionable. This throws an exception.

2. If you attempt to use any of the "extra" field columns, only deleted works properly, as getFieldColumnName usesgetReservedColumns to determine whether to prefix the field name to the column. That function only contains deleted, unlike getExtraColumns, which has entity_id and the other columns.

Steps to reproduce

1. config import the following custom field:

langcode: en
status: true
dependencies:
  module:
    - user
id: user.custom_id
field_name: custom_id
entity_type: user
type: string
settings:
  max_length: 255
  case_sensitive: false
  is_ascii: false
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

2. Let's add an index, and try running config import again, using the following:

langcode: en
status: true
dependencies:
  module:
    - user
id: user.custom_id
field_name: custom_id
entity_type: user
type: string
settings:
  max_length: 255
  case_sensitive: false
  is_ascii: false
module: core
locked: false
cardinality: 1
translatable: true
indexes:
  custom_id_entity_id:
    - value
    - entity_id
persist_with_no_fields: false
custom_storage: false

Expected result: The index is added to the custom user field table.

Actual result: multiple exceptions are encountered.

Proposed resolution

1. Don't attempt to drop/add indexes on the revision table in updateDedicatedTableSchema if there is no revision table.
2. Don't prepend the field name on any of the "extra" columns.

๐Ÿ› Bug report
Status

Needs work

Version

9.5

Component
Entityย  โ†’

Last updated 3 days ago

  • Maintained by
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom @catch
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland @berdir
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany @hchonov
Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States TomTech

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.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request โ†’ as a guide.

    As a bug it will need a test case also.

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland tcrawford

    Patch #6 allowed me to import field storage config where I had added an index. Thanks.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Based on the patch this is not a database system issue, strictly speaking.

  • Status changed to RTBC over 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland ayalon

    I also tested the patch and it fixed the described issue.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    9.5.x is EOL, bumping to 11.x. This could really do with test coverage to ensure we don't break it again in the future.

  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    On latest D10:

    I added the following to the field.storage.node.fieldโ€ฆ file:

    indexes:
      field_last_status_change__value:
        - value
    

    Then see before and after:

    $ ddev drush sql:query "show indexes from node__field_last_status_change;"
    node__field_last_status_change  0       PRIMARY 1       entity_id       A       4875    NULL    NULL            BTREE           
    node__field_last_status_change  0       PRIMARY 2       deleted A       4875    NULL    NULL            BTREE           
    node__field_last_status_change  0       PRIMARY 3       delta   A       4875    NULL    NULL            BTREE           
    node__field_last_status_change  0       PRIMARY 4       langcode        A       4875    NULL    NULL            BTREE           
    node__field_last_status_change  1       bundle  1       bundle  A       2       NULL    NULL            BTREE           
    node__field_last_status_change  1       revision_id     1       revision_id     A       4875    NULL    NULL            BTREE           
    
    $ ddev drush cim
    +------------+---------------------------------------------+-----------+
    | Collection | Config                                      | Operation |
    +------------+---------------------------------------------+-----------+
    |            | field.storage.node.field_last_status_change | Update    |
    +------------+---------------------------------------------+-----------+
    
     Import the listed configuration changes? (yes/no) [yes]:
     > 
    
     [notice] Synchronized configuration: update field.storage.node.field_last_status_change.
     [notice] Finalizing configuration synchronization.
     [success] The configuration was imported successfully.
    
    $ ddev drush sql:query "show indexes from node__field_last_status_change;"
    node__field_last_status_change  0       PRIMARY 1       entity_id       A       4875    NULL    NULL            BTREE           
    node__field_last_status_change  0       PRIMARY 2       deleted A       4875    NULL    NULL            BTREE           
    node__field_last_status_change  0       PRIMARY 3       delta   A       4875    NULL    NULL            BTREE           
    node__field_last_status_change  0       PRIMARY 4       langcode        A       4875    NULL    NULL            BTREE           
    node__field_last_status_change  1       bundle  1       bundle  A       2       NULL    NULL            BTREE           
    node__field_last_status_change  1       revision_id     1       revision_id     A       4875    NULL    NULL            BTREE           
    node__field_last_status_change  1       field_last_status_change_field_last_status_change__value        1       field_last_status_change_value  A       4875    NULL      NULL            BTREE           
    

    I didn't get any exceptions or warnings or errors in the status report.

Production build 0.71.5 2024