You are here

function search_api_page_update_7102 in Search API Pages 7

Add a {search_api_page}.machine_name column.

File

./search_api_page.install, line 130
Install, update and uninstall functions for the Search pages module.

Code

function search_api_page_update_7102() {
  $tx = db_transaction();
  try {

    // Add the machine_name field, along with its unique key index.
    $spec = array(
      'description' => 'The machine name for a search page.',
      'type' => 'varchar',
      'length' => 50,
      'not null' => TRUE,
      'default' => '',
    );
    db_add_field('search_api_page', 'machine_name', $spec);
    $names = array();
    foreach (db_query('SELECT id, name FROM {search_api_page}')
      ->fetchAllKeyed() as $id => $name) {
      $base = $name = drupal_strtolower(preg_replace('/[^a-z0-9]+/i', '_', $name));
      $i = 0;
      while (isset($names[$name])) {
        $name = $base . '_' . ++$i;
        if (drupal_strlen($name) > 50) {
          $suffix_len = drupal_strlen('_' . $i);
          $base = drupal_substr($base, 0, 50 - $suffix_len);
          $name = $base . '_' . $i;
        }
      }
      $names[$name] = TRUE;
      db_update('search_api_page')
        ->fields(array(
        'machine_name' => $name,
      ))
        ->condition('id', $id)
        ->execute();
    }
    db_add_unique_key('search_api_page', 'machine_name', array(
      'machine_name',
    ));

    // Add the status field.
    $spec = array(
      'description' => 'The exportable status of the entity.',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0x1,
      'size' => 'tiny',
    );
    db_add_field('search_api_page', 'status', $spec);

    // Add the module field.
    $spec = array(
      'description' => 'The name of the providing module if the entity has been defined in code.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
    );
    db_add_field('search_api_page', 'module', $spec);
  } catch (Exception $e) {
    $tx
      ->rollback();
    try {
      db_drop_field('search_api_page', 'machine_name');
      db_drop_field('search_api_page', 'status');
      db_drop_field('search_api_page', 'module');
    } catch (Exception $e1) {

      // Ignore.
    }
    throw new DrupalUpdateException(t('An exception occurred during the update: @msg.', array(
      '@msg' => $e
        ->getMessage(),
    )));
  }
}