You are here

function message_update_7007 in Message 7

Add "language" column to all Message's related entities.

File

./message.install, line 419
Install, update, and uninstall functions for the message module.

Code

function message_update_7007(&$sandbox) {

  // Update the existing entities, with the default language.
  $langcode = language_default()->language;
  $tables = array(
    'message_type_category',
    'message_type',
    'message',
  );
  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' => '',
      );
      db_add_field($table, 'language', $column);
      if ($table == 'message') {

        // We don't want to time out when updating messages, 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['total'] && $sandbox['last'] <= $sandbox['total']) {

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

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