Эх сурвалжийг харах

Issue #2567053: Removed hard-coding of chado from .install files

Lacey Sanderson 9 жил өмнө
parent
commit
bb0d7c55c9

+ 6 - 21
tripal_analysis/tripal_analysis.install

@@ -300,28 +300,13 @@ function tripal_analysis_update_7200() {
   // 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');
 
-  // set the analysis_property as default
+  // Set the analysis_property as default.
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'analysis_property'")->fetchField();
-
-    // Add in the default vocabulary.
-    // If a record already exists then don't change it.
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'analysisprop')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'analysisprop',
-          'field_name' => 'type_id',
-          'cv_id' => $cv_id
-        ))
-        ->execute();
+    $is_set = tripal_get_default_cv('analysisprop', 'type_id');
+    if (!$is_set) {
+      tripal_set_default_cv('analysisprop','type_id', 'analysis_property');
     }
   }
   catch (\PDOException $e) {
@@ -424,4 +409,4 @@ function tripal_analysis_update_7201() {
       ->condition('name', $view_name)
       ->execute();
   }
-}
+}

+ 61 - 101
tripal_contact/tripal_contact.install

@@ -247,51 +247,31 @@ function tripal_contact_update_7200() {
   module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');
 
 
-  // add the contact_type CV
+  // Add the contact_type CV
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_contact'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-      ->fields(array(
-        'name' => 'tripal_contact',
-        'definition' => 'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
-      ))
-      ->execute();
-    }
-
-    // make this CV the defaults for the contact properties and contact types
-    // If a record already exists then don't change it.
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'contact')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-           'table_name' => 'contact',
-           'field_name' => 'type_id',
-           'cv_id' => $cv_id
-         ))
-        ->execute();
+    // First we add the cv.
+    // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
+    $cv = tripal_insert_cv(
+      'tripal_contact',
+      'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
+    );
+    if ($cv) {
+      $cv_id = $cv->cv_id;
+
+      // Set as Default CV for contact types.
+      $is_set = tripal_get_default_cv('contact', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('contact','type_id', 'tripal_contact', $cv_id);
+      }
+
+      // Set as Default CV for contact properties.
+      $is_set = tripal_get_default_cv('contactprop', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('contactprop','type_id', 'tripal_contact', $cv_id);
+      }
     }
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'contactprop')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'contactprop',
-          'field_name' => 'type_id',
-          'cv_id' => $cv_id
-        ))
-        ->execute();
+    else {
+      throw new DrupalUpdateException('Failed to add tripal_contact vocabulary.');
     }
   }
   catch (\PDOException $e) {
@@ -300,70 +280,50 @@ function tripal_contact_update_7200() {
   }
 
 
-  // add the contact_relationship CV
+  // Add the contact_relationship CV
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_relationship'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-      ->fields(array(
-        'name' => 'contact_relationship',
-        'definition' => 'Contains types of relationships between contacts.'
-      ))
-      ->execute();
+    // First we add the cv.
+    // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
+    $cv = tripal_insert_cv(
+      'contact_relationship',
+      'Contains types of relationships between contacts.'
+    );
+    if ($cv) {
+      $cv_id = $cv->cv_id;
+
+      // Set as Default CV for contact relationships.
+      $is_set = tripal_get_default_cv('contact_relationship', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('contact_relationship','type_id', 'contact_relationship', $cv_id);
+      }
     }
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'contact_relationship')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      // add the default
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'contact_relationship',
-          'field_name' => 'type_id',
-          'cv_id' => $cv_id
-          ))
-        ->execute();
+    else {
+      throw new DrupalUpdateException('Failed to add contact_relationship vocabulary.');
     }
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
-    throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
+    throw new DrupalUpdateException('Failed to add contact_relationship vocabulary: '. $error);
   }
 
-  // add the contact_type CV
+  // Add the contact_type CV (not default).
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_type'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-      ->fields(array(
-        'name' => 'contact_type',
-        'definition' => 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
-      ))
-      ->execute();
-    }
+    $cv = tripal_insert_cv(
+      'contact_type',
+      'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
+    );
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
   }
 
