tripal_chado.install 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. <?php
  2. function tripal_chado_install() {
  3. // // The foreign key specification doesn't really add one to the
  4. // // Drupal schema, it is just used internally, but we want one.
  5. // db_query('
  6. // ALTER TABLE {tripal_custom_tables}
  7. // ADD CONSTRAINT tripal_custom_tables_fk1
  8. // FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  9. // ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  10. // ');
  11. if (chado_is_installed()) {
  12. // For an upgraded site we need to move some vocabulary terms over
  13. // to the new 'local' vocabulary:
  14. tripal_insert_db(array(
  15. 'name' => 'local',
  16. 'description' => variable_get('site_name', 'This site.'),
  17. ));
  18. // Move the library properties out of the tripal database and into the
  19. // local database.
  20. $sql = "
  21. UPDATE {dbxref}
  22. SET db_id = (SELECT db_id FROM {db} WHERE name = 'local')
  23. WHERE dbxref_id IN (
  24. SELECT DISTINCT CVT.dbxref_id
  25. FROM {cvterm} CVT
  26. INNER JOIN {cv} CV ON CV.cv_id = CVT.cv_id
  27. WHERE CV.name IN (
  28. 'library_property',
  29. 'library_type',
  30. 'project_property',
  31. 'nd_experiment_types',
  32. 'nd_geolocation_property',
  33. 'tripal_analysis'
  34. )
  35. )
  36. ";
  37. chado_query($sql);
  38. }
  39. }
  40. /**
  41. * Implementation of hook_uninstall().
  42. *
  43. * @ingroup tripal
  44. */
  45. function tripal_chado_uninstall() {
  46. // // Drop the foreign key between tripal_custom_tables and tripal_mviews
  47. // // so that Drupal can then drop the tables
  48. // db_query('
  49. // ALTER TABLE {tripal_custom_tables}
  50. // DROP CONSTRAINT tripal_custom_tables_fk1 CASCADE
  51. // ');
  52. variable_set('tripal_chado_is_prepared', FALSE);
  53. }
  54. function tripal_chado_chado_semweb_schema(){
  55. return array(
  56. 'fields' => array(
  57. 'chado_semweb_id' => array(
  58. 'type' => 'serial',
  59. 'not null' => TRUE
  60. ),
  61. 'chado_table' => array(
  62. 'type' => 'varchar',
  63. 'length ' => 128,
  64. 'not null' => TRUE
  65. ),
  66. 'chado_column' => array(
  67. 'type' => 'text',
  68. 'length ' => 128,
  69. 'not null' => TRUE
  70. ),
  71. 'cvterm_id' => array(
  72. 'type' => 'int',
  73. ),
  74. ),
  75. 'primary key' => array(
  76. 0 => 'chado_semweb_id',
  77. ),
  78. 'indexes' => array(
  79. 'chado_semweb_id_idx1' => array('cvterm_id'),
  80. 'chado_semweb_id_idx2' => array('chado_column'),
  81. 'chado_semweb_id_idx3' => array('chado_table'),
  82. ),
  83. 'unique keys' => array(
  84. 'chado_semweb_uq1' => array('chado_table', 'chado_column'),
  85. ),
  86. );
  87. }
  88. /**
  89. * Table definition for the tripal_cv_obo table
  90. * @param $schema
  91. */
  92. function tripal_chado_tripal_cv_obo_schema() {
  93. return array(
  94. 'fields' => array(
  95. 'obo_id' => array(
  96. 'type' => 'serial',
  97. 'unsigned' => TRUE,
  98. 'not null' => TRUE
  99. ),
  100. 'name' => array(
  101. 'type' => 'varchar',
  102. 'length' => 255
  103. ),
  104. 'path' => array(
  105. 'type' => 'varchar',
  106. 'length' => 1024
  107. ),
  108. ),
  109. 'indexes' => array(
  110. 'tripal_cv_obo_idx1' => array('obo_id'),
  111. ),
  112. 'primary key' => array('obo_id'),
  113. );
  114. }
  115. /**
  116. *
  117. */
  118. function tripal_chado_enable() {
  119. // If Tripal v2 is already installed, then when the module is first enabled
  120. // after an upgade, the installation of this module will try and recreate
  121. // some of the tables created with tripal_core and the installation will fail.
  122. // Therefore, the tables were temporarily moved out of the way to preserve
  123. // the data. Now we'll move them back.
  124. tripal_chado_upgrade_v2_v3_enable();
  125. }
  126. /**
  127. * Implements hook_schema().
  128. */
  129. function tripal_chado_schema() {
  130. // If Tripal v2 is already installed, then when the module is first enabled
  131. // after an upgade, the installation of this module will try and recreate
  132. // some of the tables created with tripal_core and the installation will fail.
  133. // Therefore, we need to temporarily move those tables out of the way, let
  134. // the module install and then move them back.
  135. $migrated = variable_get('tripal_v2_upgrade_v3_check_chado', FALSE);
  136. if (!$migrated) {
  137. try {
  138. tripal_chado_upgrade_v2_v3_pre_enable();
  139. variable_set('tripal_v2_upgrade_v3_check_chado', TRUE);
  140. }
  141. catch(Exception $e) {
  142. watchdog_exception('tripal_chado', $e);
  143. }
  144. }
  145. // Links TripalEntity entities to the chado record.
  146. $schema['chado_bundle'] = tripal_chado_chado_bundle_schema();
  147. $schema['chado_semweb'] = tripal_chado_chado_semweb_schema();
  148. $schema['tripal_mviews'] = tripal_chado_tripal_mviews_schema();
  149. $schema['tripal_custom_tables'] = tripal_chado_tripal_custom_tables_schema();
  150. $schema['tripal_cv_obo'] = tripal_chado_tripal_cv_obo_schema();
  151. $schema['tripal_pub_import'] = tripal_chado_tripal_pub_import_schema();
  152. // if this module is already installed and enabled, then we want to provide
  153. // the schemas for all of the custom tables. This will allow Views to
  154. // see the schemas. We check if the module is installed because during
  155. // installation we don't want to make these custom tables available as we don't
  156. // want them created in the Drupal database. The custom tables go in the
  157. // Chado database.
  158. if (db_table_exists('tripal_custom_tables')) {
  159. $sql = 'SELECT * FROM {tripal_custom_tables}';
  160. $results = db_query($sql);
  161. foreach ($results as $custom) {
  162. $schema[$custom->table_name] = unserialize($custom->schema);
  163. }
  164. }
  165. // Map cvterm usage to chado tables
  166. $schema['chado_cvterm_mapping'] = tripal_chado_chado_cvterm_mapping_schema();
  167. // When a chado Tripal content type is created, a linking table is also created to
  168. // link the entity to it's record in chado (@see tripal_chado_bundle_create() ).
  169. // This table is created via db_create_table() but in order to expose it to
  170. // the Drupal Schema API, we also need to define each one here.
  171. if (db_table_exists('chado_bundle')) {
  172. $resource = db_query('SELECT tb.name FROM chado_bundle cb LEFT JOIN tripal_bundle tb ON tb.id=cb.bundle_id');
  173. foreach ($resource as $r) {
  174. $bundle_name = $r->name;
  175. // This makes an assumption about the name of the linking table.
  176. // @todo: Switch to tripal_chado_get_bundle_entity_table($bundle).
  177. $chado_entity_table = 'chado_' . $bundle_name;
  178. $schema[$chado_entity_table] = array(
  179. 'description' => 'The linker table that associates TripalEntities with Chado records for entities of type ' . $bundle_name . '.',
  180. 'fields' => array(
  181. 'mapping_id' => array(
  182. 'type' => 'serial',
  183. 'not null' => TRUE
  184. ),
  185. 'entity_id' => array(
  186. 'description' => 'The unique entity id.',
  187. 'type' => 'int',
  188. 'not null' => TRUE,
  189. ),
  190. 'record_id' => array(
  191. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  192. 'type' => 'int',
  193. 'not null' => TRUE,
  194. ),
  195. 'nid' => array(
  196. 'description' => 'Optional. For linking nid to the entity when migrating Tripal v2 content',
  197. 'type' => 'int',
  198. )
  199. ),
  200. 'primary key' => array(
  201. 'mapping_id',
  202. ),
  203. 'indexes' => array(
  204. 'record_id' => array('record_id'),
  205. 'entity_id' => array('entity_id'),
  206. 'nid' => array('nid'),
  207. ),
  208. 'unique keys' => array(
  209. 'table_record' => array('record_id'),
  210. 'entity_id' => array('entity_id'),
  211. ),
  212. );
  213. }
  214. }
  215. return $schema;
  216. }
  217. /**
  218. * This function should be executed only one time during upgrade of v2 to v3.
  219. */
  220. function tripal_chado_upgrade_v2_v3_pre_enable() {
  221. // If Tripal v2 is already installed, then when the module is first enabled
  222. // after an upgade, the installation of this module will try and recreate
  223. // some of the tables created with tripal_core and the installation will fail.
  224. // Therefore, we need to temporarily move those tables out of the way, let
  225. // the module install and then move them back.
  226. if (db_table_exists('tripal_mviews')) {
  227. // Move the tripal_mviews table out of the way.
  228. $sql = "ALTER TABLE tripal_mviews RENAME TO tripal_mviews2";
  229. db_query($sql);
  230. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_mviews_mv_name_key'")->fetchField()) {
  231. $sql = "ALTER INDEX tripal_mviews_mv_name_key RENAME TO tripal_mviews_mv_name_key2";
  232. }
  233. else {
  234. $sql = "CREATE UNIQUE INDEX tripal_mviews_mv_name_key2 ON tripal_mviews2 USING btree (name)";
  235. }
  236. db_query($sql);
  237. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_mviews_mv_table_key'")->fetchField()) {
  238. $sql = "ALTER INDEX tripal_mviews_mv_table_key RENAME TO tripal_mviews_mv_table_key2";
  239. }
  240. else {
  241. $sql = "CREATE UNIQUE INDEX tripal_mviews_mv_table_key2 ON tripal_mviews2 USING btree (mv_table)";
  242. }
  243. db_query($sql);
  244. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_mviews_mview_id_idx'")->fetchField()) {
  245. $sql = "ALTER INDEX tripal_mviews_mview_id_idx RENAME TO tripal_mviews_mview_id_idx2";
  246. }
  247. else {
  248. $sql = "CREATE INDEX tripal_mviews_mview_id_idx2 ON tripal_mviews2 USING btree (mview_id)";
  249. }
  250. db_query($sql);
  251. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_mviews_pkey'")->fetchField()) {
  252. $sql = "ALTER INDEX tripal_mviews_pkey RENAME TO tripal_mviews_pkey2";
  253. }
  254. else {
  255. $sql = "CREATE UNIQUE INDEX tripal_mviews_pkey2 ON tripal_mviews2 USING btree (mview_id)";
  256. }
  257. db_query($sql);
  258. }
  259. if (db_table_exists('tripal_custom_tables')) {
  260. // Move the tripal_custom_tables table out of the way.
  261. $sql = "ALTER TABLE tripal_custom_tables RENAME TO tripal_custom_tables2";
  262. db_query($sql);
  263. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_custom_tables_pkey'")->fetchField()) {
  264. $sql = "ALTER INDEX tripal_custom_tables_pkey RENAME TO tripal_custom_tables_pkey2";
  265. }
  266. else {
  267. $sql = "CREATE UNIQUE INDEX tripal_custom_tables_pkey2 ON tripal_custom_tables2 USING btree (table_id)";
  268. }
  269. db_query($sql);
  270. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_custom_tables_table_id_idx'")->fetchField()) {
  271. $sql = "ALTER INDEX tripal_custom_tables_table_id_idx RENAME TO tripal_custom_tables_table_id_idx2";
  272. }
  273. else {
  274. $sql = "CREATE INDEX tripal_custom_tables_table_id_idx2 ON tripal_custom_tables2 USING btree (table_id)";
  275. }
  276. db_query($sql);
  277. }
  278. if (db_table_exists('tripal_cv_obo')) {
  279. // Move the tripal_cv_obo table out of the way.
  280. $sql = "ALTER TABLE tripal_cv_obo RENAME TO tripal_cv_obo2";
  281. db_query($sql);
  282. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_obo_obo_id_idx'")->fetchField()) {
  283. $sql = "ALTER INDEX tripal_cv_obo_obo_id_idx RENAME TO tripal_cv_obo_obo_id_idx2";
  284. }
  285. else {
  286. $sql = "CREATE INDEX tripal_cv_obo_obo_id_idx2 ON tripal_cv_obo2 USING btree (obo_id)";
  287. }
  288. db_query($sql);
  289. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_obo_pkey'")->fetchField()) {
  290. $sql = "ALTER INDEX tripal_cv_obo_pkey RENAME TO tripal_cv_obo_pkey2";
  291. }
  292. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_obo_tripal_cv_obo_idx1_idx'")->fetchField()) {
  293. $sql = "ALTER INDEX tripal_cv_obo_tripal_cv_obo_idx1_idx RENAME TO tripal_cv_obo_tripal_cv_obo_idx1_idx2";
  294. }
  295. else {
  296. $sql = "CREATE UNIQUE INDEX tripal_cv_obo_pkey2 ON tripal_cv_obo2 USING btree (obo_id)";
  297. }
  298. db_query($sql);
  299. }
  300. if (db_table_exists('tripal_pub_import')) {
  301. // Move the tripal_pub_import table out of the way.
  302. $sql = "ALTER TABLE tripal_pub_import RENAME TO tripal_pub_import2";
  303. db_query($sql);
  304. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_pub_import_name_idx'")->fetchField()) {
  305. $sql = "ALTER INDEX tripal_pub_import_name_idx RENAME TO tripal_pub_import_name_idx2";
  306. }
  307. else {
  308. $sql = "CREATE INDEX tripal_pub_import_name_idx2 ON tripal_pub_import2 USING btree (name)";
  309. }
  310. db_query($sql);
  311. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_pub_import_pkey'")->fetchField()) {
  312. $sql = "ALTER INDEX tripal_pub_import_pkey RENAME TO tripal_pub_import_pkey2";
  313. }
  314. else {
  315. $sql = "CREATE UNIQUE INDEX tripal_pub_import_pkey2 ON tripal_pub_import2 USING btree (pub_import_id)";
  316. }
  317. db_query($sql);
  318. }
  319. }
  320. /**
  321. * This function should be executed only one time during upgrade of v2 to v3.
  322. */
  323. function tripal_chado_upgrade_v2_v3_enable() {
  324. // If Tripal v2 is already installed, the installation of this module
  325. // will try and recreate some of the tables created with tripal_core and the
  326. // installation will fail. Therefore, in the install we renamed it. Now
  327. // we want to move it back.
  328. if (db_table_exists('tripal_mviews2')) {
  329. // tripal_mviews
  330. $sql = "DROP TABLE tripal_mviews";
  331. db_query($sql);
  332. $sql = "ALTER TABLE tripal_mviews2 RENAME to tripal_mviews";
  333. db_query($sql);
  334. $sql = "ALTER INDEX tripal_mviews_mv_name_key2 RENAME TO tripal_mviews_mv_name_key";
  335. db_query($sql);
  336. $sql = "ALTER INDEX tripal_mviews_mv_table_key2 RENAME TO tripal_mviews_mv_table_key";
  337. db_query($sql);
  338. $sql = "ALTER INDEX tripal_mviews_mview_id_idx2 RENAME TO tripal_mviews_mview_id_idx";
  339. db_query($sql);
  340. $sql = "ALTER INDEX tripal_mviews_pkey2 RENAME TO tripal_mviews_pkey";
  341. db_query($sql);
  342. }
  343. // tripal_custom_tables
  344. if (db_table_exists('tripal_custom_tables2')) {
  345. $sql = "DROP TABLE tripal_custom_tables";
  346. db_query($sql);
  347. $sql = "ALTER TABLE tripal_custom_tables2 RENAME to tripal_custom_tables";
  348. db_query($sql);
  349. $sql = "ALTER INDEX tripal_custom_tables_pkey2 RENAME TO tripal_custom_tables_pkey";
  350. db_query($sql);
  351. $sql = "ALTER INDEX tripal_custom_tables_table_id_idx2 RENAME TO tripal_custom_tables_table_id_idx";
  352. db_query($sql);
  353. }
  354. // tripal_cv_obo
  355. if (db_table_exists('tripal_cv_obo2')) {
  356. $sql = "DROP TABLE tripal_cv_obo";
  357. db_query($sql);
  358. $sql = "ALTER TABLE tripal_cv_obo2 RENAME to tripal_cv_obo";
  359. db_query($sql);
  360. $sql = "ALTER INDEX tripal_cv_obo_obo_id_idx2 RENAME TO tripal_cv_obo_obo_id_idx";
  361. db_query($sql);
  362. $sql = "ALTER INDEX tripal_cv_obo_pkey2 RENAME TO tripal_cv_obo_pkey";
  363. db_query($sql);
  364. $sql = "ALTER INDEX tripal_cv_obo_tripal_cv_obo_idx1_idx2 RENAME TO tripal_cv_obo_tripal_cv_obo_idx1_idx";
  365. db_query($sql);
  366. }
  367. // tripal_pub_import
  368. if (db_table_exists('tripal_pub_import2')) {
  369. $sql = "DROP TABLE tripal_pub_import";
  370. db_query($sql);
  371. $sql = "ALTER TABLE tripal_pub_import2 RENAME to tripal_pub_import";
  372. db_query($sql);
  373. $sql = "ALTER INDEX tripal_pub_import_name_idx2 RENAME TO tripal_pub_import_name_idx";
  374. db_query($sql);
  375. $sql = "ALTER INDEX tripal_pub_import_pkey2 RENAME TO tripal_pub_import_pkey";
  376. db_query($sql);
  377. }
  378. }
  379. /**
  380. * @section
  381. * Schema Definitions.
  382. */
  383. /**
  384. * Implementation of hook_schema().
  385. *
  386. * @ingroup tripal_pub
  387. */
  388. function tripal_chado_tripal_pub_import_schema() {
  389. return array(
  390. 'fields' => array(
  391. 'pub_import_id' => array(
  392. 'type' => 'serial',
  393. 'not null' => TRUE
  394. ),
  395. 'name' => array(
  396. 'type' => 'varchar',
  397. 'length' => 255,
  398. 'not null' => TRUE
  399. ),
  400. 'criteria' => array(
  401. 'type' => 'text',
  402. 'size' => 'normal',
  403. 'not null' => TRUE,
  404. 'description' => 'Contains a serialized PHP array containing the search criteria'
  405. ),
  406. 'disabled' => array(
  407. 'type' => 'int',
  408. 'unsigned' => TRUE,
  409. 'not NULL' => TRUE,
  410. 'default' => 0
  411. ),
  412. 'do_contact' => array(
  413. 'type' => 'int',
  414. 'unsigned' => TRUE,
  415. 'not NULL' => TRUE,
  416. 'default' => 0
  417. ),
  418. ),
  419. 'primary key' => array('pub_import_id'),
  420. 'indexes' => array(
  421. 'name' => array('name')
  422. ),
  423. );
  424. }
  425. /**
  426. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  427. * This keeps track of tables created by Tripal and stored in chado that may or may not
  428. * also be materialized views.
  429. *
  430. * @ingroup tripal
  431. */
  432. function tripal_chado_tripal_custom_tables_schema() {
  433. return array(
  434. 'fields' => array(
  435. 'table_id' => array(
  436. 'type' => 'serial',
  437. 'unsigned' => TRUE,
  438. 'not NULL' => TRUE
  439. ),
  440. 'table_name' => array(
  441. 'type' => 'varchar',
  442. 'length' => 255,
  443. 'not NULL' => TRUE
  444. ),
  445. 'schema' => array(
  446. 'type' => 'text',
  447. 'not NULL' => TRUE
  448. ),
  449. 'mview_id' => array(
  450. 'type' => 'int',
  451. 'not NULL' => FALSE
  452. )
  453. ),
  454. 'indexes' => array(
  455. 'table_id' => array('table_id'),
  456. ),
  457. 'primary key' => array('table_id'),
  458. 'foreign keys' => array(
  459. 'tripal_mviews' => array(
  460. 'table' => 'tripal_mviews',
  461. 'columns' => array(
  462. 'mview_id' => 'mview_id'
  463. ),
  464. ),
  465. ),
  466. );
  467. }
  468. /**
  469. * Describes the Tripal Materialized View (tripal_mviews) table
  470. * This table keeps track of all materialized views created by Tripal and stored in chado
  471. *
  472. * @ingroup tripal
  473. */
  474. function tripal_chado_tripal_mviews_schema() {
  475. return array(
  476. 'fields' => array(
  477. 'mview_id' => array(
  478. 'type' => 'serial',
  479. 'unsigned' => TRUE,
  480. 'not NULL' => TRUE
  481. ),
  482. 'name' => array(
  483. 'type' => 'varchar',
  484. 'length' => 255,
  485. 'not NULL' => TRUE
  486. ),
  487. 'modulename' => array(
  488. 'type' => 'varchar',
  489. 'length' => 50,
  490. 'not NULL' => TRUE,
  491. 'description' => 'The module name that provides the callback for this job'
  492. ),
  493. 'mv_table' => array(
  494. 'type' => 'varchar',
  495. 'length' => 128,
  496. 'not NULL' => FALSE
  497. ),
  498. 'mv_specs' => array(
  499. 'type' => 'text',
  500. 'size' => 'normal',
  501. 'not NULL' => FALSE
  502. ),
  503. 'mv_schema' => array(
  504. 'type' => 'text',
  505. 'size' => 'normal',
  506. 'not NULL' => FALSE
  507. ),
  508. 'indexed' => array(
  509. 'type' => 'text',
  510. 'size' => 'normal',
  511. 'not NULL' => FALSE
  512. ),
  513. 'query' => array(
  514. 'type' => 'text',
  515. 'size' => 'normal',
  516. 'not NULL' => TRUE
  517. ),
  518. 'special_index' => array(
  519. 'type' => 'text',
  520. 'size' => 'normal',
  521. 'not NULL' => FALSE
  522. ),
  523. 'last_update' => array(
  524. 'type' => 'int',
  525. 'not NULL' => FALSE,
  526. 'description' => 'UNIX integer time'
  527. ),
  528. 'status' => array(
  529. 'type' => 'text',
  530. 'size' => 'normal',
  531. 'not NULL' => FALSE
  532. ),
  533. 'comment' => array(
  534. 'type' => 'text',
  535. 'size' => 'normal',
  536. 'not NULL' => FALSE
  537. ),
  538. ),
  539. 'indexes' => array(
  540. 'mview_id' => array('mview_id')
  541. ),
  542. 'unique keys' => array(
  543. 'mv_table' => array('mv_table'),
  544. 'mv_name' => array('name'),
  545. ),
  546. 'primary key' => array('mview_id'),
  547. );
  548. }
  549. /**
  550. * Links Biological Data Entities to the chado "base" table the data is stored in.
  551. * This is where we would specify that a particular gene maps to the record in the
  552. * chado.feature table with a feature_id=2432;
  553. */
  554. function tripal_chado_chado_bundle_schema() {
  555. $schema = array(
  556. 'description' => 'Describes how a bundle maps data to Chado',
  557. 'fields' => array(
  558. 'chado_bundle_id' => array(
  559. 'description' => 'The primary identifier for this table.',
  560. 'type' => 'serial',
  561. 'unsigned' => TRUE,
  562. 'not null' => TRUE,
  563. ),
  564. 'bundle_id' => array(
  565. 'description' => 'The unique entity id.',
  566. 'type' => 'int',
  567. 'not null' => TRUE,
  568. ),
  569. 'data_table' => array(
  570. 'description' => 'The table in Chado that this term services (e.g. feature, stock, library, etc.)',
  571. 'type' => 'varchar',
  572. 'length' => 128,
  573. 'not null' => TRUE,
  574. 'default' => '',
  575. ),
  576. 'type_linker_table' => array(
  577. 'description' => 'If a linker table (e.g. cvterm/prop) is needed to uniquely identify a content type then that table name is provided here.',
  578. 'type' => 'varchar',
  579. 'length' => 128,
  580. 'not null' => FALSE,
  581. 'default' => '',
  582. ),
  583. 'type_column' => array(
  584. 'description' => 'The column in the data table or linker table that distinguishes the data type. This must be in a foreign key relationship to the cvterm table.',
  585. 'type' => 'varchar',
  586. 'length' => 128,
  587. 'not null' => FALSE,
  588. 'default' => '',
  589. ),
  590. 'type_id' => array(
  591. 'description' => 'If a type_column is set then this is the cvterm_id of the data type that this bundle maps to.',
  592. 'type' => 'varchar',
  593. 'length' => 128,
  594. 'not null' => TRUE,
  595. 'default' => '',
  596. ),
  597. 'type_value' => array(
  598. 'description' => 'If a property table is used for a linker, then the value that should be matched to identify this content type is stored here.',
  599. 'type' => 'text',
  600. 'not null' => FALSE,
  601. 'default' => '',
  602. )
  603. ),
  604. 'indexes' => array(
  605. 'bundle_id' => array('bundle_id'),
  606. 'data_table' => array('data_table'),
  607. ),
  608. 'unique keys' => array(
  609. 'record' => array('bundle_id'),
  610. ),
  611. 'primary key' => array('chado_bundle_id'),
  612. );
  613. return $schema;
  614. }
  615. /**
  616. * Tripal cvterm mapping schema
  617. * Map cvterms to chado tables that use them
  618. */
  619. function tripal_chado_chado_cvterm_mapping_schema() {
  620. $schema = array (
  621. 'table' => 'chado_cvterm_mapping',
  622. 'fields' => array (
  623. 'mapping_id' => array(
  624. 'type' => 'serial',
  625. 'not null' => TRUE
  626. ),
  627. 'cvterm_id' => array (
  628. 'type' => 'int',
  629. 'not null' => TRUE
  630. ),
  631. 'chado_table' => array (
  632. 'type' => 'varchar',
  633. 'length' => 128,
  634. 'not null' => TRUE
  635. ),
  636. 'chado_field' => array (
  637. 'type' => 'varchar',
  638. 'length' => 128,
  639. 'not null' => FALSE
  640. ),
  641. ),
  642. 'primary key' => array (
  643. 0 => 'mapping_id'
  644. ),
  645. 'unique key' => array(
  646. 'cvterm_id',
  647. ),
  648. 'indexes' => array(
  649. 'tripal_cvterm2table_idx1' => array('cvterm_id'),
  650. 'tripal_cvterm2table_idx2' => array('chado_table'),
  651. 'tripal_cvterm2table_idx3' => array('chado_table', 'chado_field'),
  652. ),
  653. );
  654. return $schema;
  655. }
  656. /**
  657. * Fixes the phase on the tripal_gffcds_temp table used for importing GFF files, and fixes the db.name term mapping.
  658. *
  659. */
  660. function tripal_chado_update_7300() {
  661. try {
  662. if (chado_table_exists('tripal_gffcds_temp')) {
  663. chado_query("ALTER TABLE {tripal_gffcds_temp} ALTER COLUMN phase DROP NOT NULL;");
  664. }
  665. $term = tripal_insert_cvterm(array(
  666. 'id' => 'data:1048',
  667. 'name' => 'Database ID',
  668. 'cv_name' => 'EDAM',
  669. 'definition' => 'An identifier of a biological or bioinformatics database.',
  670. ));
  671. tripal_associate_chado_semweb_term('db', 'name', $term);
  672. }
  673. catch (\PDOException $e) {
  674. $error = $e->getMessage();
  675. throw new DrupalUpdateException('Could not fix phase on tripal_gffcds_temp table: '. $error);
  676. }
  677. }
  678. /**
  679. * Divides chado_entity table for better integration with views.
  680. */
  681. function tripal_chado_update_7301() {
  682. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.bundle');
  683. try {
  684. $transaction = db_transaction();
  685. $query = db_select('chado_bundle', 'CB');
  686. $query->join('tripal_bundle', 'TB', 'TB.id = CB.bundle_id');
  687. $query->fields('CB', array('data_table'));
  688. $query->fields('TB', array('name'));
  689. $cbundles = $query->execute();
  690. // If the table for the bundle doesn't exist then create one, and then
  691. // move all of the records from the chado_entity table to it.
  692. while($cbundle = $cbundles->fetchObject()) {
  693. $cbundle_table = tripal_chado_get_bundle_entity_table($cbundle);
  694. if (!db_table_exists($cbundle_table)) {
  695. // Create the bundle table.
  696. tripal_chado_create_bundle_table($cbundle);
  697. // Now move the records over.
  698. $sql = "
  699. INSERT INTO {$cbundle_table} (entity_id, record_id, nid)
  700. SELECT CE.entity_id, CE.record_id, CE.nid
  701. FROM {chado_entity} CE
  702. INNER JOIN {tripal_entity} TE ON CE.entity_id = TE.id
  703. WHERE TE.bundle = :bundle
  704. ";
  705. db_query($sql, array(':bundle' => $cbundle->name));
  706. }
  707. }
  708. // Now remove the chado_entity table.
  709. db_drop_table('chado_entity');
  710. }
  711. catch (\PDOException $e) {
  712. $transaction->rollback();
  713. $error = $e->getMessage();
  714. throw new DrupalUpdateException('Could not perform update: '. $error);
  715. }
  716. }