how to check if table exist in update procedure
In Drupal 8/9, you can programmatically check if a specific table name exists in the database using the schema()->tableExists() method provided by the database API.
<?php
use Drupal\Core\Database\Database;
function drupalvip_module_update_9202() {
// check if table exist, if not install schema
$table['group'] = [
'description' => 'Stores IP group addresses.',
'fields' => [
'id' => [ 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'unique ID', ],
'name' => [ 'type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'description' => 'group name', ],
],
'primary key' => ['id'],
];
$schema = \Drupal::database()->schema();
if (!$schema->tableExists('drupalvip_group')) {
$schema->createTable('drupalvip_group', $table['group']);
}
}
* Create new database table {mytable2}.
*/
function mymodule_update_9000() {
$schema['mytable2'] = [
// table definition array goes here
];
\Drupal::database()->schema()->createTable('mytable2', $schema['mytable2']);
}