Drupal 7 – Update the module schema by hook_update_N()

Recently i start working on BeansTag again as some users request to add the meta robots and canonical options.

This time, the update involves a change in the module schema. In this case, we need to implement the hook_update_N() in .install.

The following piece of code is added to beanstag.install.

/**
 * Implements hook_update_N().
 * Add meta_robots and meta_canonical fields to {beanstag} table.
 */
function beanstag_update_7000() {
  // Add meta robots
  $spec = array(
    'type' => 'varchar',
    'description' => t('The meta robots for the path alias.'),
    'length' => '255',
    'not null' => FALSE,
  ); 
  db_add_field('beanstag', 'meta_robots', $spec);
  
  // Add meta_canonical
  $spec = array(
    'type' => 'varchar',
    'description' => t('The meta canonical for the path alias.'),
    'length' => '255',
    'not null' => FALSE,
  ); 
  db_add_field('beanstag', 'meta_canonical', $spec);
}

 

We also need to add the new columns in the hook_schema().

/**
 * Implements hook_schema().
 */
function beanstag_schema() {
  $schema = array();

  $schema['beanstag'] = array(
    'description' => t('The base table for saved meta tags and page title for path alias.'),
    'fields' => array(
      'id' => array(
        'description' => t('The primary identifier'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'uid' => array(
        'description' => t('The user identifier.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0
      ),
      'path_alias' => array(
        'description' => t('The path alias for customized meta tags and page title.'),
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE
      ),
      'page_title' => array(
        'description' => t('The page title for the path alias.'),
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE
      ),
      'meta_keywords' => array(
        'description' => t('The meta keywords for the path alias.'),
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE
      ),
      'meta_description' => array(
        'description' => t('The meta description for the path alias.'),
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE
      ),
      'meta_robots' => array(
        'description' => t('The meta robots for the path alias.'),
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE
      ),
      'meta_canonical' => array(
        'description' => t('The meta canonical for the path alias.'),
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE
      ),
    ),
    'primary key' => array('id'),
  );
  return $schema;
}

 

Run the Drupal update.php and you should find the 2 new columns in the database.

Done =)

Reference:

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.