A
A
adisos2014-11-24 14:42:01
Drupal
adisos, 2014-11-24 14:42:01

Is there an analogue of the dbDelta function from Wordpress in Drupal 7?

The dbDelta function checks the existing structure of the tables in the database, compares it with the structure required by the plugin and, if necessary, makes changes to the tables.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Katya, 2014-12-04
@adisos

Table schema changes are explicitly specified in update hooks .
For example, to increase the size of a varchar field, you need to do something like this ( source ):

/**
 * Enlarge URL field size for popular content database table.
 */
function yandex_metrics_reports_update_7202() {
  db_drop_unique_key('yandex_metrics_reports_popular_content', 'url');

  db_change_field('yandex_metrics_reports_popular_content', 'url', 'url', array(
      'description' => 'The url obtained from Yandex.Metrika.',
      'type' => 'varchar',
      'length' => 2048,
      'not null' => TRUE,
      'default' => ''
    ),
    array(
      'indexes' => array('url' => array(array('url', 255))),
    )
  );
}

Next, you need to start the update process by following the link /update.php .
Here the developer himself works with the database using the db_ functions (for example, db_rename_table, db_change_field, db_field_set_default , etc.) Drupal will not do this for him, so as not to inadvertently damage the data :)
There are modules that show the difference between the schema in the module and the real state of things in the database: https://www.drupal.org/project/schema, but changes are made by the author of the module.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question