tripal_chado_views.install 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /**
  3. * @file
  4. * Functions related to installing/uninstalling this module
  5. */
  6. /**
  7. * Implementation of hook_install().
  8. *
  9. * @ingroup tripal_chado_views
  10. */
  11. function tripal_chado_views_install() {
  12. // we want views to pick up our changes
  13. views_invalidate_cache();
  14. }
  15. /**
  16. * Implementation of hook_schema().
  17. *
  18. * @ingroup tripal_chado_views
  19. */
  20. function tripal_chado_views_schema() {
  21. // If Tripal v2 is already installed, then when the module is first enabled
  22. // after an upgade, the installation of this module will try and recreate
  23. // some of the tables created with tripal_core and the installation will fail.
  24. // Therefore, we need to temporarily move those tables out of the way, let
  25. // the module install and then move them back.
  26. $migrated = variable_get('tripal_v2_upgrade_v3_check_chado_views', FALSE);
  27. if (!$migrated) {
  28. try {
  29. tripal_chado_views_upgrade_v2_v3_pre_enable();
  30. variable_set('tripal_v2_upgrade_v3_check_chado_views', TRUE);
  31. }
  32. catch(Exception $e) {
  33. watchdog_exception('tripal_chado_views', $e);
  34. }
  35. }
  36. $schema = tripal_chado_views_get_schemas();
  37. return $schema;
  38. }
  39. /**
  40. * This function simply defines all tables needed for the module to work
  41. * correctly. By putting the table definitions in a separate function we
  42. * can easily provide the entire list for hook_install or individual
  43. * tables for an update.
  44. *
  45. * @ingroup tripal_chado_views
  46. */
  47. function tripal_chado_views_get_schemas() {
  48. $schema = array();
  49. $temp = tripal_chado_views_views_schema();
  50. foreach ($temp as $table => $arr) {
  51. $schema[$table] = $arr;
  52. }
  53. return $schema;
  54. }
  55. /**
  56. *
  57. */
  58. function tripal_chado_views_enable() {
  59. // If Tripal v2 is already installed, then when the module is first enabled
  60. // after an upgade, the installation of this module will try and recreate
  61. // some of the tables created with tripal_view and the installation will fail.
  62. // Therefore, the tables were temporarily moved out of the way to preserve
  63. // the data. Now we'll move them back.
  64. tripal_chado_views_upgrade_v2_v3_enable();
  65. }
  66. /**
  67. * Describe the Tripal Views Schema
  68. *
  69. * Tables include:
  70. * - tripal_views: main table for views integration setups
  71. * - tripal_views_field: keeps track of all fields related to a given views integration setup
  72. * - tripal_views_join: keeps track of joins between the current views integration setup
  73. * and other tables.
  74. * - tripal_views_handlers: keeps track of which handlers to use for a given field
  75. *
  76. * @ingroup tripal_chado_views
  77. */
  78. function tripal_chado_views_views_schema() {
  79. $schema = array();
  80. $schema['tripal_views'] = array(
  81. 'description' => 'contains the setups, their materialized view id and base table name that was used.',
  82. 'fields' => array(
  83. 'setup_id' => array(
  84. 'description' => 'the id of the setup',
  85. 'type' => 'serial',
  86. 'unsigned' => TRUE,
  87. 'not null' => TRUE,
  88. ),
  89. 'mview_id' => array(
  90. 'description' => 'the materialized view used for this setup',
  91. 'type' => 'int',
  92. 'unsigned' => TRUE,
  93. ),
  94. 'base_table' => array(
  95. 'description' => 'either TRUE (1) or FALSE (0) depending on whether the current table should be a bast table of a View',
  96. 'type' => 'int',
  97. 'not null ' => TRUE,
  98. 'default' => 1
  99. ),
  100. 'table_name' => array(
  101. 'description' => 'the table name being integrated.',
  102. 'type' => 'varchar',
  103. 'length' => 255,
  104. 'not null' => TRUE,
  105. 'default' => '',
  106. ),
  107. 'priority' => array(
  108. 'description' => 'when there are 2+ entries for the same table, the entry with the lightest (drupal-style) priority is used.',
  109. 'type' => 'int',
  110. ),
  111. 'name' => array(
  112. 'description' => 'Human readable name of this setup',
  113. 'type' => 'varchar',
  114. 'length' => 255,
  115. 'not null' => TRUE,
  116. 'default' => '',
  117. ),
  118. 'comment' => array(
  119. 'description' => 'add notes about this views setup',
  120. 'type' => 'text',
  121. 'size' => 'normal',
  122. 'not null' => FALSE,
  123. 'default' => '',
  124. ),
  125. ),
  126. 'unique_keys' => array(
  127. 'setup_id' => array('setup_id'),
  128. 'priority' => array('table_name', 'priority'),
  129. ),
  130. 'indexes' => array(
  131. 'priority' => array('table_name', 'priority'),
  132. ),
  133. 'primary key' => array('setup_id'),
  134. );
  135. $schema['tripal_views_field'] = array(
  136. 'description' => 'keep track of fields available for a given table',
  137. 'fields' => array(
  138. 'setup_id' => array(
  139. 'description' => 'the id of the setup',
  140. 'type' => 'int',
  141. 'unsigned' => TRUE,
  142. 'not null' => TRUE,
  143. ),
  144. 'column_name' => array(
  145. 'description' => 'the name of the field in the database',
  146. 'type' => 'varchar',
  147. 'length' => '255',
  148. 'not null' => TRUE,
  149. ),
  150. 'name' => array(
  151. 'description' => 'the human-readable name of the field',
  152. 'type' => 'varchar',
  153. 'length' => '255',
  154. 'not null' => TRUE,
  155. ),
  156. 'description' => array(
  157. 'description' => 'A short description of the field -seen under the field in the views UI',
  158. 'type' => 'varchar',
  159. 'length' => '255',
  160. 'not null' => TRUE,
  161. ),
  162. 'type' => array(
  163. 'description' => 'the database type of this field (ie: int, varchar)',
  164. 'type' => 'varchar',
  165. 'length' => '50',
  166. 'not null' => TRUE,
  167. ),
  168. ),
  169. 'primary key' => array('setup_id', 'column_name')
  170. );
  171. $schema['tripal_views_join'] = array(
  172. 'description' => 'coordinate the joining of tables',
  173. 'fields' => array(
  174. 'view_join_id' => array(
  175. 'description' => 'the id of the join',
  176. 'type' => 'serial',
  177. 'unsigned' => TRUE,
  178. 'not null' => TRUE,
  179. ),
  180. 'setup_id' => array(
  181. 'description' => 'setup id from tripal_views table',
  182. 'type' => 'int',
  183. 'unsigned' => TRUE,
  184. 'not null' => TRUE,
  185. ),
  186. 'base_table' => array(
  187. 'description' => 'the name of the base table',
  188. 'type' => 'varchar',
  189. 'length' => '255',
  190. 'not null' => TRUE,
  191. 'default' => '',
  192. ),
  193. 'base_field' => array(
  194. 'description' => 'the name of the base table column that will be joined',
  195. 'type' => 'varchar',
  196. 'length' => '255',
  197. 'not null' => TRUE,
  198. 'default' => '',
  199. ),
  200. 'left_table' => array(
  201. 'description' => 'the table on which to perform a left join',
  202. 'type' => 'varchar',
  203. 'length' => '255',
  204. 'not null' => TRUE,
  205. 'default' => '',
  206. ),
  207. 'left_field' => array(
  208. 'description' => 'the column on which to perform a left join',
  209. 'type' => 'varchar',
  210. 'length' => '255',
  211. 'not null' => TRUE,
  212. 'default' => '',
  213. ),
  214. 'handler' => array(
  215. 'description' => 'the name of the handler',
  216. 'type' => 'varchar',
  217. 'length' => '255',
  218. 'not null' => TRUE,
  219. 'default' => '',
  220. ),
  221. 'relationship_handler' => array(
  222. 'type' => 'varchar',
  223. 'length' => '255',
  224. 'not null' => TRUE,
  225. 'default' => 'views_handler_relationship'
  226. ),
  227. 'relationship_only' => array(
  228. 'type' => 'int',
  229. 'not null ' => TRUE,
  230. 'default' => 0
  231. ),
  232. 'arguments' => array(
  233. 'description' => 'arguments that may get passed to the handler',
  234. 'type' => 'text',
  235. 'size' => 'normal',
  236. ),
  237. ),
  238. 'unique_keys' => array(
  239. 'setup_id' => array('view_join_id'),
  240. ),
  241. 'primary key' => array('view_join_id'),
  242. );
  243. $schema['tripal_views_handlers'] = array(
  244. 'description' => 'in formation for views: column and views handler name',
  245. 'fields' => array(
  246. 'handler_id' => array(
  247. 'description' => 'the id of the handler',
  248. 'type' => 'serial',
  249. 'unsigned' => TRUE,
  250. 'not null' => TRUE,
  251. ),
  252. 'setup_id' => array(
  253. 'description' => 'setup id from the tripal_views table',
  254. 'type' => 'int',
  255. 'unsigned' => TRUE,
  256. 'not null' => TRUE,
  257. ),
  258. 'column_name' => array(
  259. 'description' => '',
  260. 'type' => 'varchar',
  261. 'length' => '255',
  262. 'not null' => TRUE,
  263. 'default' => '',
  264. ),
  265. 'handler_type' => array(
  266. 'description' => 'identifies the type of hander (e.g. field, filter, sort, argument, relationship, etc.)',
  267. 'type' => 'varchar',
  268. 'length' => '50',
  269. 'not null' => TRUE,
  270. 'default' => '',
  271. ),
  272. 'handler_name' => array(
  273. 'description' => 'the name of the handler',
  274. 'type' => 'varchar',
  275. 'length' => '255',
  276. 'not null' => TRUE,
  277. 'default' => '',
  278. ),
  279. 'arguments' => array(
  280. 'description' => 'arguments that may get passed to the handler',
  281. 'type' => 'text',
  282. 'size' => 'normal',
  283. 'not null' => FALSE,
  284. 'default' => '',
  285. ),
  286. ),
  287. 'unique_keys' => array(
  288. 'setup_id' => array('handler_id'),
  289. ),
  290. 'primary key' => array('handler_id'),
  291. );
  292. return $schema;
  293. }
  294. /**
  295. * This function should be executed only one time during upgrade of v2 to v3.
  296. */
  297. function tripal_chado_views_upgrade_v2_v3_pre_enable() {
  298. // If Tripal v2 is already installed, then when the module is first enabled
  299. // after an upgade, the installation of this module will try and recreate
  300. // some of the tables created with tripal_views and the installation will fail.
  301. // Therefore, we need to temporarily move those tables out of the way, let
  302. // the module install and then move them back.
  303. if (db_table_exists('tripal_views')) {
  304. // Move the tripal_mviews table out of the way.
  305. $sql = "ALTER TABLE tripal_views RENAME TO tripal_views2";
  306. db_query($sql);
  307. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_views_pkey'")->fetchField()) {
  308. $sql = "ALTER INDEX tripal_views_pkey RENAME TO tripal_views_pkey2";
  309. }
  310. else {
  311. $sql = "CREATE UNIQUE INDEX tripal_views_pkey2 ON tripal_views2 USING btree (setup_id)";
  312. }
  313. db_query($sql);
  314. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_views_priority_idx'")->fetchField()) {
  315. $sql = "ALTER INDEX tripal_views_priority_idx RENAME TO tripal_views_priority_idx2";
  316. }
  317. else {
  318. $sql = "CREATE UNIQUE INDEX tripal_views_priority_idx2 ON tripal_views2 USING btree (table_name, priority)";
  319. }
  320. db_query($sql);
  321. }
  322. if (db_table_exists('tripal_views_field')) {
  323. // Move the tripal_mviews table out of the way.
  324. $sql = "ALTER TABLE tripal_views_field RENAME TO tripal_views_field2";
  325. db_query($sql);
  326. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_views_field_pkey'")->fetchField()) {
  327. $sql = "ALTER INDEX tripal_views_field_pkey RENAME TO tripal_views_field_pkey2";
  328. }
  329. else {
  330. $sql = "CREATE UNIQUE INDEX tripal_views_field_pkey2 ON tripal_views_field2 USING btree (setup_id, column_name)";
  331. }
  332. db_query($sql);
  333. }
  334. if (db_table_exists('tripal_views_handlers')) {
  335. // Move the tripal_mviews table out of the way.
  336. $sql = "ALTER TABLE tripal_views_handlers RENAME TO tripal_views_handlers2";
  337. db_query($sql);
  338. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_views_handlers_pkey'")->fetchField()) {
  339. $sql = "ALTER INDEX tripal_views_handlers_pkey RENAME TO tripal_views_handlers_pkey2";
  340. }
  341. else {
  342. $sql = "CREATE UNIQUE INDEX tripal_views_handlers_pkey2 ON tripal_views_handlers2 USING btree (handler_id)";
  343. }
  344. db_query($sql);
  345. }
  346. if (db_table_exists('tripal_views_join')) {
  347. // Move the tripal_mviews table out of the way.
  348. $sql = "ALTER TABLE tripal_views_join RENAME TO tripal_views_join2";
  349. db_query($sql);
  350. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_views_join_pkey'")->fetchField()) {
  351. $sql = "ALTER INDEX tripal_views_join_pkey RENAME TO tripal_views_join_pkey2";
  352. }
  353. else {
  354. $sql = "CREATE UNIQUE INDEX tripal_views_join_pkey2 ON tripal_views_join2 USING btree (view_join_id)";
  355. }
  356. db_query($sql);
  357. }
  358. }
  359. /**
  360. * This function should be executed only one time during upgrade of v2 to v3.
  361. */
  362. function tripal_chado_views_upgrade_v2_v3_enable() {
  363. // If Tripal v2 is already installed, the installation of this module
  364. // will try and recreate some of the tables created with tripal_views and the
  365. // installation will fail. Therefore, in the install we renamed it. Now
  366. // we want to move it back.
  367. if (db_table_exists('tripal_views2')) {
  368. // tripal_mviews
  369. $sql = "DROP TABLE tripal_views";
  370. db_query($sql);
  371. $sql = "ALTER TABLE tripal_views2 RENAME to tripal_views";
  372. db_query($sql);
  373. $sql = "ALTER INDEX tripal_views_pkey2 RENAME TO tripal_views_pkey";
  374. db_query($sql);
  375. $sql = "ALTER INDEX tripal_views_priority_idx2 RENAME TO tripal_views_priority_idx";
  376. db_query($sql);
  377. }
  378. if (db_table_exists('tripal_views_field2')) {
  379. // tripal_mviews
  380. $sql = "DROP TABLE tripal_views_field";
  381. db_query($sql);
  382. $sql = "ALTER TABLE tripal_views_field2 RENAME to tripal_views_field";
  383. db_query($sql);
  384. $sql = "ALTER INDEX tripal_views_field_pkey2 RENAME TO tripal_views_field_pkey";
  385. db_query($sql);
  386. }
  387. if (db_table_exists('tripal_views_handlers2')) {
  388. // tripal_mviews
  389. $sql = "DROP TABLE tripal_views_handlers";
  390. db_query($sql);
  391. $sql = "ALTER TABLE tripal_views_handlers2 RENAME to tripal_views_handlers";
  392. db_query($sql);
  393. $sql = "ALTER INDEX tripal_views_handlers_pkey2 RENAME TO tripal_views_handlers_pkey";
  394. db_query($sql);
  395. }
  396. if (db_table_exists('tripal_views_join2')) {
  397. // tripal_mviews
  398. $sql = "DROP TABLE tripal_views_join";
  399. db_query($sql);
  400. $sql = "ALTER TABLE tripal_views_join2 RENAME to tripal_views_join";
  401. db_query($sql);
  402. $sql = "ALTER INDEX tripal_views_join_pkey2 RENAME TO tripal_views_join_pkey";
  403. db_query($sql);
  404. }
  405. }