You are here

function video_embed_field_update_7006 in Video Embed Field 7.2

Updates vef_video_styles table structure.

File

./video_embed_field.install, line 240
Install, update and uninstall functions for the video_embed_field module.

Code

function video_embed_field_update_7006() {

  // Convert the table structure.
  db_drop_field('vef_video_styles', 'vsid');
  db_add_primary_key('vef_video_styles', array(
    'name',
  ));
  db_drop_unique_key('vef_video_styles', 'name');
  db_add_field('vef_video_styles', 'title', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // Update title and name values.
  $result = db_select('vef_video_styles', 'vef')
    ->fields('vef', array(
    'name',
  ))
    ->execute();
  foreach ($result as $record) {

    // Set current name as title.
    db_update('vef_video_styles')
      ->fields(array(
      'title' => $record->name,
    ))
      ->condition('name', $record->name)
      ->execute();

    // Update name to fit with machine_name constraints.
    $new_name = preg_replace('/[^a-z0-9_]+/', '_', drupal_strtolower($record->name));
    if ($new_name != $record->name) {

      // Check if new name already exists in the database.
      $counter = 1;
      $base_name = $new_name;
      while (TRUE) {
        $result = db_select('vef_video_styles', 'vef')
          ->fields('vef', array(
          'name',
        ))
          ->condition('name', $new_name)
          ->execute();
        if ($result
          ->rowCount()) {
          $new_name = $base_name . '_' . $counter;
        }
        else {
          db_update('vef_video_styles')
            ->fields(array(
            'name' => $new_name,
          ))
            ->condition('name', $record->name)
            ->execute();
          break;
        }
      }
    }
  }
  return t('Database schema updated successfully');
}