You are here

function scheduler_update_7103 in Scheduler 7

Update role_permission table with clean machine name.

File

./scheduler.install, line 160
Installation file for Scheduler module.

Code

function scheduler_update_7103() {

  // Change all values of 'schedule (un)publishing of nodes' to the cleaner
  // 'schedule publishing of nodes'.
  // @see http://www.drupal.org/node/2538002
  //
  // Updates done in two stages to avoid integrity constraint violation. First
  // select all role ids which already have the new permission value.
  $query = db_select('role_permission', 'rp')
    ->fields('rp', array(
    'rid',
    'permission',
  ))
    ->condition('permission', 'schedule publishing of nodes');

  // Delete the rows for these roles which also have the old permission value,
  // as these are no longer needed and should not be updated to the new value.
  $rows_deleted = 0;
  if ($rows_to_delete = $query
    ->execute()
    ->fetchCol()) {
    $rows_deleted = db_delete('role_permission')
      ->condition('rid', $rows_to_delete, 'IN')
      ->condition('permission', 'schedule (un)publishing of nodes', '=')
      ->execute();
  }

  // Now update any other rows which still have the old permission value.
  $rows_updated = db_update('role_permission')
    ->fields(array(
    'permission' => 'schedule publishing of nodes',
  ))
    ->condition('permission', 'schedule (un)publishing of nodes', '=')
    ->execute();
  return format_plural($rows_updated, '1 row updated', '@count rows updated') . ', ' . format_plural($rows_deleted, '1 row deleted', '@count rows deleted') . ' ' . t('in role_permission table');
}