tripal_chado.install 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. else {
  293. $sql = "CREATE UNIQUE INDEX tripal_cv_obo_pkey2 ON tripal_cv_obo2 USING btree (obo_id)";
  294. }
  295. db_query($sql);
  296. }
  297. /* if (db_table_exists('tripal_cv_defaults')) {
  298. // Move the tripal_cv_defaults table out of the way.
  299. $sql = "ALTER TABLE tripal_cv_defaults RENAME TO tripal_cv_defaults2";
  300. db_query($sql);
  301. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_defaults_pkey'")->fetchField()) {
  302. $sql = "ALTER INDEX tripal_cv_defaults_pkey RENAME TO tripal_cv_defaults_pkey2";
  303. }
  304. else {
  305. $sql = "CREATE UNIQUE INDEX tripal_cv_defaults_pkey2 ON tripal_cv_defaults2 USING btree (cv_default_id)";
  306. }
  307. db_query($sql);
  308. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_defaults_tripal_cv_defaults_idx1_idx'")->fetchField()) {
  309. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_idx1_idx RENAME TO tripal_cv_defaults_tripal_cv_defaults_idx1_idx2";
  310. }
  311. else {
  312. $sql = "CREATE INDEX tripal_cv_defaults_tripal_cv_defaults_idx1_idx2 ON tripal_cv_defaults2 USING btree (table_name, field_name)";
  313. }
  314. db_query($sql);
  315. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_defaults_tripal_cv_defaults_unq1_key'")->fetchField()) {
  316. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_unq1_key RENAME TO tripal_cv_defaults_tripal_cv_defaults_unq1_key2";
  317. }
  318. else {
  319. $sql = "CREATE UNIQUE INDEX tripal_cv_defaults_tripal_cv_defaults_unq1_key2 ON tripal_cv_defaults2 USING btree (table_name, field_name, cv_id)";
  320. }
  321. db_query($sql);
  322. } */
  323. if (db_table_exists('tripal_pub_import')) {
  324. // Move the tripal_pub_import table out of the way.
  325. $sql = "ALTER TABLE tripal_pub_import RENAME TO tripal_pub_import2";
  326. db_query($sql);
  327. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_pub_import_name_idx'")->fetchField()) {
  328. $sql = "ALTER INDEX tripal_pub_import_name_idx RENAME TO tripal_pub_import_name_idx2";
  329. }
  330. else {
  331. $sql = "CREATE INDEX tripal_pub_import_name_idx2 ON tripal_pub_import2 USING btree (name)";
  332. }
  333. db_query($sql);
  334. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_pub_import_pkey'")->fetchField()) {
  335. $sql = "ALTER INDEX tripal_pub_import_pkey RENAME TO tripal_pub_import_pkey2";
  336. }
  337. else {
  338. $sql = "CREATE UNIQUE INDEX tripal_pub_import_pkey2 ON tripal_pub_import2 USING btree (pub_import_id)";
  339. }
  340. db_query($sql);
  341. }
  342. }
  343. /**
  344. * This function should be executed only one time during upgrade of v2 to v3.
  345. */
  346. function tripal_chado_upgrade_v2_v3_enable() {
  347. // If Tripal v2 is already installed, the installation of this module
  348. // will try and recreate some of the tables created with tripal_core and the
  349. // installation will fail. Therefore, in the install we renamed it. Now
  350. // we want to move it back.
  351. if (db_table_exists('tripal_mviews2')) {
  352. // tripal_mviews
  353. $sql = "DROP TABLE tripal_mviews";
  354. db_query($sql);
  355. $sql = "ALTER TABLE tripal_mviews2 RENAME to tripal_mviews";
  356. db_query($sql);
  357. $sql = "ALTER INDEX tripal_mviews_mv_name_key2 RENAME TO tripal_mviews_mv_name_key";
  358. db_query($sql);
  359. $sql = "ALTER INDEX tripal_mviews_mv_table_key2 RENAME TO tripal_mviews_mv_table_key";
  360. db_query($sql);
  361. $sql = "ALTER INDEX tripal_mviews_mview_id_idx2 RENAME TO tripal_mviews_mview_id_idx";
  362. db_query($sql);
  363. $sql = "ALTER INDEX tripal_mviews_pkey2 RENAME TO tripal_mviews_pkey";
  364. db_query($sql);
  365. }
  366. // tripal_custom_tables
  367. if (db_table_exists('tripal_custom_tables2')) {
  368. $sql = "DROP TABLE tripal_custom_tables";
  369. db_query($sql);
  370. $sql = "ALTER TABLE tripal_custom_tables2 RENAME to tripal_custom_tables";
  371. db_query($sql);
  372. $sql = "ALTER INDEX tripal_custom_tables_pkey2 RENAME TO tripal_custom_tables_pkey";
  373. db_query($sql);
  374. $sql = "ALTER INDEX tripal_custom_tables_table_id_idx2 RENAME TO tripal_custom_tables_table_id_idx";
  375. db_query($sql);
  376. }
  377. // tripal_cv_obo
  378. if (db_table_exists('tripal_cv_obo2')) {
  379. $sql = "DROP TABLE tripal_cv_obo";
  380. db_query($sql);
  381. $sql = "ALTER TABLE tripal_cv_obo2 RENAME to tripal_cv_obo";
  382. db_query($sql);
  383. $sql = "ALTER INDEX tripal_cv_obo_obo_id_idx2 RENAME TO tripal_cv_obo_obo_id_idx";
  384. db_query($sql);
  385. $sql = "ALTER INDEX tripal_cv_obo_pkey2 RENAME TO tripal_cv_obo_pkey";
  386. db_query($sql);
  387. }
  388. // tripal_cv_defaults
  389. /* if (db_table_exists('tripal_cv_defaults2')) {
  390. $sql = "DROP TABLE tripal_cv_defaults";
  391. db_query($sql);
  392. $sql = "ALTER TABLE tripal_cv_defaults2 RENAME to tripal_cv_defaults";
  393. db_query($sql);
  394. $sql = "ALTER INDEX tripal_cv_defaults_pkey2 RENAME TO tripal_cv_defaults_pkey";
  395. db_query($sql);
  396. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_idx1_idx2 RENAME TO tripal_cv_defaults_tripal_cv_defaults_idx1_idx";
  397. db_query($sql);
  398. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_unq1_key2 RENAME TO tripal_cv_defaults_tripal_cv_defaults_unq1_key";
  399. db_query($sql);
  400. } */
  401. // tripal_pub_import
  402. if (db_table_exists('tripal_pub_import2')) {
  403. $sql = "DROP TABLE tripal_pub_import";
  404. db_query($sql);
  405. $sql = "ALTER TABLE tripal_pub_import2 RENAME to tripal_pub_import";
  406. db_query($sql);
  407. $sql = "ALTER INDEX tripal_pub_import_name_idx2 RENAME TO tripal_pub_import_name_idx";
  408. db_query($sql);
  409. $sql = "ALTER INDEX tripal_pub_import_pkey2 RENAME TO tripal_pub_import_pkey";
  410. db_query($sql);
  411. }
  412. }
  413. /**
  414. * @section
  415. * Schema Definitions.
  416. */
  417. /**
  418. * Implementation of hook_schema().
  419. *
  420. * @ingroup tripal_pub
  421. */
  422. function tripal_chado_tripal_pub_import_schema() {
  423. return array(
  424. 'fields' => array(
  425. 'pub_import_id' => array(
  426. 'type' => 'serial',
  427. 'not null' => TRUE
  428. ),
  429. 'name' => array(
  430. 'type' => 'varchar',
  431. 'length' => 255,
  432. 'not null' => TRUE
  433. ),
  434. 'criteria' => array(
  435. 'type' => 'text',
  436. 'size' => 'normal',
  437. 'not null' => TRUE,
  438. 'description' => 'Contains a serialized PHP array containing the search criteria'
  439. ),
  440. 'disabled' => array(
  441. 'type' => 'int',
  442. 'unsigned' => TRUE,
  443. 'not NULL' => TRUE,
  444. 'default' => 0
  445. ),
  446. 'do_contact' => array(
  447. 'type' => 'int',
  448. 'unsigned' => TRUE,
  449. 'not NULL' => TRUE,
  450. 'default' => 0
  451. ),
  452. ),
  453. 'primary key' => array('pub_import_id'),
  454. 'indexes' => array(
  455. 'name' => array('name')
  456. ),
  457. );
  458. }
  459. /**
  460. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  461. * This keeps track of tables created by Tripal and stored in chado that may or may not
  462. * also be materialized views.
  463. *
  464. * @ingroup tripal
  465. */
  466. function tripal_chado_tripal_custom_tables_schema() {
  467. return array(
  468. 'fields' => array(
  469. 'table_id' => array(
  470. 'type' => 'serial',
  471. 'unsigned' => TRUE,
  472. 'not NULL' => TRUE
  473. ),
  474. 'table_name' => array(
  475. 'type' => 'varchar',
  476. 'length' => 255,
  477. 'not NULL' => TRUE
  478. ),
  479. 'schema' => array(
  480. 'type' => 'text',
  481. 'not NULL' => TRUE
  482. ),
  483. 'mview_id' => array(
  484. 'type' => 'int',
  485. 'not NULL' => FALSE
  486. )
  487. ),
  488. 'indexes' => array(
  489. 'table_id' => array('table_id'),
  490. ),
  491. 'primary key' => array('table_id'),
  492. 'foreign keys' => array(
  493. 'tripal_mviews' => array(
  494. 'table' => 'tripal_mviews',
  495. 'columns' => array(
  496. 'mview_id' => 'mview_id'
  497. ),
  498. ),
  499. ),
  500. );
  501. }
  502. /**
  503. * Describes the Tripal Materialized View (tripal_mviews) table
  504. * This table keeps track of all materialized views created by Tripal and stored in chado
  505. *
  506. * @ingroup tripal
  507. */
  508. function tripal_chado_tripal_mviews_schema() {
  509. return array(
  510. 'fields' => array(
  511. 'mview_id' => array(
  512. 'type' => 'serial',
  513. 'unsigned' => TRUE,
  514. 'not NULL' => TRUE
  515. ),
  516. 'name' => array(
  517. 'type' => 'varchar',
  518. 'length' => 255,
  519. 'not NULL' => TRUE
  520. ),
  521. 'modulename' => array(
  522. 'type' => 'varchar',
  523. 'length' => 50,
  524. 'not NULL' => TRUE,
  525. 'description' => 'The module name that provides the callback for this job'
  526. ),
  527. 'mv_table' => array(
  528. 'type' => 'varchar',
  529. 'length' => 128,
  530. 'not NULL' => FALSE
  531. ),
  532. 'mv_specs' => array(
  533. 'type' => 'text',
  534. 'size' => 'normal',
  535. 'not NULL' => FALSE
  536. ),
  537. 'mv_schema' => array(
  538. 'type' => 'text',
  539. 'size' => 'normal',
  540. 'not NULL' => FALSE
  541. ),
  542. 'indexed' => array(
  543. 'type' => 'text',
  544. 'size' => 'normal',
  545. 'not NULL' => FALSE
  546. ),
  547. 'query' => array(
  548. 'type' => 'text',
  549. 'size' => 'normal',
  550. 'not NULL' => TRUE
  551. ),
  552. 'special_index' => array(
  553. 'type' => 'text',
  554. 'size' => 'normal',
  555. 'not NULL' => FALSE
  556. ),
  557. 'last_update' => array(
  558. 'type' => 'int',
  559. 'not NULL' => FALSE,
  560. 'description' => 'UNIX integer time'
  561. ),
  562. 'status' => array(
  563. 'type' => 'text',
  564. 'size' => 'normal',
  565. 'not NULL' => FALSE
  566. ),
  567. 'comment' => array(
  568. 'type' => 'text',
  569. 'size' => 'normal',
  570. 'not NULL' => FALSE
  571. ),
  572. ),
  573. 'indexes' => array(
  574. 'mview_id' => array('mview_id')
  575. ),
  576. 'unique keys' => array(
  577. 'mv_table' => array('mv_table'),
  578. 'mv_name' => array('name'),
  579. ),
  580. 'primary key' => array('mview_id'),
  581. );
  582. }
  583. /**
  584. * Links Biological Data Entities to the chado "base" table the data is stored in.
  585. * This is where we would specify that a particular gene maps to the record in the
  586. * chado.feature table with a feature_id=2432;
  587. */
  588. function tripal_chado_chado_bundle_schema() {
  589. $schema = array(
  590. 'description' => 'Describes how a bundle maps data to Chado',
  591. 'fields' => array(
  592. 'chado_bundle_id' => array(
  593. 'description' => 'The primary identifier for this table.',
  594. 'type' => 'serial',
  595. 'unsigned' => TRUE,
  596. 'not null' => TRUE,
  597. ),
  598. 'bundle_id' => array(
  599. 'description' => 'The unique entity id.',
  600. 'type' => 'int',
  601. 'not null' => TRUE,
  602. ),
  603. 'data_table' => array(
  604. 'description' => 'The table in Chado that this term services (e.g. feature, stock, library, etc.)',
  605. 'type' => 'varchar',
  606. 'length' => 128,
  607. 'not null' => TRUE,
  608. 'default' => '',
  609. ),
  610. 'type_linker_table' => array(
  611. 'description' => 'If a linker table (e.g. cvterm/prop) is needed to uniquely identify a content type then that table name is provided here.',
  612. 'type' => 'varchar',
  613. 'length' => 128,
  614. 'not null' => FALSE,
  615. 'default' => '',
  616. ),
  617. 'type_column' => array(
  618. '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.',
  619. 'type' => 'varchar',
  620. 'length' => 128,
  621. 'not null' => FALSE,
  622. 'default' => '',
  623. ),
  624. 'type_id' => array(
  625. 'description' => 'If a type_column is set then this is the cvterm_id of the data type that this bundle maps to.',
  626. 'type' => 'varchar',
  627. 'length' => 128,
  628. 'not null' => TRUE,
  629. 'default' => '',
  630. ),
  631. 'type_value' => array(
  632. '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.',
  633. 'type' => 'text',
  634. 'not null' => FALSE,
  635. 'default' => '',
  636. )
  637. ),
  638. 'indexes' => array(
  639. 'bundle_id' => array('bundle_id'),
  640. 'data_table' => array('data_table'),
  641. ),
  642. 'unique keys' => array(
  643. 'record' => array('bundle_id'),
  644. ),
  645. 'primary key' => array('chado_bundle_id'),
  646. );
  647. return $schema;
  648. }
  649. /**
  650. * Tripal cvterm mapping schema
  651. * Map cvterms to chado tables that use them
  652. */
  653. function tripal_chado_chado_cvterm_mapping_schema() {
  654. $schema = array (
  655. 'table' => 'chado_cvterm_mapping',
  656. 'fields' => array (
  657. 'mapping_id' => array(
  658. 'type' => 'serial',
  659. 'not null' => TRUE
  660. ),
  661. 'cvterm_id' => array (
  662. 'type' => 'int',
  663. 'not null' => TRUE
  664. ),
  665. 'chado_table' => array (
  666. 'type' => 'varchar',
  667. 'length' => 128,
  668. 'not null' => TRUE
  669. ),
  670. 'chado_field' => array (
  671. 'type' => 'varchar',
  672. 'length' => 128,
  673. 'not null' => FALSE
  674. ),
  675. ),
  676. 'primary key' => array (
  677. 0 => 'mapping_id'
  678. ),
  679. 'unique key' => array(
  680. 'cvterm_id',
  681. ),
  682. 'indexes' => array(
  683. 'tripal_cvterm2table_idx1' => array('cvterm_id'),
  684. 'tripal_cvterm2table_idx2' => array('chado_table'),
  685. 'tripal_cvterm2table_idx3' => array('chado_table', 'chado_field'),
  686. ),
  687. );
  688. return $schema;
  689. }
  690. /**
  691. * Fixes the phase on the tripal_gffcds_temp table used for importing GFF files, and fixes the db.name term mapping.
  692. *
  693. */
  694. function tripal_chado_update_7300() {
  695. try {
  696. if (chado_table_exists('tripal_gffcds_temp')) {
  697. chado_query("ALTER TABLE {tripal_gffcds_temp} ALTER COLUMN phase DROP NOT NULL;");
  698. }
  699. $term = tripal_insert_cvterm(array(
  700. 'id' => 'data:1048',
  701. 'name' => 'Database ID',
  702. 'cv_name' => 'EDAM',
  703. 'definition' => 'An identifier of a biological or bioinformatics database.',
  704. ));
  705. tripal_associate_chado_semweb_term('db', 'name', $term);
  706. }
  707. catch (\PDOException $e) {
  708. $error = $e->getMessage();
  709. throw new DrupalUpdateException('Could not fix phase on tripal_gffcds_temp table: '. $error);
  710. }
  711. }
  712. /**
  713. * Divides chado_entity table for better integration with views.
  714. */
  715. function tripal_chado_update_7301() {
  716. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.bundle');
  717. try {
  718. $transaction = db_transaction();
  719. $query = db_select('chado_bundle', 'CB');
  720. $query->join('tripal_bundle', 'TB', 'TB.id = CB.bundle_id');
  721. $query->fields('CB', array('data_table'));
  722. $query->fields('TB', array('name'));
  723. $cbundles = $query->execute();
  724. // If the table for the bundle doesn't exist then create one, and then
  725. // move all of the records from the chado_entity table to it.
  726. while($cbundle = $cbundles->fetchObject()) {
  727. $cbundle_table = tripal_chado_get_bundle_entity_table($cbundle);
  728. if (!db_table_exists($cbundle_table)) {
  729. // Create the bundle table.
  730. tripal_chado_create_bundle_table($cbundle);
  731. // Now move the records over.
  732. $sql = "
  733. INSERT INTO {$cbundle_table} (entity_id, record_id, nid)
  734. SELECT CE.entity_id, CE.record_id, CE.nid
  735. FROM {chado_entity} CE
  736. INNER JOIN {tripal_entity} TE ON CE.entity_id = TE.id
  737. WHERE TE.bundle = :bundle
  738. ";
  739. db_query($sql, array(':bundle' => $cbundle->name));
  740. }
  741. }
  742. // Now remove the chado_entity table.
  743. db_drop_table('chado_entity');
  744. }
  745. catch (\PDOException $e) {
  746. $transaction->rollback();
  747. $error = $e->getMessage();
  748. throw new DrupalUpdateException('Could not perform update: '. $error);
  749. }
  750. }