/** * ::onEntityTypeUpdate */ public function testonEntityTypeUpdateWithNewIndex() { $this->entityType = $original_entity_type = new ContentEntityType(array('id' => 'entity_test', 'entity_keys' => array('id' => 'id'))); // Add a field with a really long index. $this->setUpStorageDefinition('long_index_name', array('columns' => array('long_index_name' => array('type' => 'int')), 'indexes' => array('long_index_name_really_long_long_name' => array(array('long_index_name', 10))))); $expected = array('entity_test' => array('description' => 'The base table for entity_test entities.', 'fields' => array('id' => array('type' => 'serial', 'not null' => TRUE), 'long_index_name' => array('type' => 'int', 'not null' => FALSE)), 'indexes' => array('entity_test__b588603cb9' => array(array('long_index_name', 10))))); $this->setUpStorageSchema($expected); $table_mapping = new DefaultTableMapping($this->entityType, $this->storageDefinitions); $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions)); $table_mapping->setExtraColumns('entity_test', array('default_langcode')); $this->storage->expects($this->any())->method('getTableMapping')->will($this->returnValue($table_mapping)); $this->storageSchema->expects($this->any())->method('loadEntitySchemaData')->willReturn(['entity_test' => ['indexes' => ['entity_test__b588603cb9' => ['longer_index_name'], 'entity_test__removed_field' => ['removed_field']]]]); // The original indexes should be dropped before the new one is added. $this->dbSchemaHandler->expects($this->at(0))->method('dropIndex')->with('entity_test', 'entity_test__b588603cb9'); $this->dbSchemaHandler->expects($this->at(1))->method('dropIndex')->with('entity_test', 'entity_test__removed_field'); $this->dbSchemaHandler->expects($this->atLeastOnce())->method('fieldExists')->willReturn(TRUE); $this->dbSchemaHandler->expects($this->atLeastOnce())->method('addIndex')->with('entity_test', 'entity_test__b588603cb9', [['long_index_name', 10]], $this->callback(function ($actual_value) use($expected) { $this->assertEquals($expected['entity_test']['indexes'], $actual_value['indexes']); $this->assertEquals($expected['entity_test']['fields'], $actual_value['fields']); // If the parameters don't match, the assertions above will throw an // exception. return TRUE; })); $this->assertNull($this->storageSchema->onEntityTypeUpdate($this->entityType, $original_entity_type)); }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'users_field_data') { switch ($field_name) { case 'name': // Improves the performance of the user__name index defined // in getEntitySchema(). $schema['fields'][$field_name]['not null'] = TRUE; // Make sure the field is no longer than 191 characters so we can // add a unique constraint in MySQL. $schema['fields'][$field_name]['length'] = USERNAME_MAX_LENGTH; break; case 'mail': $this->addSharedTableFieldIndex($storage_definition, $schema); break; case 'access': case 'created': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; } } return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'node_revision') { switch ($field_name) { case 'langcode': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; case 'revision_uid': $this->addSharedTableFieldForeignKey($storage_definition, $schema, 'users', 'uid'); break; } } if ($table_name == 'node_field_data') { switch ($field_name) { case 'promote': case 'status': case 'sticky': case 'title': // Improves the performance of the indexes defined // in getEntitySchema(). $schema['fields'][$field_name]['not null'] = TRUE; break; case 'changed': case 'created': // @todo Revisit index definitions: // https://www.drupal.org/node/2015277. $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; } } return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); if (\Drupal::state()->get('entity_test_update.additional_field_index.' . $table_name . '.' . $storage_definition->getName())) { $this->addSharedTableFieldIndex($storage_definition, $schema); } return $schema; }
/** * {@inheritdoc} */ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) { $schema = parent::getEntitySchema($entity_type, $reset); // Add indexes. $schema['redirect']['unique keys'] += ['hash' => ['hash']]; $schema['redirect']['indexes'] += ['source_language' => [['redirect_source__path', 191], 'language']]; return $schema; }
/** * {@inheritdoc} */ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) { $schema = parent::getEntitySchema($entity_type, $reset); // Creates an index to ensure that the lookup in // \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList::getModerationState() // is performant. $schema['content_moderation_state_field_data']['indexes'] += array('content_moderation_state__lookup' => array('content_entity_type_id', 'content_entity_id', 'content_entity_revision_id')); return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { // Setting the initial value to 1 when we add a 'status' field. // @todo this is a workaround for https://www.drupal.org/node/2346019 $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); if ($storage_definition->getName() == 'status') { $schema['fields']['status']['initial'] = 1; } return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'menu_link_content') { switch ($field_name) { case 'rediscover': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; } } return $schema; }
/** * {@inheritdoc} */ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) { $schema = parent::getEntitySchema($entity_type, $reset); // @todo Create a numeric field type and use that instead. $schema['uc_orders']['fields']['order_total']['type'] = 'numeric'; $schema['uc_orders']['fields']['order_total']['precision'] = 16; $schema['uc_orders']['fields']['order_total']['scale'] = 5; // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['uc_orders']['fields']['uid']['not null'] = TRUE; $schema['uc_orders']['fields']['order_status']['not null'] = TRUE; $schema['uc_orders']['indexes'] += array('uid' => array('uid'), 'order_status' => array('order_status')); $schema['uc_orders']['foreign keys'] += array('users' => array('table' => 'users', 'columns' => array('uid' => 'uid'))); return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'block_content_field_data') { switch ($field_name) { case 'info': // Improves the performance of the block_content__info index defined // in getEntitySchema(). $schema['fields'][$field_name]['not null'] = TRUE; break; } } return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'aggregator_item') { switch ($field_name) { case 'timestamp': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; case 'fid': $this->addSharedTableFieldForeignKey($storage_definition, $schema, 'aggregator_feed', 'fid'); break; } } return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'file_managed') { switch ($field_name) { case 'status': case 'changed': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; case 'uri': $this->addSharedTableFieldUniqueKey($storage_definition, $schema, TRUE); break; } } return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'aggregator_feed') { switch ($field_name) { case 'url': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE, 255); break; case 'queued': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; case 'title': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; } } return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'file_managed') { switch ($field_name) { case 'status': case 'changed': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; case 'uri': $this->addSharedTableFieldUniqueKey($storage_definition, $schema, TRUE); // @todo There should be a 'binary' field type or setting: // https://www.drupal.org/node/2068655. $schema['fields'][$field_name]['binary'] = TRUE; break; } } return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'taxonomy_term_field_data') { // Remove unneeded indexes. unset($schema['indexes']['taxonomy_term_field__vid__target_id']); unset($schema['indexes']['taxonomy_term_field__description__format']); switch ($field_name) { case 'weight': // Improves the performance of the taxonomy_term__tree index defined // in getEntitySchema(). $schema['fields'][$field_name]['not null'] = TRUE; break; case 'name': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; } } return $schema; }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'users_field_data') { switch ($field_name) { case 'name': // Improves the performance of the user__name index defined // in getEntitySchema(). $schema['fields'][$field_name]['not null'] = TRUE; break; case 'mail': $this->addSharedTableFieldIndex($storage_definition, $schema); break; case 'access': case 'created': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; } } return $schema; }
/** * Sets up the storage schema object to test. * * This uses the field definitions set in $this->storageDefinitions. * * @param array $expected * (optional) An associative array describing the expected entity schema to * be created. Defaults to expecting nothing. */ protected function setUpStorageSchema(array $expected = array()) { $this->entityManager->expects($this->any())->method('getDefinition')->with($this->entityType->id())->will($this->returnValue($this->entityType)); $this->entityManager->expects($this->any())->method('getFieldStorageDefinitions')->with($this->entityType->id())->will($this->returnValue($this->storageDefinitions)); $db_schema_handler = $this->getMockBuilder('Drupal\\Core\\Database\\Schema')->disableOriginalConstructor()->getMock(); if ($expected) { $invocation_count = 0; $expected_table_names = array_keys($expected); $expected_table_schemas = array_values($expected); $db_schema_handler->expects($this->any())->method('createTable')->with($this->callback(function ($table_name) use(&$invocation_count, $expected_table_names) { return $expected_table_names[$invocation_count] == $table_name; }), $this->callback(function ($table_schema) use(&$invocation_count, $expected_table_schemas) { return $expected_table_schemas[$invocation_count] == $table_schema; }))->will($this->returnCallback(function () use(&$invocation_count) { $invocation_count++; })); } $connection = $this->getMockBuilder('Drupal\\Core\\Database\\Connection')->disableOriginalConstructor()->getMock(); $connection->expects($this->any())->method('schema')->will($this->returnValue($db_schema_handler)); $key_value = $this->getMock('Drupal\\Core\\KeyValueStore\\KeyValueStoreInterface'); $this->storageSchema = $this->getMockBuilder('Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorageSchema')->setConstructorArgs(array($this->entityManager, $this->entityType, $this->storage, $connection))->setMethods(array('installedStorageSchema', 'loadEntitySchemaData', 'hasSharedTableNameChanges'))->getMock(); $this->storageSchema->expects($this->any())->method('installedStorageSchema')->will($this->returnValue($key_value)); }
/** * {@inheritdoc} */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); $field_name = $storage_definition->getName(); if ($table_name == 'comment_field_data') { // Remove unneeded indexes. unset($schema['indexes']['comment_field__pid__target_id']); unset($schema['indexes']['comment_field__entity_id__target_id']); switch ($field_name) { case 'thread': // Improves the performance of the comment__num_new index defined // in getEntitySchema(). $schema['fields'][$field_name]['not null'] = TRUE; break; case 'created': $this->addSharedTableFieldIndex($storage_definition, $schema, TRUE); break; case 'uid': $this->addSharedTableFieldForeignKey($storage_definition, $schema, 'users', 'uid'); } } return $schema; }
/** * {@inheritdoc} */ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) { $schema = parent::getEntitySchema($entity_type, $reset); $this->alterEntitySchemaWithNonFieldColumns($schema); return $schema; }