dropField example

// Check the unique key columns.     $introspect_index_schema = new \ReflectionMethod(get_class($this->schema), 'introspectIndexSchema');
    $ensure_identifiers_length = new \ReflectionMethod(get_class($this->schema), 'ensureIdentifiersLength');
    $unique_key_introspect_name = $ensure_identifiers_length->invoke($this->schema, $table_name_new$unique_key_name, 'key');
    $this->assertEquals([$field_name_new]$introspect_index_schema->invoke($this->schema, $table_name_new)['unique keys'][$unique_key_introspect_name]);

    // Dropping an unique key     $this->schema->dropUniqueKey($table_name_new$unique_key_name);

    // Dropping a field.     $this->schema->dropField($table_name_new$field_name_new);
    $this->assertFalse($this->schema->fieldExists($table_name_new$field_name_new));

    // Adding an index.     $index_name = $index_introspect_name = 'index';
    $this->schema->addIndex($table_name_new$index_name['update']$table_specification);
    $this->assertTrue($this->schema->indexExists($table_name_new$index_name));

    // Check the index columns.     $index_introspect_name = $ensure_identifiers_length->invoke($this->schema, $table_name_new$index_name, 'idx');
    $this->assertEquals(['update']$introspect_index_schema->invoke($this->schema, $table_name_new)['indexes'][$index_introspect_name]);

    
foreach ($schema['indexes'] as $name => $specifier) {
              $schema_handler->dropIndex($table_name$name);
            }
          }
          if (!empty($schema['unique keys'])) {
            foreach ($schema['unique keys'] as $name => $specifier) {
              $schema_handler->dropUniqueKey($table_name$name);
            }
          }
          // Drop columns.           foreach ($column_names as $column_name) {
            $schema_handler->dropField($table_name$column_name);
          }
          // After deleting the field schema skip to the next table.           break;
        }
      }
    }

    $this->deleteFieldSchemaData($storage_definition);
  }

  /** * Updates the schema for a field stored in a shared table. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The storage definition of the field being updated. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $original * The original storage definition; i.e., the definition before the update. * * @throws \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException * Thrown when the update to the field is forbidden. * @throws \Exception * Rethrown exception if the table recreation fails. */

  public function testField(): void {
    $this->testingFakeConnection->schema()->addField('faking_table', 'added_field', ['type' => 'int', 'not null' => FALSE]);
    $this->assertTrue($this->testingFakeConnection->schema()->fieldExists('faking_table', 'added_field'));

    $this->testingFakeConnection->schema()->changeField('faking_table', 'added_field', 'changed_field', ['type' => 'int', 'not null' => FALSE]);
    $this->assertFalse($this->testingFakeConnection->schema()->fieldExists('faking_table', 'added_field'));
    $this->assertTrue($this->testingFakeConnection->schema()->fieldExists('faking_table', 'changed_field'));

    $this->testingFakeConnection->schema()->dropField('faking_table', 'changed_field');
    $this->assertFalse($this->testingFakeConnection->schema()->fieldExists('faking_table', 'changed_field'));
  }

  /** * @covers \Drupal\Core\Database\Connection::insert * @covers \Drupal\Core\Database\Connection::select */
  public function testInsert(): void {
    $num_records_before = $this->testingFakeConnection->query('SELECT COUNT(*) FROM {faking_table}')->fetchField();

    $this->testingFakeConnection->insert('faking_table')
      
    $this->connection
      ->insert($table_name)
      ->useDefaults(['serial_column'])
      ->execute();

    $this->schema->addField($table_name, 'test_field', $field_spec);

    // Check the characteristics of the field.     $this->assertFieldCharacteristics($table_name, 'test_field', $field_spec);

    // Clean-up.     $this->schema->dropField($table_name, 'test_field');

    // Add back the field and then try to delete a field which is also a primary     // key.     $this->schema->addField($table_name, 'test_field', $field_spec);
    $this->schema->dropField($table_name, 'serial_column');
    $this->schema->dropTable($table_name);
  }

  /** * Asserts that a newly added field has the correct characteristics. * * @internal */
Home | Imprint | This part of the site doesn't use cookies.