You are here

function image_replace_update_8102 in Image Replace 8

Switch to hash based key system.

File

./image_replace.install, line 74
Database schema for image replace module.

Code

function image_replace_update_8102(&$sandbox) {
  $database = Database::getConnection();
  $image_replace_dataset = $database
    ->select('image_replace')
    ->fields('image_replace', [
    'target_style',
    'target_uri',
    'replacement_uri',
  ])
    ->condition('target_uri', '', '<>')
    ->execute()
    ->fetchAll();
  $schema = $database
    ->schema();
  if (!$schema
    ->fieldExists('image_replace', 'target_uri_hash')) {
    $schema
      ->addField('image_replace', 'target_uri_hash', [
      'type' => 'varchar',
      'length' => '64',
      'not null' => TRUE,
      'default' => '',
      'description' => 'Hash of target uris. Used as part of primary key',
    ]);
  }

  // Convert existing entries over to hash.
  foreach ($image_replace_dataset as $key => $data) {
    $target_uri_hash = hash('sha256', $data->target_uri);
    $database
      ->update('image_replace')
      ->fields([
      'target_uri_hash' => $target_uri_hash,
    ])
      ->condition('target_style', $data->target_style)
      ->condition('target_uri', $data->target_uri)
      ->execute();
  }
  $schema
    ->dropPrimaryKey('image_replace');
  $schema
    ->dropField('image_replace', 'target_uri');
  $schema
    ->addPrimaryKey('image_replace', [
    'target_style',
    'target_uri_hash',
  ]);
}