You are here

function apply_for_role_update_7000 in Apply for role 7

Same name and namespace in other branches
  1. 7.2 apply_for_role.install \apply_for_role_update_7000()

Add a non-composite primary key named aid.

File

./apply_for_role.install, line 86
Installs the Apply for Role (AFR) module.

Code

function apply_for_role_update_7000(&$sandbox) {
  $sandbox['#finished'] = 0;
  if (!db_table_exists('users_roles_apply_temp')) {
    $schema['users_roles_apply_temp'] = array(
      'fields' => array(
        'aid' => array(
          'type' => 'serial',
          'description' => 'Primary Key: Unique role application ID.',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'uid' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'description' => 'Foreign Key: {users}.uid.',
        ),
        'rid' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'description' => 'Foreign Key: {role}.rid.',
        ),
        'approved' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
          'size' => 'tiny',
          'description' => 'The status of the role application.',
        ),
        'apply_date' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'description' => 'Timestamp for when the role application was created.',
        ),
        'approve_date' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'description' => 'Timestamp for when the role application was processed.',
        ),
      ),
      'unique keys' => array(
        'uid_rid' => array(
          'uid',
          'rid',
        ),
      ),
      'primary key' => array(
        'aid',
      ),
    );
    db_create_table('users_roles_apply_temp', $schema['users_roles_apply_temp']);
  }

  // Multi-part update.
  if (!isset($sandbox['apply_from'])) {
    $sandbox['apply_from'] = 0;
    $sandbox['apply_count'] = db_query("SELECT COUNT(*) FROM {users_roles_apply}")
      ->fetchField();
  }
  else {
    $has_rows = FALSE;

    // Update this many per page load.
    $count = 1000;
    $results = db_query_range("SELECT * FROM {users_roles_apply}", $sandbox['apply_from'], $count);
    foreach ($results as $apply) {
      $has_rows = TRUE;
      db_insert('users_roles_apply_temp')
        ->fields(array(
        'uid' => $apply->uid,
        'rid' => $apply->rid,
        'approved' => $apply->approved,
        'apply_date' => $apply->apply_date,
        'approve_date' => $apply->approve_date,
      ))
        ->execute();
    }
    $sandbox['#finished'] = $sandbox['apply_from'] / $sandbox['apply_count'];
    $sandbox['apply_from'] += $count;
    if (!$has_rows) {
      db_drop_table('users_roles_apply');
      db_rename_table('users_roles_apply_temp', 'users_roles_apply');
      $sandbox['#finished'] = 1;
      return t('The <em>Apply for Role</em> table has been updated.');
    }
  }
}