tripal_entities.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * @file
  4. * Install for a tripal data entity - creates the base table for our entity.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function tripal_entities_install() {
  10. // Add tripal bundle variables needed for storing additional settings for Tripal Bundles.
  11. tripal_insert_variable('title_format', 'A pattern including tokens that can be used to generate tripal entity titles.');
  12. tripal_insert_variable('url_format', 'A pattern including tokens that can be used to generate tripal entity url aliases.');
  13. tripal_insert_variable('description', 'The description of a Tripal Entity type/bundle.');
  14. }
  15. /**
  16. * Implements hook_uninstall().
  17. */
  18. function tripal_entities_uninstall() {
  19. /*
  20. // So somehow I was able to uninstall this module without deleting the bundles. This
  21. // caused aweful errors because fields weren't deleted so when I re-installed, the code
  22. // tried to create fields that were inactive (despite checking if the field exists
  23. // before creating). The following code was meant to ensure that all content was deleted
  24. // before uninstall so these errors would not occur. Unfortunatly I am now unable to
  25. // test this because the Field API module is disabling uninstall of Tripal Chado until
  26. // all the content is deleted. Thus ensuring the errors described above don't occur.
  27. // But I'm Sure I was able to uninstall with content before...
  28. // **I am slowly going crazy; Crazy going slowly am I**
  29. // Anyway, I'll leaving the solution code here in case I am able to repeat it in
  30. // the future.
  31. // @see https://www.drupal.org/node/1262092
  32. // @see https://www.drupal.org/node/1861710
  33. // First delete all TripalEntities.
  34. $entity_ids = (new EntityFieldQuery)->entityCondition("entity_type", "TripalEntity")->execute();
  35. $entity_ids = reset($entity_ids);
  36. entity_delete_multiple("TripalEntity", array_keys($entity_ids));
  37. // Then delete all TripalBundles.
  38. $bundle_ids = (new EntityFieldQuery)->entityCondition("entity_type", "TripalBundle")->execute();
  39. $bundle_ids = reset($bundle_ids);
  40. entity_delete_multiple("TripalBundle", array_keys($bundle_ids));
  41. // @TODO: Should we delete all TripalVocabularies and TripalTerms?
  42. // Finally purge all fields that are no longer used.
  43. field_purge_batch(100);
  44. */
  45. }
  46. /**
  47. * Implements hook_schema().
  48. */
  49. function tripal_entities_schema() {
  50. // Adds a table for managing TripalEntity entities.
  51. $schema['tripal_vocab'] = tripal_entities_tripal_vocab_schema();
  52. $schema['tripal_term'] = tripal_entities_tripal_term_schema();
  53. $schema['tripal_entity'] = tripal_entities_tripal_entity_schema();
  54. $schema['tripal_bundle'] = tripal_entities_tripal_bundle_schema();
  55. // Adds a table for additional information related to bundles.
  56. $schema['tripal_bundle_variables'] = tripal_entities_tripal_bundle_variables_schema();
  57. return $schema;
  58. }
  59. /**
  60. * @section
  61. * Schema Definitions.
  62. */
  63. /**
  64. * The base table for Biological Data Entities.
  65. *
  66. * This contains the actual data. For example, if you have a 5 genes and 10 mRNA then
  67. * this table will have 15 records and include both genes and mRNA's.
  68. */
  69. function tripal_entities_tripal_entity_schema() {
  70. $schema = array(
  71. 'description' => 'The base table for Tripal Vocabulary-based entities.',
  72. 'fields' => array(
  73. 'id' => array(
  74. 'description' => 'The primary identifier for a vocabulary entity.',
  75. 'type' => 'serial',
  76. 'unsigned' => TRUE,
  77. 'not null' => TRUE,
  78. ),
  79. 'type' => array(
  80. 'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
  81. 'type' => 'varchar',
  82. 'length' => 64,
  83. 'not null' => TRUE,
  84. 'default' => '',
  85. ),
  86. 'bundle' => array(
  87. 'description' => 'The type of bundle. This should be an official vocabulary ID (e.g. SO, RO, GO) followed by an underscore and the term accession.',
  88. 'type' => 'varchar',
  89. 'length' => 1024,
  90. 'not null' => TRUE,
  91. 'default' => '',
  92. ),
  93. 'term_id' => array(
  94. 'description' => 'The term_id for the type of entity. This term_id corresponds to a TripalTerm record.',
  95. 'type' => 'int',
  96. 'not null' => TRUE,
  97. ),
  98. 'title' => array(
  99. 'description' => 'The title of this node, always treated as non-markup plain text.',
  100. 'type' => 'text',
  101. 'not null' => TRUE,
  102. 'default' => '',
  103. ),
  104. 'uid' => array(
  105. 'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
  106. 'type' => 'int',
  107. 'not null' => TRUE,
  108. 'default' => 0,
  109. ),
  110. 'status' => array(
  111. 'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
  112. 'type' => 'int',
  113. 'not null' => TRUE,
  114. 'default' => 1,
  115. ),
  116. 'created' => array(
  117. 'description' => 'The Unix timestamp when the node was created.',
  118. 'type' => 'int',
  119. 'not null' => TRUE,
  120. 'default' => 0,
  121. ),
  122. 'changed' => array(
  123. 'description' => 'The Unix timestamp when the node was most recently saved.',
  124. 'type' => 'int',
  125. 'not null' => TRUE,
  126. 'default' => 0,
  127. ),
  128. ),
  129. 'indexes' => array(
  130. 'term_id' => array('term_id'),
  131. 'entity_changed' => array('changed'),
  132. 'entity_created' => array('created'),
  133. 'type' => array('type'),
  134. 'uid' => array('uid'),
  135. ),
  136. 'unique keys' => array(),
  137. 'primary key' => array('id'),
  138. );
  139. return $schema;
  140. }
  141. /**
  142. * The base table for TripalVocab schema.
  143. *
  144. * This contains the actual data. For example, if you have a 5 genes and 10 mRNA then
  145. * this table will have 15 records and include both genes and mRNA's.
  146. */
  147. function tripal_entities_tripal_vocab_schema() {
  148. // This schema only provides enough information to assign a unique ID
  149. // to the vocabulary. Any additonal information is added to the Entity object
  150. // by the selected database back-end.
  151. $schema = array(
  152. 'description' => 'The base table for TripalVocab entities.',
  153. 'fields' => array(
  154. 'id' => array(
  155. 'description' => 'The primary identifier for a vocab entity.',
  156. 'type' => 'serial',
  157. 'unsigned' => TRUE,
  158. 'not null' => TRUE,
  159. ),
  160. 'namespace' => array(
  161. 'description' => 'The namespace for the vocabulary (e.g. SO, PATO, etc.).',
  162. 'type' => 'varchar',
  163. 'length' => 10,
  164. 'not null' => TRUE,
  165. ),
  166. 'created' => array(
  167. 'description' => 'The Unix timestamp when the entity was created.',
  168. 'type' => 'int',
  169. 'not null' => TRUE,
  170. 'default' => 0,
  171. ),
  172. 'changed' => array(
  173. 'description' => 'The Unix timestamp when the entity was most recently saved.',
  174. 'type' => 'int',
  175. 'not null' => TRUE,
  176. 'default' => 0,
  177. ),
  178. ),
  179. 'indexes' => array(
  180. 'namespace' => array('namespace'),
  181. 'entity_changed' => array('changed'),
  182. 'entity_created' => array('created'),
  183. ),
  184. 'unique keys' => array('namespace' => array('namespace')),
  185. 'primary key' => array('id'),
  186. );
  187. return $schema;
  188. }
  189. /**
  190. * The base table for TripalTerm entities.
  191. *
  192. * This contains the actual data. For example, if you have a 5 genes and 10 mRNA then
  193. * this table will have 15 records and include both genes and mRNA's.
  194. */
  195. function tripal_entities_tripal_term_schema() {
  196. // This schema only provides enough information to assign a unique ID
  197. // to the term and associate it to it's vocabulary. Any additonal information
  198. // is added to the Entity object by the selected database back-end.
  199. $schema = array(
  200. 'description' => 'The base table for TripalTerm entities.',
  201. 'fields' => array(
  202. 'id' => array(
  203. 'description' => 'The primary identifier for a term entity.',
  204. 'type' => 'serial',
  205. 'unsigned' => TRUE,
  206. 'not null' => TRUE,
  207. ),
  208. 'vocab_id' => array(
  209. 'description' => 'The vocabulary_id of the TripalVocab entity to which this term belongs.',
  210. 'type' => 'int',
  211. 'not null' => TRUE,
  212. ),
  213. 'accession' => array(
  214. 'description' => 'The id (or accession) of this term in the vocabulary.',
  215. 'type' => 'varchar',
  216. 'length' => 1024,
  217. 'not null' => TRUE,
  218. 'default' => '',
  219. ),
  220. 'name' => array(
  221. 'description' => 'The human readable name for this term.',
  222. 'type' => 'varchar',
  223. 'length' => 1024,
  224. 'not null' => TRUE,
  225. 'default' => '',
  226. ),
  227. 'created' => array(
  228. 'description' => 'The Unix timestamp when the entity was created.',
  229. 'type' => 'int',
  230. 'not null' => TRUE,
  231. 'default' => 0,
  232. ),
  233. 'changed' => array(
  234. 'description' => 'The Unix timestamp when the entity was most recently saved.',
  235. 'type' => 'int',
  236. 'not null' => TRUE,
  237. 'default' => 0,
  238. ),
  239. ),
  240. 'indexes' => array(
  241. 'vocab_id' => array('vocab_id'),
  242. 'accession' => array('accession'),
  243. 'entity_changed' => array('changed'),
  244. 'entity_created' => array('created'),
  245. ),
  246. 'foreign keys' => array(
  247. 'tripal_vocab' => array(
  248. 'table' => 'tripal_vocab',
  249. 'columns' => array(
  250. 'vocab_id' => 'vocab_id',
  251. ),
  252. ),
  253. ),
  254. 'unique keys' => array('vocab_term' => array('vocab_id', 'accession')),
  255. 'primary key' => array('id'),
  256. );
  257. return $schema;
  258. }
  259. /**
  260. * The base table for TripalEntity entities.
  261. *
  262. * This table contains a list of Biological Data Types.
  263. * For the example above (5 genes and 10 mRNAs), there would only be two records in
  264. * this table one for "gene" and another for "mRNA".
  265. */
  266. function tripal_entities_tripal_bundle_schema() {
  267. $schema = array(
  268. 'description' => 'Stores information about defined tripal data types.',
  269. 'fields' => array(
  270. 'id' => array(
  271. 'type' => 'serial',
  272. 'not null' => TRUE,
  273. 'description' => 'Primary Key: Unique numeric ID.',
  274. ),
  275. 'type' => array(
  276. 'description' => 'The type of entity (e.g. TripalEntity).',
  277. 'type' => 'varchar',
  278. 'length' => 64,
  279. 'not null' => TRUE,
  280. 'default' => '',
  281. ),
  282. 'term_id' => array(
  283. 'description' => 'The term_id for the type of entity. This term_id corresponds to a TripalTerm record.',
  284. 'type' => 'int',
  285. 'not null' => TRUE,
  286. ),
  287. 'name' => array(
  288. 'description' => 'The name of the bundle. This should be an official vocabulary ID (e.g. SO, RO, GO) followed by an underscore and the term accession.',
  289. 'type' => 'varchar',
  290. 'length' => 1024,
  291. 'not null' => TRUE,
  292. 'default' => '',
  293. ),
  294. 'label' => array(
  295. 'description' => 'The human-readable name of this bundle.',
  296. 'type' => 'varchar',
  297. 'length' => 255,
  298. 'not null' => TRUE,
  299. 'default' => '',
  300. ),
  301. ),
  302. 'indexes' => array(
  303. 'name' => array('name'),
  304. 'term_id' => array('term_id'),
  305. ),
  306. 'primary key' => array('id'),
  307. 'unique keys' => array(
  308. 'name' => array('name'),
  309. ),
  310. );
  311. return $schema;
  312. }
  313. /**
  314. * Additional Tripal Bundle Information.
  315. *
  316. * This table is used for storing any additonal information describing
  317. * a tripal bundle. For example, this is a good place to store title/url formats.
  318. */
  319. function tripal_entities_tripal_bundle_variables_schema() {
  320. $schema = array(
  321. 'description' => 'This table is used for storing any additonal information describing
  322. a tripal bundle. For example, this is a good place to store title/url formats.',
  323. 'fields' => array (
  324. 'bundle_variable_id' => array (
  325. 'type' => 'serial',
  326. 'not null' => TRUE,
  327. ),
  328. 'bundle_id' => array (
  329. 'type' => 'int',
  330. 'not null' => TRUE,
  331. ),
  332. 'variable_id' => array (
  333. 'type' => 'int',
  334. 'not null' => TRUE,
  335. ),
  336. 'value' => array (
  337. 'type' => 'text',
  338. 'not null' => FALSE,
  339. ),
  340. 'rank' => array (
  341. 'type' => 'int',
  342. 'not null' => TRUE,
  343. 'default' => 0,
  344. ),
  345. ),
  346. 'primary key' => array (
  347. 0 => 'bundle_variable_id',
  348. ),
  349. 'unique keys' => array (
  350. 'tripal_bundle_variables_c1' => array (
  351. 0 => 'bundle_id',
  352. 1 => 'variable_id',
  353. 2 => 'rank',
  354. ),
  355. ),
  356. 'indexes' => array (
  357. 'tripal_bundle_variables_idx1' => array (
  358. 0 => 'variable_id',
  359. ),
  360. ),
  361. 'foreign keys' => array (
  362. 'tripal_variables' => array (
  363. 'table' => 'tripal_variables',
  364. 'columns' => array (
  365. 'variable_id' => 'variable_id',
  366. ),
  367. ),
  368. ),
  369. );
  370. return $schema;
  371. }