You are here

function og_update_7201 in Organic groups 7.2

Add "language" column to all OG membership's related entities.

File

./og.install, line 1037
Install, update, and uninstall functions for the Organic groups module.

Code

function og_update_7201(&$sandbox) {

  //Update the existing entities, with the default language.
  $langcode = language_default()->language;
  $tables = array(
    'og_membership_type',
    'og_membership',
  );
  if (!isset($sandbox['total'])) {
    foreach ($tables as $key => $table) {
      $name = str_replace('_', ' ', $table);
      $column = array(
        'description' => "The {languages}.language of this {$name}.",
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => $langcode,
      );
      db_add_field($table, 'language', $column);
      if ($table == 'og_membership') {

        // We don't want to time out when updating og_membership, so we will
        // process it using batches.
        $query = db_select($table);
        $sandbox['last'] = 0;
        $sandbox['total'] = $query
          ->countQuery()
          ->execute()
          ->fetchField();
        $sandbox['#finished'] = 0;
      }
      else {
        db_update($table)
          ->fields(array(
          'language' => $langcode,
        ))
          ->execute();
      }
    }
  }
  elseif ($sandbox['last'] <= $sandbox['total'] && $sandbox['total'] > 0) {

    // Update og_memberships.
    $batch_size = 200;
    db_update('og_membership')
      ->fields(array(
      'language' => $langcode,
    ))
      ->condition('id', $sandbox['last'], '>')
      ->execute();
    $sandbox['last'] += $batch_size;
    $sandbox['#finished'] = min(0.99, $sandbox['last'] / $sandbox['total']);
  }
  else {

    // Finished processing.
    $sandbox['#finished'] = 1;
  }
}