You are here

function eck_update_7006 in Entity Construction Kit (ECK) 7.2

Same name and namespace in other branches
  1. 7.3 eck.install \eck_update_7006()

Update 7006.

File

./eck.install, line 342
ECK's requirements, schemas, and logic for install and uninstall.

Code

function eck_update_7006() {

  // We already made sure in hook_requirements that the module ctools exists
  // in the system, so now we can safely enable it.
  module_enable(array(
    'ctools',
  ));

  // Lets start with then entity type table
  // Let's add an id field to the entity_type table, and set it up as a primary
  // key.
  db_drop_primary_key('eck_entity_type');
  db_add_field('eck_entity_type', 'id', array(
    'description' => "The primary identifier for an entity type",
    'type' => 'serial',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ), array(
    'primary key' => array(
      'id',
      'name',
    ),
  ));

  // Let's take the properties and translate them to the new form.
  $results = db_select('eck_entity_type', 't')
    ->fields('t')
    ->execute();
  foreach ($results as $record) {

    // @todo for each entity type we want to drop all the indexes
    // they will have to be managed dynamically by the behaviors even though
    // I don't know how to do that yet.
    $entity_table = "eck_{$record->name}";
    if (db_index_exists($entity_table, 'uid')) {
      db_drop_index($entity_table, 'uid');
    }
    if (db_index_exists($entity_table, "{$record->name}_created")) {
      db_drop_index($entity_table, "{$record->name}_created");
    }
    if (db_index_exists($entity_table, "{$record->name}_changed")) {
      db_drop_index($entity_table, "{$record->name}_changed");
    }
    $new_properties = array();
    $properties = $record->properties;
    $properties = unserialize($properties);
    foreach ($properties as $property => $status) {
      switch ($property) {
        case 'uuid':
          $new_properties['uuid'] = array(
            'label' => 'UUID',
            'type' => 'uuid',
          );
          break;
        case 'uid':
          $new_properties['uid'] = array(
            'label' => 'Author',
            'type' => 'integer',
            'behavior' => 'author',
          );
          break;
        case 'created':
          $new_properties['created'] = array(
            'label' => 'Created',
            'type' => 'integer',
            'behavior' => 'created',
          );
          break;
        case 'changed':
          $new_properties['changed'] = array(
            'label' => 'Changed',
            'type' => 'integer',
            'behavior' => 'changed',
          );
          break;
        case 'state':
          $new_properties['state'] = array(
            'label' => 'State',
            'type' => 'positive_integer',
          );
          break;
      }
    }
    $custom_properties = $record->custom_properties;
    $custom_properties = unserialize($custom_properties);
    foreach ($custom_properties as $property => $info) {
      $type = $info['type'];
      $label = $info['label'];
      switch ($type) {
        case 'text':
          $new_properties[$property] = array(
            'label' => $label,
            'type' => $type,
          );
          break;
        case 'decimal':
          $new_properties[$property] = array(
            'label' => $label,
            'type' => $type,
          );
          break;
        default:
          $new_properties[$property] = array(
            'label' => $label,
            'type' => 'integer',
          );
          break;
      }
    }
    $encode_new = drupal_json_encode($new_properties);
    db_update('eck_entity_type')
      ->fields(array(
      'properties' => $encode_new,
    ))
      ->condition('name', $record->name, '=')
      ->execute();
  }

  // Now we can drop the custom_properties field now that everything has
  // been moved.
  db_drop_field('eck_entity_type', 'custom_properties');

  // Now the changes to the eck_bundle table.
  // First lets drop that index.
  db_drop_index('eck_bundle', 'entity_type_bundle_name');
  db_add_field('eck_bundle', 'machine_name', array(
    'description' => "A combination of the entity type and the name of " . "this bundle, this combination is unique",
    'type' => 'varchar',
    'length' => 128,
    'default' => "",
    'not null' => TRUE,
  ));
}