changeField example

    $this->schema->addPrimaryKey($table_name_new[$field_name]);

    // Check the primary key columns.     $find_primary_key_columns = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
    $this->assertEquals([$field_name]$find_primary_key_columns->invoke($this->schema, $table_name_new));

    // Dropping a primary key.     $this->schema->dropPrimaryKey($table_name_new);

    // Changing a field.     $field_name_new = 'where';
    $this->schema->changeField($table_name_new$field_name$field_name_new['type' => 'int', 'not null' => FALSE]);
    $this->assertFalse($this->schema->fieldExists($table_name_new$field_name));
    $this->assertTrue($this->schema->fieldExists($table_name_new$field_name_new));

    // Adding an unique key     $unique_key_name = $unique_key_introspect_name = 'unique';
    $this->schema->addUniqueKey($table_name_new$unique_key_name[$field_name_new]);

    // 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');
    
->execute();
    $saved_value = $this->connection->query('SELECT [update] FROM {select} WHERE [id] = :id', [':id' => 2])->fetchField();
    $this->assertEquals('Update value 2', $saved_value);
  }

  /** * Tests insertion integrity violation with no default value for a column. */
  public function testInsertIntegrityViolation() {
    // Remove the default from the 'age' column, so that inserting a record     // without its value specified will lead to integrity failure.     $this->connection->schema()->changeField('test', 'age', 'age', [
      'description' => "The person's age",
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
    ]);

    // Try inserting a record that misses the value for the 'age' column,     // should raise an IntegrityConstraintViolationException.     $this->expectException(IntegrityConstraintViolationException::class);
    $this->connection->insert('test')
      ->fields(['name'])
      
// Handle NOT NULL constraints.             foreach ($schema[$table_name]['fields'] as $column_name => $specifier) {
              $not_null = !empty($specifier['not null']);
              $original_not_null = !empty($original_schema[$table_name]['fields'][$column_name]['not null']);
              if ($not_null !== $original_not_null) {
                if ($not_null && $this->hasNullFieldPropertyData($table_name$column_name)) {
                  throw new EntityStorageException("The $column_name column cannot have NOT NULL constraints as it holds NULL values.");
                }
                $column_schema = $original_schema[$table_name]['fields'][$column_name];
                $column_schema['not null'] = $not_null;
                $schema_handler->changeField($table_name$column_name$column_name$column_schema);
              }
            }

            // Drop original indexes and unique keys.             if (!empty($original_schema[$table_name]['indexes'])) {
              foreach ($original_schema[$table_name]['indexes'] as $name => $specifier) {
                $schema_handler->dropIndex($table_name$name);
              }
            }
            if (!empty($original_schema[$table_name]['unique keys'])) {
              foreach ($original_schema[$table_name]['unique keys'] as $name => $specifier) {
                
// Assert that the column comment has been set.     $this->checkSchemaComment($table_specification['fields']['test_field']['description'], 'test_table', 'test_field');

    // Make sure that fields have the correct collation, if supported.     $this->assertCollation();

    // An insert without a value for the column 'test_table' should fail.     $this->assertFalse($this->tryInsert(), 'Insert without a default failed.');

    // Add a default value to the column.     $this->schema->changeField('test_table', 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE, 'default' => 0]);
    // The insert should now succeed.     $this->assertTrue($this->tryInsert(), 'Insert with a default succeeded.');

    // Remove the default.     $this->schema->changeField('test_table', 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE]);
    // The insert should fail again.     $this->assertFalse($this->tryInsert(), 'Insert without a default failed.');

    // Test for fake index and test for the boolean result of indexExists().     $index_exists = $this->schema->indexExists('test_table', 'test_field');
    $this->assertFalse($index_exists, 'Fake index does not exist');
    
/** * @covers ::addField * @covers ::fieldExists * @covers ::dropField * @covers ::changeField */
  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 */
  
$this->connection->update($table)
        ->expression($field$expression$arguments)
        ->execute();
    }
    elseif (isset($spec['initial'])) {
      $this->connection->update($table)
        ->fields([$field => $spec['initial']])
        ->execute();
    }
    if ($fixnull) {
      $spec['not null'] = TRUE;
      $this->changeField($table$field$field$spec);
    }
  }

  /** * {@inheritdoc} */
  public function dropField($table$field) {
    if (!$this->fieldExists($table$field)) {
      return FALSE;
    }

    
'type' => 'varchar_ascii',
      'length' => 50,
      'not null' => TRUE,
      'default' => '',
    ];
    $keys_spec = [
      'unique keys' => [
        'job' => [['job', 10]],
      ],
    ];
    $this->connection->schema()
      ->changeField('test_people', 'job', 'job', $field_spec$keys_spec);

    $this->checkUniqueConstraintException('test_people', 'job');
  }

  /** * Verifies that inserting the same value/prefix twice causes an exception. * * @param string $table * The table to insert into. * @param string $column * The column on that table that has a UNIQUE index. If prefix lengths are * accepted for UNIQUE keys on the current database, the prefix length for * the field is expected to be set to 10 characters. */
Home | Imprint | This part of the site doesn't use cookies.