tripal_core.install 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions used to install/uninstall tripal_core.
  5. */
  6. /**
  7. * Implementation of hook_install().
  8. *
  9. * @ingroup tripal_core
  10. */
  11. function tripal_core_install() {
  12. // make the data directory for this module
  13. $data_dir = tripal_get_files_dir();
  14. if (!file_prepare_directory($data_dir, FILE_CREATE_DIRECTORY)) {
  15. $message = "Cannot create directory $data_dir. This module may not " .
  16. "behave correctly without this directory. Please create " .
  17. "the directory manually or fix the problem and reinstall.";
  18. drupal_set_message(check_plain($message), 'error');
  19. tripal_report_error('tripal_core', TRIPAL_ERROR, $message, array());
  20. }
  21. // the foreign key specification doesn't really add one to the
  22. // Drupal schema, it is just used internally, but we want one
  23. db_query('
  24. ALTER TABLE {tripal_custom_tables}
  25. ADD CONSTRAINT tripal_custom_tables_fk1
  26. FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  27. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  28. ');
  29. }
  30. /**
  31. * Implementation of hook_schema().
  32. *
  33. * @ingroup tripal_core
  34. */
  35. function tripal_core_schema() {
  36. // get the schemas defined by this install file
  37. $schema = tripal_core_get_schemas();
  38. // if this module is already installed and enabled, then we want to provide
  39. // the schemas for all of the custom tables. This will allow Views to
  40. // see the schemas. We check if the module is installed because during
  41. // installation we don't want to make these custom tables available as we don't
  42. // want them created in the Drupal database. The custom tables go in the
  43. // Chado database.
  44. if (db_table_exists('tripal_custom_tables')) {
  45. $sql = 'SELECT * FROM {tripal_custom_tables}';
  46. $results = db_query($sql);
  47. foreach ($results as $custom) {
  48. $schema[$custom->table_name] = unserialize($custom->schema);
  49. }
  50. }
  51. return $schema;
  52. }
  53. /**
  54. * Implementation of hook_uninstall().
  55. *
  56. * @ingroup tripal_core
  57. */
  58. function tripal_core_uninstall() {
  59. // drop the foreign key between tripal_custom_tables and tripal_mviews
  60. // so that Drupal can then drop the tables
  61. db_query('
  62. ALTER TABLE {tripal_custom_tables}
  63. DROP CONSTRAINT IF EXISTS tripal_custom_tables_fk1 CASCADE
  64. ');
  65. }
  66. /**
  67. * This function simply defines all tables needed for the module to work
  68. * correctly. By putting the table definitions in a separate function we
  69. * can easily provide the entire list for hook_install or individual
  70. * tables for an update.
  71. *
  72. * @ingroup tripal_core
  73. */
  74. function tripal_core_get_schemas() {
  75. $schema = array();
  76. // get all the various schema parts and join them together
  77. $temp = tripal_core_jobs_schema();
  78. foreach ($temp as $table => $arr) {
  79. $schema[$table] = $arr;
  80. }
  81. $temp = tripal_core_mviews_schema();
  82. foreach ($temp as $table => $arr) {
  83. $schema[$table] = $arr;
  84. }
  85. $temp = tripal_core_custom_tables_schema();
  86. foreach ($temp as $table => $arr) {
  87. $schema[$table] = $arr;
  88. }
  89. return $schema;
  90. }
  91. /**
  92. * Describes the Tripal Materialized View (tripal_mviews) table
  93. * This table keeps track of all materialized views created by Tripal and stored in chado
  94. *
  95. * @ingroup tripal_core
  96. */
  97. function tripal_core_mviews_schema() {
  98. $schema = array();
  99. $schema['tripal_mviews'] = array(
  100. 'fields' => array(
  101. 'mview_id' => array(
  102. 'type' => 'serial',
  103. 'unsigned' => TRUE,
  104. 'not NULL' => TRUE
  105. ),
  106. 'name' => array(
  107. 'type' => 'varchar',
  108. 'length' => 255,
  109. 'not NULL' => TRUE
  110. ),
  111. 'modulename' => array(
  112. 'type' => 'varchar',
  113. 'length' => 50,
  114. 'not NULL' => TRUE,
  115. 'description' => 'The module name that provides the callback for this job'
  116. ),
  117. 'mv_table' => array(
  118. 'type' => 'varchar',
  119. 'length' => 128,
  120. 'not NULL' => FALSE
  121. ),
  122. 'mv_specs' => array(
  123. 'type' => 'text',
  124. 'size' => 'normal',
  125. 'not NULL' => FALSE
  126. ),
  127. 'mv_schema' => array(
  128. 'type' => 'text',
  129. 'size' => 'normal',
  130. 'not NULL' => FALSE
  131. ),
  132. 'indexed' => array(
  133. 'type' => 'text',
  134. 'size' => 'normal',
  135. 'not NULL' => FALSE
  136. ),
  137. 'query' => array(
  138. 'type' => 'text',
  139. 'size' => 'normal',
  140. 'not NULL' => TRUE
  141. ),
  142. 'special_index' => array(
  143. 'type' => 'text',
  144. 'size' => 'normal',
  145. 'not NULL' => FALSE
  146. ),
  147. 'last_update' => array(
  148. 'type' => 'int',
  149. 'not NULL' => FALSE,
  150. 'description' => 'UNIX integer time'
  151. ),
  152. 'status' => array(
  153. 'type' => 'text',
  154. 'size' => 'normal',
  155. 'not NULL' => FALSE
  156. ),
  157. 'comment' => array(
  158. 'type' => 'text',
  159. 'size' => 'normal',
  160. 'not NULL' => FALSE
  161. ),
  162. ),
  163. 'indexes' => array(
  164. 'mview_id' => array('mview_id')
  165. ),
  166. 'unique keys' => array(
  167. 'mv_table' => array('mv_table'),
  168. 'mv_name' => array('name'),
  169. ),
  170. 'primary key' => array('mview_id'),
  171. );
  172. return $schema;
  173. }
  174. /**
  175. * Describes the Tripal Jobs (tripal_jobs) table
  176. * This table keeps track of all tripal jobs including their current status and is used
  177. * by tripal_launch_jobs to determine which jobs need to be run
  178. *
  179. * @ingroup tripal_core
  180. */
  181. function tripal_core_jobs_schema() {
  182. $schema = array();
  183. $schema['tripal_jobs'] = array(
  184. 'fields' => array(
  185. 'job_id' => array(
  186. 'type' => 'serial',
  187. 'unsigned' => TRUE,
  188. 'not NULL' => TRUE
  189. ),
  190. 'uid' => array(
  191. 'type' => 'int',
  192. 'unsigned' => TRUE,
  193. 'not NULL' => TRUE,
  194. 'description' => 'The Drupal userid of the submitee'
  195. ),
  196. 'job_name' => array(
  197. 'type' => 'varchar',
  198. 'length' => 255,
  199. 'not NULL' => TRUE
  200. ),
  201. 'modulename' => array(
  202. 'type' => 'varchar',
  203. 'length' => 50,
  204. 'not NULL' => TRUE,
  205. 'description' => 'The module name that provides the callback for this job'
  206. ),
  207. 'callback' => array(
  208. 'type' => 'varchar',
  209. 'length' => 255,
  210. 'not NULL' => TRUE
  211. ),
  212. 'arguments' => array(
  213. 'type' => 'text',
  214. 'size' => 'normal',
  215. 'not NULL' => FALSE
  216. ),
  217. 'progress' => array(
  218. 'type' => 'int',
  219. 'unsigned' => TRUE,
  220. 'default' => 0,
  221. 'not NULL' => FALSE,
  222. 'description' => 'a value from 0 to 100 indicating percent complete'
  223. ),
  224. 'status' => array(
  225. 'type' => 'varchar',
  226. 'length' => 50,
  227. 'not NULL' => TRUE
  228. ),
  229. 'submit_date' => array(
  230. 'type' => 'int',
  231. 'not NULL' => TRUE,
  232. 'description' => 'UNIX integer submit time'
  233. ),
  234. 'start_time' => array(
  235. 'type' => 'int',
  236. 'not NULL' => FALSE,
  237. 'description' => 'UNIX integer start time'
  238. ),
  239. 'end_time' => array(
  240. 'type' => 'int',
  241. 'not NULL' => FALSE,
  242. 'description' => 'UNIX integer end time'
  243. ),
  244. 'error_msg' => array(
  245. 'type' => 'text',
  246. 'size' => 'normal',
  247. 'not NULL' => FALSE
  248. ),
  249. 'pid' => array(
  250. 'type' => 'int',
  251. 'unsigned' => TRUE,
  252. 'not NULL' => FALSE,
  253. 'description' => 'The process id for the job'
  254. ),
  255. 'priority' => array(
  256. 'type' => 'int',
  257. 'unsigned' => TRUE,
  258. 'not NULL' => TRUE,
  259. 'default' => '0',
  260. 'description' => 'The job priority'
  261. ),
  262. 'mlock' => array(
  263. 'type' => 'int',
  264. 'unsigned' => TRUE,
  265. 'not NULL' => FALSE,
  266. 'description' => 'If set to 1 then all jobs for the module are held until this one finishes'
  267. ),
  268. 'lock' => array(
  269. 'type' => 'int',
  270. 'unsigned' => TRUE,
  271. 'not NULL' => FALSE,
  272. 'description' => 'If set to 1 then all jobs are held until this one finishes'
  273. ),
  274. ),
  275. 'indexes' => array(
  276. 'job_id' => array('job_id'),
  277. 'job_name' => array('job_name')
  278. ),
  279. 'primary key' => array('job_id'),
  280. );
  281. return $schema;
  282. }
  283. /**
  284. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  285. * This keeps track of tables created by Tripal and stored in chado that may or may not
  286. * also be materialized views.
  287. *
  288. * @ingroup tripal_core
  289. */
  290. function tripal_core_custom_tables_schema() {
  291. $schema = array();
  292. $schema['tripal_custom_tables'] = array(
  293. 'fields' => array(
  294. 'table_id' => array(
  295. 'type' => 'serial',
  296. 'unsigned' => TRUE,
  297. 'not NULL' => TRUE
  298. ),
  299. 'table_name' => array(
  300. 'type' => 'varchar',
  301. 'length' => 255,
  302. 'not NULL' => TRUE
  303. ),
  304. 'schema' => array(
  305. 'type' => 'text',
  306. 'not NULL' => TRUE
  307. ),
  308. 'mview_id' => array(
  309. 'type' => 'int',
  310. 'not NULL' => FALSE
  311. )
  312. ),
  313. 'indexes' => array(
  314. 'table_id' => array('table_id'),
  315. ),
  316. 'primary key' => array('table_id'),
  317. 'foreign keys' => array(
  318. 'tripal_mviews' => array(
  319. 'table' => 'tripal_mviews',
  320. 'columns' => array(
  321. 'mview_id' => 'mview_id'
  322. ),
  323. ),
  324. ),
  325. );
  326. return $schema;
  327. }
  328. /**
  329. * Adds an mview_id column to the tripal_custom_tables table and makes an assocation between the mview and the custom table
  330. *
  331. */
  332. function tripal_core_update_7200() {
  333. // add an mview column to the tripal_custom_tables table so we
  334. // can associate which of the custom tables are also mviews
  335. if (!db_field_exists('tripal_custom_tables', 'mview_id')) {
  336. $spec = array(
  337. 'type' => 'int',
  338. 'not NULL' => FALSE
  339. );
  340. $keys = array(
  341. 'foreign keys' => array(
  342. 'tripal_mviews' => array(
  343. 'table' => 'tripal_mviews',
  344. 'columns' => array(
  345. 'mview_id' => 'mview_id'
  346. ),
  347. ),
  348. ),
  349. );
  350. db_add_field('tripal_custom_tables', 'mview_id', $spec, $keys);
  351. // the foreign key specification doesn't really add one to the
  352. // Drupal schema, it is just used internally, but we want one
  353. db_query('
  354. ALTER TABLE {tripal_custom_tables}
  355. ADD CONSTRAINT tripal_custom_tables_fk1
  356. FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  357. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  358. ');
  359. }
  360. // now link the materialized view to it's custom table entry
  361. $mviews = db_select('tripal_mviews', 'tmv')
  362. ->fields('tmv', array('mview_id', 'mv_table'))
  363. ->execute();
  364. foreach ($mviews as $mview) {
  365. db_update('tripal_custom_tables')
  366. ->fields(array(
  367. 'mview_id' => $mview->mview_id
  368. ))
  369. ->condition('table_name', $mview->mv_table)
  370. ->execute();
  371. }
  372. }