function tripal_library_update_7200

2.x tripal_library.install tripal_library_update_7200()

This is the required update for tripal_library when upgrading from Drupal core API 6.x.

File

tripal_library/tripal_library.install, line 267
Installation of the library module

Code

function tripal_library_update_7200() {
  // Make sure we have the full API loaded this will help during a
  // site upgrade when the tripal_core module is disabled.
  module_load_include('module', 'tripal_core', 'tripal_core');
  tripal_core_import_api();
  module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');

  // the library types were formerly in a vocabulary named 'tripal_library_types'.
  // rename that to just be 'library_type'.
  try {
    $check = chado_query("SELECT cv_id FROM {cv} WHERE name = 'library_type'")->fetchObject();
    if (!$check->cv_id) {
      $sql = "UPDATE {cv} SET name = 'library_type' WHERE name = 'tripal_library_types'";
      chado_query($sql);
    }
  }
  catch (\PDOException $e) {
    $error = $e->getMessage();
    throw new DrupalUpdateException('Failed to change the vocabulary from tripal_library_types to library_type: ' . $error);
  }

  // add the library_property CV
  try {
    $cv = tripal_insert_cv(
    'library_property', 
    'Contains properties for libraries.'
    );
    if ($cv) {
      $cv_id = $cv->cv_id;

      // Set as Default CV for library properties.
      $is_set = tripal_get_default_cv('libraryprop', 'type_id');
      if (!$is_set) {
        tripal_set_default_cv('libraryprop', 'type_id', 'library_property', $cv_id);
      }
    }
  }
  catch (\PDOException $e) {
    $error = $e->getMessage();
    throw new DrupalUpdateException('Failed to add library_property vocabulary: ' . $error);
  }

  // add the library_type CV
  try {
    // Note: tripal_insert_cv will only insert it if doesn't already exist
    // so this doesn't conflict with the update above.
    $cv = tripal_insert_cv(
    'library_type', 
    'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
    );
    if ($cv) {
      $cv_id = $cv->cv_id;

      // Set as Default CV for library types.
      $is_set = tripal_get_default_cv('library', 'type_id');
      if (!$is_set) {
        tripal_set_default_cv('library', 'type_id', 'library_type', $cv_id);
      }
    }
  }
  catch (\PDOException $e) {
    $error = $e->getMessage();
    throw new DrupalUpdateException('Failed to add library_type vocabulary: ' . $error);
  }

  // For Tripal in Drupal 6 the library_description cvterm was stored in the
  // 'tripal' CV.  It should be stored in the new library_property CV that
  // is added by this module for Tripal 2.0 and Drupal 7.  So, we need to
  // reset the CV ID for that term and rename the term to 'Library Description'
  try {
    $sql = "
      UPDATE {cvterm}
      SET
        name = 'Library Description',
        cv_id = (SELECT cv_id FROM {cv} WHERE name = 'library_property')
      WHERE
        name = 'library_description' AND
        cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal')
    ";
    chado_query($sql);
  }
  catch (\PDOException $e) {
    $error = $e->getMessage();
    throw new DrupalUpdateException('Failed to change library_description property type to the library_property CV and update the name: ' . $error);
  }

  // During the upgrade from D6 to D7 the vocabulary terms assigned to libraries were
  // copied to the field_data_taxonomyextra table rather than to the correct
  // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Library'")->fetchField();
  if ($vid) {
    try {
      if (db_table_exists('field_data_taxonomyextra')) {
        // first move from the field_data_taxonomyextra table
        $sql = "
        INSERT INTO {field_data_taxonomy_vocabulary_$vid}
        (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid . "_tid)
          (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
           FROM field_data_taxonomyextra
           WHERE bundle = 'chado_feature')
        ";
        db_query($sql);
        $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_library'";
        db_query($sql);

        // next move from the field_revision_taxonomyextra table
        $sql = "
          INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
            (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid . "_tid)
          (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
           FROM field_revision_taxonomyextra
           WHERE bundle = 'chado_feature')
        ";
        db_query($sql);
        $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_library'";
        db_query($sql);
      }
    }
    catch (\PDOException $e) {
      $error = $e->getMessage();
      throw new DrupalUpdateException('Could not move library taxonomy terms: ' . $error);
    }
  }
}