function Schema::changeField

Same name in this branch
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::changeField()
  2. 9 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::changeField()
  3. 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Schema.php \Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Schema::changeField()
  4. 9 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::changeField()
Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php \Drupal\Core\Database\Driver\sqlite\Schema::changeField()
  2. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\Schema::changeField()
  3. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::changeField()
  4. 8.9.x core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::changeField()
  5. 10 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::changeField()
  6. 10 core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::changeField()
  7. 10 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::changeField()
  8. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Schema.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Schema::changeField()
  9. 10 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::changeField()
  10. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::changeField()
  11. 11.x core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::changeField()
  12. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::changeField()
  13. 11.x core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::changeField()

Overrides Schema::changeField

1 call to Schema::changeField()
Schema::addField in core/modules/mysql/src/Driver/Database/mysql/Schema.php
Add a new field to a table.

File

core/modules/mysql/src/Driver/Database/mysql/Schema.php, line 621

Class

Schema
MySQL implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\mysql\Driver\Database\mysql

Code

public function changeField($table, $field, $field_new, $spec, $keys_new = []) {
    if (!$this->fieldExists($table, $field)) {
        throw new SchemaObjectDoesNotExistException("Cannot change the definition of field '{$table}.{$field}': field doesn't exist.");
    }
    if ($field != $field_new && $this->fieldExists($table, $field_new)) {
        throw new SchemaObjectExistsException("Cannot rename field '{$table}.{$field}' to '{$field_new}': target field already exists.");
    }
    if (isset($keys_new['primary key']) && in_array($field_new, $keys_new['primary key'], TRUE)) {
        $this->ensureNotNullPrimaryKey($keys_new['primary key'], [
            $field_new => $spec,
        ]);
    }
    $sql = 'ALTER TABLE {' . $table . '} CHANGE `' . $field . '` ' . $this->createFieldSql($field_new, $this->processField($spec));
    if ($keys_sql = $this->createKeysSql($keys_new)) {
        $sql .= ', ADD ' . implode(', ADD ', $keys_sql);
    }
    $this->connection
        ->query($sql);
    if ($spec['type'] === 'serial') {
        $max = $this->connection
            ->query('SELECT MAX(`' . $field_new . '`) FROM {' . $table . '}')
            ->fetchField();
        $this->connection
            ->query("ALTER TABLE {" . $table . "} AUTO_INCREMENT = " . ($max + 1));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.