You are here

function search_api_page_update_7103 in Search API Pages 7

Restrict Search API Page machine names to 32 characters.

File

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

Code

function search_api_page_update_7103() {
  $tx = db_transaction();
  try {
    foreach (db_query('SELECT machine_name FROM {search_api_page} WHERE LENGTH(machine_name) > 32')
      ->fetchCol() as $old_name) {
      if (!isset($existing)) {
        $existing = db_query('SELECT machine_name FROM {search_api_page}')
          ->fetchAllAssoc('machine_name');
      }
      $base = $name = substr($old_name, 0, 32);
      $i = 0;
      while (isset($existing[$name])) {
        $name = $base . '_' . ++$i;
        if (strlen($name) > 32) {
          $suffix_len = strlen('_' . $i);
          $base = substr($base, 0, 32 - $suffix_len);
          $name = $base . '_' . $i;
        }
      }
      $existing[$name] = TRUE;
      db_update('search_api_page')
        ->fields(array(
        'machine_name' => $name,
      ))
        ->condition('machine_name', $old_name)
        ->execute();
    }
    $spec = array(
      'description' => 'The machine name for a search page.',
      'type' => 'varchar',
      'length' => 32,
      'not null' => TRUE,
    );
    db_change_field('search_api_page', 'machine_name', 'machine_name', $spec);
  } catch (Exception $e) {
    $tx
      ->rollback();
    throw new DrupalUpdateException(t('An exception occurred during the update: @msg.', array(
      '@msg' => $e
        ->getMessage(),
    )));
  }
}