You are here

function blockcache_alter_update_7000 in Block Cache Alter 7

Add the "bid" column to {blockcache_alter} and backfill block IDs.

File

./blockcache_alter.install, line 54
Install file.

Code

function blockcache_alter_update_7000() {

  // Ensure we don't run this update for existing installs; D6 updates only.
  if (!db_field_exists('blockcache_alter', 'bid')) {

    // Define the bid column and primary key.
    $spec = array(
      'type' => 'serial',
      'not null' => TRUE,
      'description' => 'Primary Key: Unique block ID.',
    );
    $keys = array(
      'primary key' => array(
        'bid',
      ),
    );

    // Add the bid column along with the new primary key.
    db_add_field('blockcache_alter', 'bid', $spec, $keys);

    // If there are existing block cache alter entries, find their block IDs.
    $bcas = db_select('blockcache_alter', 'bca', array(
      'fetch' => PDO::FETCH_ASSOC,
    ))
      ->fields('bca');
    $bcas
      ->join('block', 'b', 'bca.module = %alias.module AND bca.delta = %alias.delta');
    $bcas
      ->fields('b', array(
      'bid',
    ));
    $results = $bcas
      ->execute();

    // If results were returned, truncate the existing block cache alter table,
    // then insert all of the database records we previously found/built.
    if (!empty($results)) {
      db_truncate('blockcache_alter')
        ->execute();
      $insert = db_insert('blockcache_alter')
        ->fields(array(
        'module',
        'delta',
        'cache',
        'bid',
      ));
      foreach ($results as $bca) {
        $insert
          ->values($bca);
      }
      $insert
        ->execute();
    }
  }
}