tripal_chado/tripal_chado.install

Summary

Maintainability
Test Coverage
<?php
/**
 * @file
 * Contains functions used to install/uninstall tripal_chado.
 */

use \Drupal\Core\Database\Database;

/**
 * Implements hook_install().
 */
function tripal_chado_install($is_syncing) {
  // Reserves Chado common schemas in 'reserved_schema_patterns' settings.
  $config = \Drupal::service('config.factory')
    ->getEditable('tripaldbx.settings')
  ;
  $reserved_schema_patterns = $config->get('reserved_schema_patterns') ?? [];
  $reserved_schema_patterns['frange'] = 'feature range (chado)';
  $reserved_schema_patterns['genetic_code'] = 'genetic code (chado)';
  $reserved_schema_patterns['so'] = 'sequence ontology (chado)';
  $reserved_schema_patterns[Drupal\tripal_chado\Task\ChadoUpgrader::CHADO_REF_SCHEMA_13] = 'upgrade template (chado)';
  $config->set('reserved_schema_patterns', $reserved_schema_patterns)->save();

  $test_schema_base_names = $config->get('test_schema_base_names') ?? [];
  $test_schema_base_names['chado'] = '_test_chado';
  $config->set('test_schema_base_names', $test_schema_base_names)->save();

}

/**
 * Implements hook_uninstall().
 */
function tripal_chado_uninstall() {
  // Unreserves Chado common schemas in 'reserved_schema_patterns' settings.
  $config = \Drupal::service('config.factory')
    ->getEditable('tripaldbx.settings')
  ;
  $reserved_schema_patterns = $config->get('reserved_schema_patterns') ?? [];
  unset($reserved_schema_patterns['frange']);
  unset($reserved_schema_patterns['genetic_code']);
  unset($reserved_schema_patterns['so']);
  $config->set('reserved_schema_patterns', $reserved_schema_patterns)->save();
}

/**
 * Implementation of hook_schema().
 *
 * @ingroup tripal_chado
 */
function tripal_chado_schema() {
  $schema = [];

  $schema['chado_installations'] = tripal_chado_chado_installations_schema();
  $schema['tripal_custom_tables'] = tripal_chado_tripal_custom_tables_schema();
  $schema['tripal_mviews'] = tripal_chado_tripal_mviews_schema();

  // Add tables to integrate the independant Tripal Vocabularies to Chado terms.
  $schema['chado_tripalvocab'] = tripal_chado_chado_vocab();
  $schema['chado_tripalvocabspace'] = tripal_chado_chado_vocabidspace();
  $schema['chado_tripalterm'] = tripal_chado_chado_term();

  // Add tables supporting the Tripal Storage.
  $schema['tripal_cv_obo'] = tripal_chado_tripal_cv_obo_schema();

  return $schema;
}

/**
 * Table definition for the tripal_cv_obo table used by the OBO loader.
 */
function tripal_chado_tripal_cv_obo_schema() {
  return [
    'fields' => [
      'obo_id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ],
      'name' => [
        'type' => 'varchar',
        'length' => 255
      ],
      'path'  => [
        'type' => 'varchar',
        'length' => 1024
      ],
    ],
    'indexes' => [
      'tripal_cv_obo_idx1' => ['obo_id'],
    ],
    'primary key' => ['obo_id'],
  ];
}

/**
 * Returns the Drupal Schema API array for the chado_installations table.
 */
function tripal_chado_chado_installations_schema() {
  return [
    'fields' => [
      'install_id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'schema_name' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'version' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'created' => [
        'type' => 'varchar',
        'length' => 255,
      ],
      'updated' => [
        'type' => 'varchar',
        'length' => 255,
      ],
    ],
    'indexes' => [
      'schema_name' => ['schema_name'],
    ],
    'primary key' => ['install_id'],
  ];
}

/**
 * Links Tripal Vocabularies to Chado cvs.
 */
function tripal_chado_chado_vocab() {
  return [
    'fields' => [
      'mapping_id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'schema_name' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'cv_id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'tripalvocab_id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'indexes' => [
      'chado' => ['schema_name', 'cv_id'],
      'tripal' => ['tripalvocab_id'],
    ],
    'primary key' => ['mapping_id'],
  ];
}

/**
 * Links Tripal Vocabulary IDSpaces to Chado dbs.
 */
function tripal_chado_chado_vocabidspace() {
  return [
    'fields' => [
      'mapping_id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'schema_name' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'db_id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'tripalvocabspace_id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'indexes' => [
      'chado' => ['schema_name', 'db_id'],
      'tripal' => ['tripalvocabspace_id'],
    ],
    'primary key' => ['mapping_id'],
  ];
}

/**
 * Links Tripal Vocabularies to Chado cvs.
 */
function tripal_chado_chado_term() {
  return [
    'fields' => [
      'mapping_id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'schema_name' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'cvterm_id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'tripalterm_id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'indexes' => [
      'chado' => ['schema_name', 'cvterm_id'],
      'tripal' => ['tripalterm_id'],
    ],
    'primary key' => ['mapping_id'],
  ];
}

/**
 * Describes the tripal_mviews table.
 */
function tripal_chado_tripal_mviews_schema() {
  return array(
    'fields' => array(
      'mview_id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'table_id' => array(
        'type' => 'int',
        'not null' => True,
        'description' => 'The custom table ID'
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE
      ),
      'query' => array(
        'type' => 'text',
        'size' => 'normal',
        'not null' => TRUE
      ),
      'last_update' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'UNIX integer time'
      ),
      'status' => array(
        'type' => 'text',
        'size' => 'normal',
        'not null' => FALSE
      ),
      'comment' => array(
        'type' => 'text',
        'size' => 'normal',
        'not null' => FALSE
      ),
    ),
    'indexes' => array(
      'mview_id' => array('mview_id')
    ),
    'unique keys' => array(
      'table_id' => array('table_id'),
    ),
    'primary key' => array('mview_id'),
  );
}

/**
 * Describes the Tripal Custom Tables (tripal_custom_tables) table.
 * This keeps track of tables created by Tripal and stored in chado.
 */
function tripal_chado_tripal_custom_tables_schema() {
  return [
    'fields' => array(
      'table_id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'table_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE
      ),
      'schema' => array(
        'type' => 'text',
        'not null' => TRUE
      ),
      'hidden' => array(
        'type' => 'int',
        'size' => 'tiny',
        'default' => 0,
        'description' => 'Set to true if this custom table is not for end-users to manage, but for the Tripal module.'
      ),
      'chado' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'description' => 'The name of the Chado schema where this table exists.'
      ),
    ),
    'indexes' => array(
      'table_id' => array('table_id'),
    ),
    'primary key' => array('table_id'),
    'foreign keys' => array(
      'tripal_mviews' => array(
        'table' => 'tripal_mviews',
        'columns' => array(
          'mview_id' => 'mview_id'
        ),
      ),
    ),
  ];
}