You are here

function recipe_update_7003 in Recipe 7.2

Same name and namespace in other branches
  1. 7 recipe.install \recipe_update_7003()

Adds unit_key column to the recipe_node_ingredient table.

File

./recipe.install, line 347
Install, update and uninstall functions for the recipe module.

Code

function recipe_update_7003() {

  // Add the new unit_key field to the recipe_node_ingredient table.
  db_add_field('recipe_node_ingredient', 'unit_key', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // Update unit_key field from the old unit name field from the recipe_unit table.
  $data_errors = FALSE;
  $query = 'update {recipe_node_ingredient} rni set rni.unit_key = (select lower(max(ru.name)) from {recipe_unit} ru where ru.id=rni.unit_id)';
  if (db_query($query) == FALSE) {
    $data_errors = TRUE;
  }

  // For the unit keys that changed, update them to the new key.
  $changes = array(
    'pint' => 'us liquid pint',
    'quart' => 'us liquid quart',
    'gallon' => 'us gallon',
    'tablespoon (metric)' => 'tablespoon',
    'metric tablespoon' => 'tablespoon',
    'teaspoon (metric)' => 'teaspoon',
    'metric teaspoon' => 'teaspoon',
    'millilitre' => 'milliliter',
    'centilitre' => 'centiliter',
    'decilitre' => 'decilitre',
    'litre' => 'liter',
  );
  foreach ($changes as $old_key => $new_key) {
    db_update('recipe_node_ingredient')
      ->condition('unit_key', $old_key)
      ->fields(array(
      'unit_key' => $new_key,
    ))
      ->execute();
  }

  // Remove unit_id field from the recipe_node_ingredient table.
  db_drop_field('recipe_node_ingredient', 'unit_id');

  // Don't remove the recipe_unit table at this time.
  // People may have user-entered content in there.
}