Problem/Motivation

The $field parameter for Drupal\Core\Database\Schema::addIndex is not completely documented. There is hidden functionality in that it can additionally provide a maximum length for a given field.

The NodeStorageSchema illustrates this:

$schema ['node_field_data']['indexes'] += array(
    ...
    'node__title_type' => array('title', array('type', 4)),
  );

That call results in the type field being restricted to 4 characters in the index.

The code responsible is actually in each separate driver. This is the mysql version:

protected function createKeySql($fields) {
  $return = array();
  foreach ($fields as $field) {
    if (is_array($field)) {
      $return [] = '`' . $field [0] . '`(' . $field [1] . ')';
    }
    else {
      $return [] = '`' . $field . '`';
    }
  }
  return implode(', ', $return);
}

Proposed resolution

Document this behavior.

Remaining tasks

User interface changes

API changes

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug
Unfrozen changes Unfrozen because it only changes documentation.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhedstrom’s picture

Status: Active » Needs review
Issue tags: +Documentation
FileSize
931 bytes
jhodgdon’s picture

Status: Needs review » Needs work

Thanks for documenting this!

So... I think the docs could be clearer. How about:

An array of field names or field information; if field information is passed, it's an array whose first element is the field name and whose second is the maximum length in the index. For example [...]

Also the @code tags should be indented the same as the previous text.

jhedstrom’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
1.28 KB
150.79 KB

Thanks! I realized as I was typing the docs for #1 that it felt extremely awkward :)

jhedstrom’s picture

Issue summary: View changes
jhedstrom’s picture

FileSize
925 bytes

Woah, that was the wrong patch :) Interdiff was still accurate though.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Looks good now, thanks!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 36b1c8b and pushed to 8.0.x. Thanks!

  • alexpott committed 36b1c8b on 8.0.x
    Issue #2462641 by jhedstrom: Incomplete documentation in Schema::...

Status: Fixed » Closed (fixed)

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