You are here

function commerce_pricelist_update_7002 in Commerce Pricelist 7

Migrate pricelist uuids to ids. Depending on how many items there are this may take a while.

File

./commerce_pricelist.install, line 207
Install for a Commerce Pricelist - create the base tables for our entities.

Code

function commerce_pricelist_update_7002(&$sandbox) {
  if (db_field_exists('commerce_pricelist_item', 'pricelist_uuid')) {

    // Prepare query
    $query = db_select('commerce_pricelist_item', 'i');
    $query
      ->join('commerce_pricelist_list', 'l', 'i.pricelist_uuid = l.uuid');
    $query
      ->fields('i', array(
      'item_id',
    ))
      ->fields('l', array(
      'list_id',
    ));

    // If this is the first pass through this update function then set some variables.
    if (!isset($sandbox['total'])) {
      $count_query = $query
        ->countQuery();
      $sandbox['total'] = $count_query
        ->execute()
        ->fetchField();
      $sandbox['current'] = 0;
    }

    // How many items should be processed per pass. The higher this number is, the faster your update will
    // complete, but the more likely your server will run out of memory or timeout.
    $items_per_pass = 20;

    // Get the items to process during this pass.
    $result = $query
      ->range($sandbox['current'], $items_per_pass)
      ->execute();
    while ($row = $result
      ->fetchAssoc()) {
      db_update('commerce_pricelist_item')
        ->fields(array(
        'pricelist_id' => $row['list_id'],
      ))
        ->condition('item_id', $row['item_id'], '=')
        ->execute();

      // Increment "current" by 1.
      $sandbox['current']++;
    }

    // Lets tell the site admin what we are doing.
    drupal_set_message(t('Processing pricelist items, now at @current out of @total', array(
      '@current' => $sandbox['current'],
      '@total' => $sandbox['total'],
    )));

    // Set the value for finished. If current == total then finished will be 1, signifying we are done.
    $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
    if ($sandbox['#finished'] === 1) {
      drupal_set_message(t('Added list_id value to @items pricelist items.', array(
        '@items' => $sandbox['total'],
      )));
      drupal_set_message(t('It is now safe to drop unused pricelist_uuid column from commerce_pricelist_item table. To do so, uncomment commerce_pricelist_update_7003() and run database updates. Please backup your data first!'));
    }
  }
}