-  // add the contact_property CV
+  // Add the contact_property CV (not default).
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_property'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-      ->fields(array(
-        'name' => 'contact_property',
-        'definition' => 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
-      ))
-      ->execute();
-    }
+    $cv = tripal_insert_cv(
+      'contact_property',
+      'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
+    );
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
@@ -407,25 +367,25 @@ function tripal_contact_update_7201() {
   try {
     $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'contactprop_type_id_fkey'))->fetchField();
     if ($fkey_exists) {
-      db_query('
-        ALTER TABLE chado.contactprop
+      chado_query('
+        ALTER TABLE {contactprop}
         DROP CONSTRAINT contactprop_type_id_fkey CASCADE
       ');
-      db_query('
-        ALTER TABLE chado.contactprop
+      chado_query('
+        ALTER TABLE {contactprop}
         DROP CONSTRAINT contactprop_contact_id_fkey CASCADE
       ');
     }
-    db_query('
-      ALTER TABLE chado.contactprop
+    chado_query('
+      ALTER TABLE {contactprop}
       ADD CONSTRAINT contactprop_type_id_fkey
-      FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
+      FOREIGN KEY (type_id) REFERENCES {cvterm} (cvterm_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
-    db_query('
-      ALTER TABLE chado.contactprop
+    chado_query('
+      ALTER TABLE {contactprop}
       ADD CONSTRAINT contactprop_contact_id_fkey
-      FOREIGN KEY (contact_id) REFERENCES chado.contact (contact_id)
+      FOREIGN KEY (contact_id) REFERENCES {contact} (contact_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
   }

+ 5 - 3
tripal_cv/tripal_cv.install

@@ -73,9 +73,10 @@ function tripal_cv_install() {
 function tripal_cv_uninstall() {
 
   // drop the tripal_obo_temp table
+  // @todo: Remove hardcoding of chado schema name.
   if (db_table_exists('chado.tripal_obo_temp')) {
-    $sql = "DROP TABLE chado.tripal_obo_temp";
-    db_query($sql);
+    $sql = "DROP TABLE {tripal_obo_temp}";
+    chado_query($sql);
   }
 }
 
@@ -90,6 +91,7 @@ function tripal_cv_create_tripal_obo_temp() {
   // do not want to use the Tripal Custom Table API because we don't want it to appear in the
   // list of custom tables.  It needs to be available for the Tripal Chado API so we create it
   // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
+  // @todo: Remove hardcoding of chado schema name.
   if (!db_table_exists('chado.tripal_obo_temp')) {
     $sql = "
       CREATE TABLE {tripal_obo_temp} (
@@ -334,4 +336,4 @@ function tripal_cv_update_dependencies() {
   $dependencies = array();
 
   return $dependencies;
-}
+}

+ 30 - 30
tripal_featuremap/tripal_featuremap.install

@@ -548,73 +548,73 @@ function tripal_featuremap_update_7201() {
     $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'featuremapprop_type_id_fkey'))->fetchField();
     if ($fkey_exists) {
       // featuremapprop table
-      db_query('
-        ALTER TABLE chado.featuremapprop
+      chado_query('
+        ALTER TABLE {featuremapprop}
         DROP CONSTRAINT featuremapprop_type_id_fkey CASCADE
       ');
-      db_query('
-        ALTER TABLE chado.featuremapprop
+      chado_query('
+        ALTER TABLE {featuremapprop}
         DROP CONSTRAINT featuremapprop_featuremap_id_fkey CASCADE
       ');
     }
-    db_query('
-      ALTER TABLE chado.featuremapprop
+    chado_query('
+      ALTER TABLE {featuremapprop}
       ADD CONSTRAINT featuremapprop_type_id_fkey
-      FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
+      FOREIGN KEY (type_id) REFERENCES {cvterm} (cvterm_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
-    db_query('
-      ALTER TABLE chado.featuremapprop
+    chado_query('
+      ALTER TABLE {featuremapprop}
       ADD CONSTRAINT featuremapprop_featuremap_id_fkey
-      FOREIGN KEY (featuremap_id) REFERENCES chado.featuremap (featuremap_id)
+      FOREIGN KEY (featuremap_id) REFERENCES {featuremap} (featuremap_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
 
     // featuremap_dbref table
     if ($fkey_exists) {
-      db_query('
-        ALTER TABLE chado.featuremap_dbxref
+      chado_query('
+        ALTER TABLE {featuremap_dbxref}
         DROP CONSTRAINT featuremap_dbxref_dbxref_id_fkey CASCADE
       ');
-      db_query('
-        ALTER TABLE chado.featuremap_dbxref
+      chado_query('
+        ALTER TABLE {featuremap_dbxref}
         DROP CONSTRAINT featuremap_dbxref_featuremap_id_fkey CASCADE
       ');
     }
-    db_query('
-      ALTER TABLE chado.featuremap_dbxref
+    chado_query('
+      ALTER TABLE {featuremap_dbxref}
       ADD CONSTRAINT featuremap_dbxref_dbxref_id_fkey
-      FOREIGN KEY (dbxref_id) REFERENCES chado.dbxref (dbxref_id)
+      FOREIGN KEY (dbxref_id) REFERENCES {dbxref} (dbxref_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
-    db_query('
-      ALTER TABLE chado.featuremap_dbxref
+    chado_query('
+      ALTER TABLE {featuremap_dbxref}
       ADD CONSTRAINT featuremap_dbxref_featuremap_id_fkey
-      FOREIGN KEY (featuremap_id) REFERENCES chado.featuremap (featuremap_id)
+      FOREIGN KEY (featuremap_id) REFERENCES {featuremap} (featuremap_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
 
     // featureposprop
     if ($fkey_exists) {
-      db_query('
-        ALTER TABLE chado.featureposprop
+      chado_query('
+        ALTER TABLE {featureposprop}
         DROP CONSTRAINT featureposprop_type_id_fkey CASCADE
       ');
-      db_query('
-        ALTER TABLE chado.featureposprop
+      chado_query('
+        ALTER TABLE {featureposprop}
         DROP CONSTRAINT featureposprop_featurepos_id_fkey CASCADE
       ');
     }
-    db_query('
-      ALTER TABLE chado.featureposprop
+    chado_query('
+      ALTER TABLE {featureposprop}
       ADD CONSTRAINT featureposprop_type_id_fkey
-      FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
+      FOREIGN KEY (type_id) REFERENCES {cvterm} (cvterm_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
-    db_query('
-      ALTER TABLE chado.featureposprop
+    chado_query('
+      ALTER TABLE {featureposprop}
       ADD CONSTRAINT featureposprop_featurepos_id_fkey
-      FOREIGN KEY (featurepos_id) REFERENCES chado.featurepos (featurepos_id)
+      FOREIGN KEY (featurepos_id) REFERENCES {featurepos} (featurepos_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
   }

+ 42 - 81
tripal_library/tripal_library.install

@@ -269,15 +269,15 @@ function tripal_library_update_7200() {
   // 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'. We cannot use the Tripal API calls
-  // because during upgrade the tripal_core should also be disabled
+  // rename that to just be 'library_type'.
   try {
-    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
+    $check = chado_query("SELECT cv_id FROM {cv} WHERE name = 'library_type'")->fetchObject();
     if (!$check->cv_id) {
-      $sql = "UPDATE chado.cv SET name = 'library_type' WHERE name = 'tripal_library_types'";
-      db_query($sql);
+      $sql = "UPDATE {cv} SET name = 'library_type' WHERE name = 'tripal_library_types'";
+      chado_query($sql);
     }
   }
   catch (\PDOException $e) {
@@ -287,13 +287,18 @@ function tripal_library_update_7200() {
 
   // add the library_property CV
   try {
-    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchObject();
-    if (!$check->cv_id) {
-      $sql = "INSERT INTO chado.cv (name, definition) VALUES (
-        'library_property',
-        'Contains properties for libraries.')
-      ";
-      db_query($sql);
+    $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) {
@@ -303,13 +308,20 @@ function tripal_library_update_7200() {
 
   // add the library_type CV
   try {
-    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
-    if (!$check->cv_id) {
-      $sql = "INSERT INTO chado.cv (name, definition) VALUES (
-        'library_type',
-        'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).')
-      ";
-      db_query($sql);
+    // 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) {
@@ -321,19 +333,17 @@ function tripal_library_update_7200() {
   // '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'
-  // We cannot use the Tripal API calls in the 7000 update
-  // because during upgrade the tripal_core should also be disabled
-  $sql = "
-    UPDATE chado.cvterm
-    SET
-      name = 'Library Description',
-      cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'library_property')
-    WHERE
-      name = 'library_description' AND
-      cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'tripal')
-  ";
   try {
-    db_query($sql);
+    $sql = "
+      UPDATE chado.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();
@@ -377,55 +387,6 @@ function tripal_library_update_7200() {
       throw new DrupalUpdateException('Could not move library taxonomy terms: '. $error);
     }
   }
-
-  // set the default vocabularies
-  // library_type
-  try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchField();
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'library')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'library',
-          'field_name' => 'type_id',
-          'cv_id' => $cv_id
-        ))
-        ->execute();
-    }
-  }
-  catch (\PDOException $e) {
-    $error = $e->getMessage();
-    throw new DrupalUpdateException('Failed to set library_type vocabulary as default: '. $error);
-  }
-  // library_property
-  try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchField();
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'libraryprop')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'libraryprop',
-          'field_name' => 'type_id',
-          'cv_id' => $cv_id
-        ))
-        ->execute();
-    }
-  }
-  catch (\PDOException $e) {
-    $error = $e->getMessage();
-    throw new DrupalUpdateException('Failed to set the library_property vocabulary as default: '. $error);
-  }
-
 }
 
 /**
@@ -483,4 +444,4 @@ function tripal_library_update_7201() {
       ->condition('name', $view_name)
       ->execute();
   }
-}
+}

+ 35 - 58
tripal_project/tripal_project.install

@@ -143,34 +143,24 @@ function tripal_project_update_7200() {
   // 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');
 
-  // add the project_property CV
+  // Add the project_property CV
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'project_property'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-        ->fields(array(
-          'name' => 'project_property',
-          'definition' => 'Contains properties for projects.'
-        ))
-        ->execute();
-    }
-    // use the new project_property CV we just added
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'projectprop')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-        'table_name' => 'projectprop',
-        'field_name' => 'type_id',
-        'cv_id' => $cv_id
-        ))
-        ->execute();
+    // First we add the cv.
+    // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
+    $cv = tripal_insert_cv(
+      'project_property',
+      'Contains properties for projects'
+    );
+    if ($cv) {
+      $cv_id = $cv->cv_id;
+
+      // Set as Default CV for contact types.
+      $is_set = tripal_get_default_cv('projectprop', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('projectprop','type_id', 'project_property', $cv_id);
+      }
     }
   }
   catch (\PDOException $e) {
@@ -180,31 +170,20 @@ function tripal_project_update_7200() {
 
   // add the project_relationship CV
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'project_relationship'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-      ->fields(array(
-        'name' => 'project_relationship',
-        'definition' => 'Contains types of relationships between projects.'
-      ))
-      ->execute();
-    }
-    // use the new project_property CV we just added
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'project_relationship')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'project_relationship',
-          'field_name' => 'type_id',
-          'cv_id' => $cv_id
-        ))
-        ->execute();
+    // First we add the cv.
+    // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
+    $cv = tripal_insert_cv(
+      'project_relationship',
+      'Contains types of relationships between projects.'
+    );
+    if ($cv) {
+      $cv_id = $cv->cv_id;
+
+      // Set as Default CV for contact types.
+      $is_set = tripal_get_default_cv('project_relationship', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('project_relationship','type_id', 'project_relationship', $cv_id);
+      }
     }
   }
   catch (\PDOException $e) {
@@ -216,19 +195,17 @@ function tripal_project_update_7200() {
   // 'tripal' CV.  It should be stored in the new project_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 'Project Description'
-  // We cannot use the Tripal API calls'because during upgrade the tripal_core
-  // should also be disabled
   $sql = "
-    UPDATE chado.cvterm CVT
+    UPDATE {cvterm} CVT
     SET
       name = 'Project Description',
-      cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'project_property')
+      cv_id = (SELECT cv_id FROM {cv} WHERE name = 'project_property')
     WHERE
       name = 'project_description' AND
-      cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'tripal')
+      cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal')
   ";
   try {
-    db_query($sql);
+    chado_query($sql);
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
@@ -254,4 +231,4 @@ function tripal_project_update_dependencies() {
   );
 
   return $dependencies;
-}
+}

+ 47 - 87
tripal_pub/tripal_pub.install

@@ -285,51 +285,30 @@ function tripal_pub_update_7200() {
   // 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');
 
   // add the tripal_pub CV and set it to be the default for pub types and pub properties
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_pub'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-        ->fields(array(
-          'name' => 'tripal_pub',
-          'definition' => 'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
-        ))
-        ->execute();
-    }
-    // use the new pub_property CV we just added
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'pub')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'pub',
-          'field_name' => 'type_id',
-          'cv_id' => $cv_id
-        ))
-        ->execute();
-    }
-
-    // use the new pub_property CV we just added
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'pubprop')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'pubprop',
-          'field_name' => 'type_id',
-          'cv_id' => $cv_id
-        ))
-        ->execute();
+    // First we add the cv.
+    // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
+    $cv = tripal_insert_cv(
+      'tripal_pub',
+      'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
+    );
+    if ($cv) {
+      $cv_id = $cv->cv_id;
+
+      // Set as Default CV for pub types.
+      $is_set = tripal_get_default_cv('pub', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('pub','type_id', 'tripal_pub', $cv_id);
+      }
+
+      // Set as Default CV for pub properties.
+      $is_set = tripal_get_default_cv('pub', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('pubprop','type_id', 'tripal_pub', $cv_id);
+      }
     }
   }
   catch (\PDOException $e) {
@@ -357,16 +336,10 @@ function tripal_pub_update_7200() {
 
   // add the pub_type CV
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_type'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-      ->fields(array(
-        'name' => 'pub_type',
-        'definition' => 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
-      ))
-      ->execute();
-    }
+    $cv = tripal_insert_cv(
+      'pub_type',
+      'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
+    );
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
@@ -375,31 +348,18 @@ function tripal_pub_update_7200() {
 
   // add the pub_relationship CV
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_relationship'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-      ->fields(array(
-        'name' => 'pub_relationship',
-        'definition' => 'Contains types of relationships between publications.'
-      ))
-      ->execute();
-    }
-    // use the new pub_property CV we just added
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'pub_relationship')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'pub_relationship',
-          'field_name' => 'type_id',
-          'cv_id' => $cv_id
-        ))
-        ->execute();
+    $cv = tripal_insert_cv(
+      'pub_relationship',
+      'Contains types of relationships between publications.'
+    );
+    if ($cv) {
+      $cv_id = $cv->cv_id;
+
+      // Set as Default CV for pub relationships.
+      $is_set = tripal_get_default_cv('pub_relationship', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('pub_relationship','type_id', 'pub_relationship', $cv_id);
+      }
     }
   }
   catch (\PDOException $e) {
@@ -444,25 +404,25 @@ function tripal_pub_update_7201() {
   try {
     $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'pubauthor_contact_pubauthor_id_fkey'))->fetchField();
     if ($fkey_exists) {
-      db_query('
-        ALTER TABLE chado.pubauthor_contact
+      chado_query('
+        ALTER TABLE {pubauthor_contact}
         DROP CONSTRAINT pubauthor_contact_pubauthor_id_fkey CASCADE
       ');
-      db_query('
-        ALTER TABLE chado.pubauthor_contact
+      chado_query('
+        ALTER TABLE {pubauthor_contact}
         DROP CONSTRAINT pubauthor_contact_contact_id_fkey CASCADE
       ');
     }
-    db_query('
-      ALTER TABLE chado.pubauthor_contact
+    chado_query('
+      ALTER TABLE {pubauthor_contact}
       ADD CONSTRAINT pubauthor_contact_pubauthor_id_fkey
-      FOREIGN KEY (pubauthor_id) REFERENCES chado.pubauthor (pubauthor_id)
+      FOREIGN KEY (pubauthor_id) REFERENCES {pubauthor} (pubauthor_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
-    db_query('
-      ALTER TABLE chado.pubauthor_contact
+    chado_query('
+      ALTER TABLE {pubauthor_contact}
       ADD CONSTRAINT pubauthor_contact_contact_id_fkey
-      FOREIGN KEY (contact_id) REFERENCES chado.contact (contact_id)
+      FOREIGN KEY (contact_id) REFERENCES {contact} (contact_id)
       ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
     ');
   }

+ 56 - 82
tripal_stock/tripal_stock.install

@@ -217,36 +217,28 @@ function tripal_stock_update_7200() {
   // 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');
 
   // add the stock_relationshp CV
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_relationship'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-        ->fields(array(
-          'name' => 'stock_relationship',
-          'definition' => 'Contains types of relationships between stocks.'
-        ))
-        ->execute();
-    }
-    // for backwards compatibility, get the previously set stock relationship CV, otherwise
-    // use the new stock_relationship CV we just added
-    $default_stockrel_cv = variable_get('chado_stock_relationship_cv', $cv_id);
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'stock_relationship')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-          'table_name' => 'stock_relationship',
-          'field_name' => 'type_id',
-          'cv_id' => $default_stockrel_cv
-        ))
-        ->execute();
+    // First we add the cv.
+    // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
+    $cv = tripal_insert_cv(
+      'stock_relationship',
+      'Contains types of relationships between stocks.'
+    );
+    if ($cv) {
+      $cv_id = $cv->cv_id;
+
+      // for backwards compatibility, get the previously set stock relationship CV, otherwise
+      // use the new stock_relationship CV we just added
+      $default_stockrel_cv = variable_get('chado_stock_relationship_cv', $cv_id);
+
+      // Set as Default CV for stock relationship types.
+      $is_set = tripal_get_default_cv('stock_relationship', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('stock_relationship','type_id', 'stock_relationship', $default_stockrel_cv);
+      }
     }
   }
   catch (\PDOException $e) {
@@ -256,33 +248,24 @@ function tripal_stock_update_7200() {
 
   // add the stock_property CV
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_property'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-        ->fields(array(
-          'name' => 'stock_property',
-          'definition' => 'Contains properties for stocks.'
-        ))
-        ->execute();
-    }
-    // for backwards compatibility, get the previously set stock property CV, otherwise
-    // use the new stock_property CV we just added
-    $default_stockprop_cv = variable_get('chado_stock_prop_types_cv', $cv_id);
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'stockprop')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-        'table_name' => 'stockprop',
-        'field_name' => 'type_id',
-        'cv_id' => $default_stockprop_cv
-        ))
-        ->execute();
+    // First we add the cv.
+    // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
+    $cv = tripal_insert_cv(
+      'stock_property',
+      'Contains properties for stocks.'
+    );
+    if ($cv) {
+      $cv_id = $cv->cv_id;
+
+      // for backwards compatibility, get the previously set stock property CV, otherwise
+      // use the new stock_property CV we just added
+      $default_stockprop_cv = variable_get('chado_stock_prop_types_cv', $cv_id);
+
+      // Set as Default CV for stock properties.
+      $is_set = tripal_get_default_cv('stockprop', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('stockprop','type_id', 'stock_property', $default_stockprop_cv);
+      }
     }
   }
   catch (\PDOException $e) {
@@ -293,33 +276,24 @@ function tripal_stock_update_7200() {
 
   // add the stock_type CV
   try {
-    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_type'")->fetchField();
-    if (!$cv_id) {
-      // add the vocabulary
-      $cv_id = db_insert('chado.cv')
-      ->fields(array(
-        'name' => 'stock_type',
-        'definition' => 'Contains a list of types for stocks.'
-      ))
-      ->execute();
-    }
-    // for backwards compatibility, get the previously set stock types CV, otherwise
-    // use the new stock_type CV we just added
-    $default_stocktype_cv = variable_get('chado_stock_types_cv', $cv_id);
-    $cdi = db_select('tripal_cv_defaults', 't')
-      ->fields('t', array('cv_default_id'))
-      ->condition('table_name', 'stock')
-      ->condition('field_name', 'type_id')
-      ->execute()
-      ->fetchField();
-    if (!$cdi) {
-      db_insert('tripal_cv_defaults')
-        ->fields(array(
-        'table_name' => 'stock',
-        'field_name' => 'type_id',
-        'cv_id' => $default_stocktype_cv
-        ))
-        ->execute();
+    // First we add the cv.
+    // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
+    $cv = tripal_insert_cv(
+      'stock_type',
+      'Contains a list of types for stocks.'
+    );
+    if ($cv) {
+      $cv_id = $cv->cv_id;
+
+      // for backwards compatibility, get the previously set stock types CV, otherwise
+      // use the new stock_type CV we just added
+      $default_stocktype_cv = variable_get('chado_stock_types_cv', $cv_id);
+
+      // Set as Default CV for stock types.
+      $is_set = tripal_get_default_cv('stock', 'type_id');
+      if (!$is_set) {
+        tripal_set_default_cv('stock','type_id', 'stock_type', $default_stocktype_cv);
+      }
     }
   }
   catch (\PDOException $e) {
@@ -360,4 +334,4 @@ function tripal_stock_update_dependencies() {
   );
 
   return $dependencies;
-}
+}