You are here

function values_update_6101 in Values 6

Implementation of hook_update_N(). Condense schema into a single table to handle exportables easily.

File

./values.install, line 88
Install file for Values module.

Code

function values_update_6101(&$sandbox) {
  $ret = array();

  // Define the new table
  $schema['values_list'] = array(
    'description' => t('List of configured value lists.'),
    'export' => array(
      'key' => 'name',
      'identifier' => 'values',
      'default hook' => 'default_values_values',
      'api' => array(
        'owner' => 'values',
        'api' => 'default_values_list',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'name' => array(
        'description' => t('Unique ID for value lists.'),
        'type' => 'varchar',
        'length' => 255,
      ),
      'description' => array(
        'description' => t('A human-readable name of a value list'),
        'type' => 'varchar',
        'length' => 255,
      ),
      'data' => array(
        'description' => t('Configured list of values.'),
        'type' => 'text',
        'size' => 'big',
        'serialize' => TRUE,
      ),
    ),
    'unique keys' => array(
      'name' => array(
        'name',
      ),
    ),
    'primary key' => array(
      'name',
    ),
  );
  db_create_table($ret, 'values_list', $schema['values_list']);

  // Migrate existing data to the new schema
  $values_sets = db_query('SELECT * FROM {values_sets}');
  while ($set = db_fetch_object($values_sets)) {
    $values = new stdClass();
    $values->name = $set->id;
    $values->description = $set->description;
    $values->data = array();
    $values_values = db_query("SELECT * FROM {values_values} WHERE id = '%s' ORDER BY weight ASC", $set->id);
    while ($value = db_fetch_array($values_values)) {
      unset($value['id']);
      $values->data[] = $value;
    }
    drupal_write_record('values_list', $values);
  }

  // Remove old tables
  db_drop_table($ret, 'values_sets');
  db_drop_table($ret, 'values_values');
  return $ret;
}