Skip to main content
4 of 5
added 293 characters in body
Sugandh Khanna
  • 3.5k
  • 5
  • 40
  • 69

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;

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

@edit

module a

a.install

function a_schema() {
  $schema['my_table'] = array(
    'fields' => array(
       'type' => array(
          'description' => 'registration type',
          'type' => 'varchar',
          'length' => 255,
        ),
     ),
  );
}

module b

/**
 * Implements  hook_update_N()().
 */
    function b_update_8201() {
     $spec = array(
        'type' => 'varchar',
        'description' => "New Col",
        'length' => 20,
        'not null' => FALSE,
      ); 
     $schema = Database::getConnection()->schema();
     $schema->addField('my_table', 'newcol', $spec);
     return $schema;
    }
Sugandh Khanna
  • 3.5k
  • 5
  • 40
  • 69