You are here

function role_expire_update_8101 in Role Expire 8

Same name and namespace in other branches
  1. 2.x role_expire.install \role_expire_update_8101()

Update schema for default role_expire_length table. Convert integer days to text "# days".

File

./role_expire.install, line 66
Role expire install.

Code

function role_expire_update_8101() {
  $connection = Database::getConnection();

  // Fetch out all the current durations.
  $result = $connection
    ->query('SELECT rid,duration FROM {role_expire_length}');
  $durations = [];
  while ($row = $result
    ->fetchObject()) {
    $durations[$row->rid] = $row->duration;
  }

  // Convert the original duration column from type int to type text.
  $field = array(
    'type' => 'text',
    'size' => 'small',
    'not null' => TRUE,
    'description' => t('A strtotime-compatible default duration string.'),
  );
  $connection
    ->schema()
    ->changeField('role_expire_length', 'duration', 'duration', $field);

  // Append "days" since that's all it could've been before this revision.
  if (!empty($durations)) {
    foreach ($durations as $rid => $duration) {
      $connection
        ->update('role_expire_length')
        ->fields([
        'duration' => "{$duration} days",
      ])
        ->condition('rid', $rid)
        ->execute();
    }
  }
}