Skip to main content
3 of 5
edited body
ya.teck
  • 5.7k
  • 3
  • 35
  • 40

How to alter database schema in Drupal 8?

Since, hook_schema_alter() is removed from Drupal 8, how to alter any existing schema of some module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) function in my .install file

use Drupal\Core\Database\Database;
/**
 * Implements  hook_update_N()().
 */
function mymodule_update_8201() {
 $spec = array(
    'type' => 'varchar',
    'description' => "New Col",
    'length' => 20,
    'not null' => FALSE,
  ); 
 $schema = Database::getConnection()->schema();
 $schema->addField('mytable', 'newcol', $spec);
 return $schema;
}

But no new field or column gets added into my existing table. Is their any other way to achieve this?

Sugandh Khanna
  • 3.5k
  • 5
  • 40
  • 69