chado_install.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * @file
  4. * Functions to install chado schema through Drupal
  5. */
  6. /**
  7. * Load Chado Schema Form
  8. *
  9. * @ingroup tripal_core
  10. */
  11. function tripal_core_chado_load_form() {
  12. // we want to force the version of Chado to be set properly
  13. $real_version = tripal_core_set_chado_version();
  14. // get the effective version. Pass true as second argument
  15. // to warn the user if the current version is not compatible
  16. $version = tripal_core_get_chado_version(FALSE, TRUE);
  17. $form['current_version'] = array(
  18. '#type' => 'item',
  19. '#title' => t("Current installed version of Chado:"),
  20. '#description' => $real_version,
  21. );
  22. $form['action_to_do'] = array(
  23. '#type' => 'radios',
  24. '#title' => 'Installation/Upgrade Action',
  25. '#options' => array(
  26. 'Install Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'),
  27. 'Upgrade Chado v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'),
  28. 'Install Chado v1.11' => t('New Install of Chado v1.11 (erases all existing Chado data if Chado already exists)')
  29. ),
  30. '#description' => t('Select an action to perform'),
  31. '#required' => TRUE
  32. );
  33. $form['description'] = array(
  34. '#type' => 'item',
  35. '#value' => t("<font color=\"red\">WARNING:</font> A new install of Chado v1.2 or v1.11 "
  36. . "will install Chado within the Drupal database in a \"chado\" schema. If the \"chado\" schema already exists it will "
  37. . "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 "
  38. . "version of Tripal (e.g. v0.3b or v0.3.1). The update will not erase any data. "
  39. . "If you are using chado in a database external to the "
  40. . "Drupal database with a 'chado' entry in the 'settings.php' \$db_url argument "
  41. . "then Chado will be installed but will not be used . The external "
  42. . "database specified in the settings.php file takes precedence."),
  43. );
  44. $form['button'] = array(
  45. '#type' => 'submit',
  46. '#value' => t('Install/Upgrade Chado'),
  47. '#weight' => 2,
  48. );
  49. return $form;
  50. }
  51. /**
  52. * Submit Load Chado Schema 1.11 Form
  53. *
  54. * @ingroup tripal_core
  55. */
  56. function tripal_core_chado_load_form_submit($form, &$form_state) {
  57. global $user;
  58. $action_to_do = trim($form_state['values']['action_to_do']);
  59. $args = array($action_to_do);
  60. tripal_add_job($action_to_do, 'tripal_core', 'tripal_core_install_chado', $args, $user->uid);
  61. }
  62. /**
  63. * Install Chado Schema
  64. *
  65. * @ingroup tripal_core
  66. */
  67. function tripal_core_install_chado($action) {
  68. $vsql = "INSERT INTO {chadoprop} (type_id, value) VALUES " .
  69. "((SELECT cvterm_id " .
  70. "FROM {cvterm} CVT " .
  71. " INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id " .
  72. "WHERE CV.name = 'chado_properties' AND CVT.name = 'version'), " .
  73. "'%s') ";
  74. if ($action == 'Install Chado v1.2') {
  75. $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.2.sql';
  76. $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/initialize-1.2.sql';
  77. if (tripal_core_reset_chado_schema()) {
  78. $success = tripal_core_install_sql($schema_file);
  79. if ($success) {
  80. print "Install of Chado v1.2 (Step 1 of 2) Successful!\n";
  81. }
  82. else {
  83. print "Installation (Step 1 of 2) Problems! Please check output above for errors.\n";
  84. exit;
  85. }
  86. $success = tripal_core_install_sql($init_file);
  87. if ($success) {
  88. print "Install of Chado v1.2 (Step 2 of 2) Successful.\nInstallation Complete\n";
  89. }
  90. else {
  91. print "Installation (Step 2 of 2) Problems! Please check output above for errors.\n";
  92. exit;
  93. }
  94. chado_query($vsql, '1.2'); # set the version
  95. }
  96. else {
  97. print "ERROR: cannot install chado. Please check database permissions\n";
  98. exit;
  99. }
  100. }
  101. elseif ($action == 'Upgrade Chado v1.11 to v1.2') {
  102. $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.11-1.2-diff.sql';
  103. $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/upgrade-1.11-1.2.sql';
  104. $success = tripal_core_install_sql($schema_file);
  105. if ($success) {
  106. print "Upgrade from v1.11 to v1.2 (Step 1 of 2) Successful!\n";
  107. }
  108. else {
  109. print "Upgrade (Step 1 of 2) problems! Please check output above for errors.\n";
  110. exit;
  111. }
  112. $success = tripal_core_install_sql($init_file);
  113. if ($success) {
  114. print "Upgrade from v1.11 to v1.2 (Step 2 of 2) Successful.\nUpgrade Complete!\n";
  115. }
  116. else {
  117. print "Upgrade (Step 2 of 2) problems! Please check output above for errors.\n";
  118. exit;
  119. }
  120. chado_query($vsql, '1.2'); # set the version
  121. }
  122. elseif ($action == 'Install Chado v1.11') {
  123. $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.11.sql';
  124. $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/initialize-1.11.sql';
  125. if (tripal_core_reset_chado_schema()) {
  126. $success = tripal_core_install_sql($schema_file);
  127. if ($success) {
  128. print "Install of Chado v1.11 (Step 1 of 2) Successful!\n";
  129. }
  130. else {
  131. print "Installation (Step 1 of 2) Problems! Please check output above for errors.\n";
  132. exit;
  133. }
  134. $success = tripal_core_install_sql($init_file);
  135. if ($success) {
  136. print "Install of Chado v1.11 (Step 2 of 2) Successful.\nInstallation Complete!\n";
  137. }
  138. else {
  139. print "Installation (Step 2 of 2) Problems! Please check output above for errors.\n";
  140. exit;
  141. }
  142. }
  143. else {
  144. print "ERROR: cannot install chado. Please check database permissions\n";
  145. exit;
  146. }
  147. }
  148. }
  149. /**
  150. * Reset the Chado Schema
  151. * This drops the current chado and chado-related schema and re-creates it
  152. *
  153. * @ingroup tripal_core
  154. */
  155. function tripal_core_reset_chado_schema() {
  156. // drop current chado and chado-related schema
  157. if (tripal_core_schema_exists('chado')) {
  158. print "Dropping existing 'chado' schema\n";
  159. db_query("drop schema chado cascade");
  160. }
  161. if (tripal_core_schema_exists('genetic_code')) {
  162. print "Dropping existing 'genetic_code' schema\n";
  163. db_query("drop schema genetic_code cascade");
  164. }
  165. if (tripal_core_schema_exists('so')) {
  166. print "Dropping existing 'so' schema\n";
  167. db_query("drop schema so cascade");
  168. }
  169. if (tripal_core_schema_exists('frange')) {
  170. print "Dropping existing 'frange' schema\n";
  171. db_query("drop schema frange cascade");
  172. }
  173. // create the new chado schema
  174. print "Creating 'chado' schema\n";
  175. db_query("create schema chado");
  176. if (tripal_core_schema_exists('chado')) {
  177. // before creating the plpgsql language let's check to make sure
  178. // it doesn't already exists
  179. $sql = "SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'";
  180. $results = db_query($sql);
  181. $count = $results->fetchObject();
  182. if (!$count or $count->count == 0) {
  183. db_query("create language plpgsql");
  184. }
  185. return TRUE;
  186. }
  187. return FALSE;
  188. }
  189. /**
  190. * Execute the provided SQL
  191. *
  192. * @param $sql_file
  193. * Contains SQL statements to be executed
  194. *
  195. * @ingroup tripal_core
  196. */
  197. function tripal_core_install_sql($sql_file) {
  198. $chado_local = tripal_core_schema_exists('chado');
  199. if ($chado_local) {
  200. db_query("set search_path to chado");
  201. }
  202. print "Loading $sql_file...\n";
  203. $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
  204. if (!$lines) {
  205. return 'Cannot open $schema_file';
  206. }
  207. $stack = array();
  208. $in_string = 0;
  209. $query = '';
  210. $i = 0;
  211. $success = 1;
  212. foreach ($lines as $line_num => $line) {
  213. $i++;
  214. $type = '';
  215. // find and remove comments except when inside of strings
  216. if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) {
  217. $line = preg_replace('/--.*$/', '', $line); // remove comments
  218. }
  219. if (preg_match('/\/\*.*?\*\//', $line)) {
  220. $line = preg_replace('/\/\*.*?\*\//', '', $line); // remove comments
  221. }
  222. // skip empty lines
  223. if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) {
  224. continue;
  225. }
  226. // Find SQL for new objects
  227. if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string) {
  228. $stack[] = 'table';
  229. $line = preg_replace("/public./", "chado.", $line);
  230. }
  231. if (preg_match('/^\s*ALTER\s+TABLE/i', $line) and !$in_string) {
  232. $stack[] = 'alter table';
  233. $line = preg_replace("/public./", "chado.", $line);
  234. }
  235. if (preg_match('/^\s*SET/i', $line) and !$in_string) {
  236. $stack[] = 'set';
  237. }
  238. if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string) {
  239. $stack[] = 'schema';
  240. }
  241. if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string) {
  242. $stack[] = 'sequence';
  243. $line = preg_replace("/public./", "chado.", $line);
  244. }
  245. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string) {
  246. $stack[] = 'view';
  247. $line = preg_replace("/public./", "chado.", $line);
  248. }
  249. if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0) {
  250. $stack[] = 'comment';
  251. $line = preg_replace("/public./", "chado.", $line);
  252. }
  253. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string) {
  254. $stack[] = 'function';
  255. $line = preg_replace("/public./", "chado.", $line);
  256. }
  257. if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string) {
  258. $stack[] = 'index';
  259. }
  260. if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string) {
  261. $stack[] = 'insert';
  262. $line = preg_replace("/public./", "chado.", $line);
  263. }
  264. if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string) {
  265. $stack[] = 'type';
  266. }
  267. if (preg_match('/^\s*GRANT/i', $line) and !$in_string) {
  268. $stack[] = 'grant';
  269. }
  270. if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string) {
  271. $stack[] = 'aggregate';
  272. }
  273. // determine if we are in a string that spans a line
  274. $matches = preg_match_all("/[']/i", $line, $temp);
  275. $in_string = $in_string - ($matches % 2);
  276. $in_string = abs($in_string);
  277. // if we've reached the end of an object the pop the stack
  278. if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
  279. $type = array_pop($stack);
  280. }
  281. if (strcmp($stack[sizeof($stack)-1], 'alter table') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  282. $type = array_pop($stack);
  283. }
  284. if (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  285. $type = array_pop($stack);
  286. }
  287. if (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  288. $type = array_pop($stack);
  289. }
  290. if (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  291. $type = array_pop($stack);
  292. }
  293. if (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  294. $type = array_pop($stack);
  295. }
  296. if (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  297. $type = array_pop($stack);
  298. }
  299. if (strcmp($stack[sizeof($stack)-1], 'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i", $line)) {
  300. $type = array_pop($stack);
  301. }
  302. if (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  303. $type = array_pop($stack);
  304. }
  305. if (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
  306. $type = array_pop($stack);
  307. }
  308. if (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
  309. $type = array_pop($stack);
  310. }
  311. if (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  312. $type = array_pop($stack);
  313. }
  314. if (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
  315. $type = array_pop($stack);
  316. }
  317. // if we're in a recognized SQL statement then let's keep track of lines
  318. if ($type or sizeof($stack) > 0) {
  319. $query .= "$line";
  320. }
  321. else {
  322. print "UNHANDLED $i, $in_string: $line";
  323. tripal_core_chado_install_done();
  324. return FALSE;
  325. }
  326. if (preg_match_all("/\n/", $query, $temp) > 100) {
  327. print "SQL query is too long. Terminating:\n$query\n";
  328. tripal_core_chado_install_done();
  329. return FALSE;
  330. }
  331. if ($type and sizeof($stack) == 0) {
  332. //print "Adding $type: line $i\n";
  333. // rewrite the set search_path to make 'public' be 'chado', but only if the
  334. // chado schema exists
  335. if (strcmp($type, 'set') == 0 and $chado_local) {
  336. $query = preg_replace("/public/m", "chado", $query);
  337. }
  338. if (!$chado_local) {
  339. $previous = tripal_db_set_active('chado');
  340. }
  341. $result = db_query($query);
  342. if (!$chado_local) {
  343. tripal_db_set_active($previous);
  344. }
  345. if (!$result) {
  346. $error = pg_last_error();
  347. print "FAILED. Line $i, $in_string\n$error:\n$query\n\n";
  348. tripal_core_chado_install_done();
  349. $success = 0;
  350. return $success;
  351. }
  352. $query = '';
  353. }
  354. }
  355. tripal_core_chado_install_done();
  356. return $success;
  357. }
  358. /**
  359. * Finish the Chado Schema Installation
  360. *
  361. * @ingroup tripal_core
  362. */
  363. function tripal_core_chado_install_done() {
  364. // return the search path to normal
  365. db_query("set search_path to public");
  366. }