chado_install.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * @file
  4. * Functions to install chado schema through Drupal
  5. */
  6. /**
  7. * Load Chado Schema 1.11 Form
  8. *
  9. * @ingroup tripal_core
  10. */
  11. function tripal_core_chado_load_form() {
  12. $form['description'] = array(
  13. '#type' => 'item',
  14. '#value' => t("<font color=\"red\">WARNING:</font> A new install of Chado v1.2 or v1.11 "
  15. ."will install Chado within the Drupal database in a \"chado\" schema. If the \"chado\" schema already exists it will "
  16. ."be overwritten and all data will be lost. You may choose to update an existing Chado v1.11 if it was installed with a previous "
  17. ."version of Tripal (e.g. v0.3b or v0.3.1). The update will not erase any data. "
  18. ."If you are using chado in a database external to the "
  19. ."Drupal database with a 'chado' entry in the 'settings.php' \$db_url argument "
  20. ."then Chado will be installed but will not be used . The external "
  21. ."database specified in the settings.php file takes precedence."),
  22. '#weight' => 1,
  23. );
  24. $form['action_to_do'] = array(
  25. '#type' => 'radios',
  26. '#title' => 'Installation/Upgrade Action',
  27. '#options' => array(
  28. 'Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'),
  29. 'Upgrade v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'),
  30. 'Chado v1.11' => t('New Install of Chado v1.11 (erases all existing Chado data if Chado already exists)')
  31. ),
  32. '#description' => t('Select an action to perform'),
  33. '#required' => TRUE
  34. );
  35. $form['button'] = array(
  36. '#type' => 'submit',
  37. '#value' => t('Install/Upgrade Chado'),
  38. '#weight' => 2,
  39. );
  40. return $form;
  41. }
  42. /**
  43. * Submit Load Chado Schema 1.11 Form
  44. *
  45. * @ingroup tripal_core
  46. */
  47. function tripal_core_chado_v1_11_load_form_submit($form, &$form_state) {
  48. global $user;
  49. $action_to_do = trim($form_state['values']['action_to_do']);
  50. $args = array($action_to_do);
  51. tripal_add_job("Install Chado", 'tripal_core',
  52. 'tripal_core_install_chado', $args, $user->uid);
  53. }
  54. /**
  55. * Install Chado Schema
  56. *
  57. * @ingroup tripal_core
  58. */
  59. function tripal_core_install_chado() {
  60. $schema_file = drupal_get_path('module', 'tripal_core') . '/default_schema.sql';
  61. $init_file = drupal_get_path('module', 'tripal_core') . '/initialize.sql';
  62. if (tripal_core_reset_chado_schema()) {
  63. tripal_core_install_sql($schema_file);
  64. tripal_core_install_sql($init_file);
  65. }
  66. else {
  67. print "ERROR: cannot install chado. Please check database permissions\n";
  68. exit;
  69. }
  70. }
  71. /**
  72. * Reset the Chado Schema
  73. * This drops the current chado and chado-related schema and re-creates it
  74. *
  75. * @ingroup tripal_core
  76. */
  77. function tripal_core_reset_chado_schema() {
  78. global $active_db;
  79. // drop current chado and chado-related schema
  80. if (tripal_core_schema_exists('chado')) {
  81. print "Dropping existing 'chado' schema\n";
  82. pg_query($active_db, "drop schema chado cascade");
  83. }
  84. if (tripal_core_schema_exists('genetic_code')) {
  85. print "Dropping existing 'genetic_code' schema\n";
  86. pg_query($active_db, "drop schema genetic_code cascade");
  87. }
  88. if (tripal_core_schema_exists('so')) {
  89. print "Dropping existing 'so' schema\n";
  90. pg_query($active_db, "drop schema so cascade");
  91. }
  92. if (tripal_core_schema_exists('frange')) {
  93. print "Dropping existing 'frange' schema\n";
  94. pg_query($active_db, "drop schema frange cascade");
  95. }
  96. // create the new chado schema
  97. print "Creating 'chado' schema\n";
  98. pg_query($active_db, "create schema chado");
  99. if (tripal_core_schema_exists('chado')) {
  100. pg_query($active_db, "create language plpgsql");
  101. return TRUE;
  102. }
  103. return FALSE;
  104. }
  105. /**
  106. * Check that a given schema exists
  107. *
  108. * @param $schema
  109. * The name of the schema to check the existence of
  110. *
  111. * @return
  112. * TRUE/FALSE depending upon whether or not the schema exists
  113. *
  114. * @ingroup tripal_core
  115. */
  116. function tripal_core_schema_exists($schema) {
  117. // check that the chado schema now exists
  118. $sql = "SELECT nspname
  119. FROM pg_namespace
  120. WHERE has_schema_privilege(nspname, 'USAGE') and nspname = '%s'
  121. ORDER BY nspname";
  122. $name = db_fetch_object(db_query($sql, $schema));
  123. if (strcmp($name->nspname, $schema) != 0) {
  124. return FALSE;
  125. }
  126. return TRUE;
  127. }
  128. /**
  129. * Execute the provided SQL
  130. *
  131. * @param $sql_file
  132. * Contains SQL statements to be executed
  133. *
  134. * @ingroup tripal_core
  135. */
  136. function tripal_core_install_sql($sql_file) {
  137. global $active_db;
  138. pg_query($active_db, "set search_path to chado,public");
  139. print "Loading $sql_file...\n";
  140. $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
  141. if (!$lines) {
  142. return 'Cannot open $schema_file';
  143. }
  144. $stack = array();
  145. $in_string = 0;
  146. $query = '';
  147. $i = 0;
  148. foreach ($lines as $line_num => $line) {
  149. $i++;
  150. $type = '';
  151. // find and remove comments except when inside of strings
  152. if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) {
  153. $line = preg_replace('/--.*$/', '', $line); // remove comments
  154. }
  155. if (preg_match('/\/\*.*?\*\//', $line)) {
  156. $line = preg_replace('/\/\*.*?\*\//', '', $line); // remove comments
  157. }
  158. // skip empty lines
  159. if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) {
  160. continue;
  161. }
  162. // Find SQL for new objects
  163. if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string) {
  164. $stack[] = 'table';
  165. $line = preg_replace("/public./", "chado.", $line);
  166. }
  167. if (preg_match('/^\s*ALTER\s+TABLE/i', $line) and !$in_string) {
  168. $stack[] = 'alter table';
  169. $line = preg_replace("/public./", "chado.", $line);
  170. }
  171. if (preg_match('/^\s*SET/i', $line) and !$in_string) {
  172. $stack[] = 'set';
  173. }
  174. if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string) {
  175. $stack[] = 'schema';
  176. }
  177. if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string) {
  178. $stack[] = 'sequence';
  179. $line = preg_replace("/public./", "chado.", $line);
  180. }
  181. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string) {
  182. $stack[] = 'view';
  183. $line = preg_replace("/public./", "chado.", $line);
  184. }
  185. if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0) {
  186. $stack[] = 'comment';
  187. $line = preg_replace("/public./", "chado.", $line);
  188. }
  189. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string) {
  190. $stack[] = 'function';
  191. $line = preg_replace("/public./", "chado.", $line);
  192. }
  193. if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string) {
  194. $stack[] = 'index';
  195. }
  196. if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string) {
  197. $stack[] = 'insert';
  198. $line = preg_replace("/public./", "chado.", $line);
  199. }
  200. if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string) {
  201. $stack[] = 'type';
  202. }
  203. if (preg_match('/^\s*GRANT/i', $line) and !$in_string) {
  204. $stack[] = 'grant';
  205. }
  206. if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string) {
  207. $stack[] = 'aggregate';
  208. }
  209. // determine if we are in a string that spans a line
  210. $matches = preg_match_all("/[']/i", $line, $temp);
  211. $in_string = $in_string - ($matches % 2);
  212. $in_string = abs($in_string);
  213. // if we've reached the end of an object the pop the stack
  214. if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
  215. $type = array_pop($stack);
  216. }
  217. if (strcmp($stack[sizeof($stack)-1], 'alter table') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  218. $type = array_pop($stack);
  219. }
  220. if (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  221. $type = array_pop($stack);
  222. }
  223. if (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  224. $type = array_pop($stack);
  225. }
  226. if (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  227. $type = array_pop($stack);
  228. }
  229. if (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  230. $type = array_pop($stack);
  231. }
  232. if (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  233. $type = array_pop($stack);
  234. }
  235. if (strcmp($stack[sizeof($stack)-1], 'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i", $line)) {
  236. $type = array_pop($stack);
  237. }
  238. if (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  239. $type = array_pop($stack);
  240. }
  241. if (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
  242. $type = array_pop($stack);
  243. }
  244. if (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
  245. $type = array_pop($stack);
  246. }
  247. if (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  248. $type = array_pop($stack);
  249. }
  250. if (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
  251. $type = array_pop($stack);
  252. }
  253. // if we're in a recognized SQL statement then let's keep track of lines
  254. if ($type or sizeof($stack) > 0) {
  255. $query .= "$line";
  256. }
  257. else {
  258. print "UNHANDLED $i, $in_string: $line";
  259. return tripal_core_chado_install_done();
  260. }
  261. if (preg_match_all("/\n/", $query, $temp) > 100) {
  262. print "SQL query is too long. Terminating:\n$query\n";
  263. return tripal_core_chado_install_done();
  264. }
  265. if ($type and sizeof($stack) == 0) {
  266. print "Adding $type: line $i\n";
  267. // rewrite the set serach_path to make 'public' be 'chado'
  268. if (strcmp($type, 'set')==0) {
  269. $query = preg_replace("/public/m", "chado", $query);
  270. }
  271. $result = pg_query($active_db, $query);
  272. if (!$result) {
  273. $error = pg_last_error();
  274. print "Installation failed:\nSQL $i, $in_string: $query\n$error\n";
  275. pg_query($active_db, "set search_path to public,chado");
  276. return tripal_core_chado_install_done();
  277. }
  278. $query = '';
  279. }
  280. }
  281. print "Installation Complete!\n";
  282. tripal_core_chado_install_done();
  283. }
  284. /**
  285. * Finish the Chado Schema Installation
  286. *
  287. * @ingroup tripal_core
  288. */
  289. function tripal_core_chado_install_done() {
  290. // return the search path to normal
  291. global $active_db;
  292. pg_query($active_db, "set search_path to public,chado");
  293. }