tripal_core.chado_install.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 = chado_get_version(TRUE);
  14. // get the effective version. Pass true as second argument
  15. // to warn the user if the current version is not compatible
  16. $version = chado_get_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.3' => t('New Install of Chado v1.3 (erases all existing Chado data if Chado already exists)'),
  27. 'Upgrade Chado v1.2 to v1.3' => t('Upgrade existing Chado v1.2 to v1.3 (no data is lost)'),
  28. 'Install Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'),
  29. 'Upgrade Chado v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'),
  30. 'Install 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. If you want to install Chado all other Tripal modules must not be installed.'),
  33. '#required' => TRUE,
  34. );
  35. $form['warning'] = array(
  36. '#markup' => "<div><font color=\"red\">WARNING:</font>" . t('A new install of
  37. Chado will remove and recreate the Chado database if it already exists.') . '</div>',
  38. );
  39. $form['button'] = array(
  40. '#type' => 'submit',
  41. '#value' => t('Install/Upgrade Chado'),
  42. );
  43. return $form;
  44. }
  45. function tripal_core_chado_load_form_validate($form, &$form_state) {
  46. // We do not want to allow re-installation of Chado if other
  47. // Tripal modules are installed. This is because the install files
  48. // of those modules may add content to Chado and reinstalling Chado
  49. // removes that content which may break the modules.
  50. if ($form_state['values']['action_to_do'] == "Install Chado v1.2" or
  51. $form_state['values']['action_to_do'] == "Install Chado v1.11") {
  52. $modules = system_get_info('module');
  53. $list = array();
  54. foreach ($modules as $mname => $module) {
  55. if (array_key_exists('dependencies', $module) and in_array('tripal_core', $module['dependencies'])) {
  56. $list[] = $module['name'] . " ($mname)";
  57. }
  58. }
  59. if (count($list) > 0) {
  60. form_set_error("action_to_do", "Chado cannot be installed while other Tripal modules
  61. are enabled. You must fully uninstall the following modules if you
  62. would like to install or re-install chado.<br>" .
  63. implode("<br>", $list));
  64. }
  65. }
  66. if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.11 to v1.2") {
  67. // Make sure we are already not at v1.2
  68. $real_version = chado_get_version(TRUE);
  69. if ($real_version == "1.2") {
  70. form_set_error("action_to_do", "You are already at v1.2. There is no need to upgrade.");
  71. }
  72. }
  73. }
  74. /**
  75. * Submit Load Chado Schema Form
  76. *
  77. * @ingroup tripal_core
  78. */
  79. function tripal_core_chado_load_form_submit($form, &$form_state) {
  80. global $user;
  81. $action_to_do = trim($form_state['values']['action_to_do']);
  82. $args = array($action_to_do);
  83. tripal_add_job($action_to_do, 'tripal_core',
  84. 'tripal_core_install_chado', $args, $user->uid);
  85. }
  86. /**
  87. * Install Chado Schema
  88. *
  89. * @ingroup tripal_core
  90. */
  91. function tripal_core_install_chado($action) {
  92. $vsql = "
  93. INSERT INTO {chadoprop} (type_id, value)
  94. VALUES (
  95. (SELECT cvterm_id
  96. FROM {cvterm} CVT
  97. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  98. WHERE CV.name = 'chado_properties' AND CVT.name = 'version'),
  99. :version)
  100. ";
  101. try {
  102. if ($action == 'Install Chado v1.3') {
  103. tripal_core_install_chado_1_3();
  104. chado_query($vsql, array(':version' => '1.3'));
  105. }
  106. elseif ($action == 'Upgrade Chado v1.2 to v1.3') {
  107. tripal_core_upgrade_chado_1_2_to_1_3();
  108. chado_query($vsql, array(':version' => '1.3'));
  109. }
  110. elseif ($action == 'Install Chado v1.2') {
  111. tripal_core_install_chado_1_2();
  112. chado_query($vsql, array(':version' => '1.2'));
  113. }
  114. elseif ($action == 'Upgrade Chado v1.11 to v1.2') {
  115. tripal_core_upgrade_chado_1_11_to_1_2();
  116. chado_query($vsql, array(':version' => '1.2'));
  117. }
  118. elseif ($action == 'Install Chado v1.11') {
  119. tripal_core_install_chado_1_11();
  120. }
  121. }
  122. catch (Exception $e) {
  123. }
  124. }
  125. /**
  126. * Installs Chado v1.3.
  127. */
  128. function tripal_core_install_chado_1_3() {
  129. // Get the path to the schema and init SQL files.
  130. $schema_file = drupal_get_path('module', 'tripal_core') .
  131. '/chado_schema/default_schema-1.3.sql';
  132. $init_file = drupal_get_path('module', 'tripal_core') .
  133. '/chado_schema/initialize-1.3.sql';
  134. // Erase the Chado schema if it exists and perform the install.
  135. if (tripal_core_reset_chado_schema()) {
  136. $success = tripal_core_install_sql($schema_file);
  137. if ($success) {
  138. print "Install of Chado v1.3 (Step 1 of 2) Successful!\n";
  139. }
  140. else {
  141. throw new Exception("Installation (Step 1 of 2) Problems! Please check output above for errors.");
  142. }
  143. $success = tripal_core_install_sql($init_file);
  144. if ($success) {
  145. print "Install of Chado v1.3 (Step 2 of 2) Successful.\nInstallation Complete\n";
  146. }
  147. else {
  148. throw new Exception("Installation (Step 2 of 2) Problems! Please check output above for errors.");
  149. }
  150. }
  151. else {
  152. throw new Exception("ERROR: cannot install chado. Please check database permissions");
  153. }
  154. }
  155. /**
  156. * Installs Chado v1.2.
  157. */
  158. function tripal_core_install_chado_1_2() {
  159. // Get the path to the schema and init SQL files.
  160. $schema_file = drupal_get_path('module', 'tripal_core') .
  161. '/chado_schema/default_schema-1.2.sql';
  162. $init_file = drupal_get_path('module', 'tripal_core') .
  163. '/chado_schema/initialize-1.2.sql';
  164. // Erase the Chado schema if it exists and perform the install.
  165. if (tripal_core_reset_chado_schema()) {
  166. $success = tripal_core_install_sql($schema_file);
  167. if ($success) {
  168. print "Install of Chado v1.2 (Step 1 of 2) Successful!\n";
  169. }
  170. else {
  171. throw new Exception("Installation (Step 1 of 2) Problems! Please check output above for errors.");
  172. }
  173. $success = tripal_core_install_sql($init_file);
  174. if ($success) {
  175. print "Install of Chado v1.2 (Step 2 of 2) Successful.\nInstallation Complete\n";
  176. }
  177. else {
  178. throw new Exception("Installation (Step 2 of 2) Problems! Please check output above for errors.");
  179. }
  180. }
  181. else {
  182. throw new Exception("ERROR: cannot install chado. Please check database permissions");
  183. }
  184. }
  185. /**
  186. *
  187. */
  188. function tripal_core_install_chado_1_11() {
  189. // Get the path to the schema and init SQL files.
  190. $schema_file = drupal_get_path('module', 'tripal_core') .
  191. '/chado_schema/default_schema-1.11.sql';
  192. $init_file = drupal_get_path('module', 'tripal_core') .
  193. '/chado_schema/initialize-1.11.sql';
  194. // Erase the Chado schema if it exists and perform the install.
  195. if (tripal_core_reset_chado_schema()) {
  196. $success = tripal_core_install_sql($schema_file);
  197. if ($success) {
  198. print "Install of Chado v1.11 (Step 1 of 2) Successful!\n";
  199. }
  200. else {
  201. throw new Exception("Installation (Step 1 of 2) Problems! Please check output above for errors.");
  202. }
  203. $success = tripal_core_install_sql($init_file);
  204. if ($success) {
  205. print "Install of Chado v1.11 (Step 2 of 2) Successful.\nInstallation Complete!\n";
  206. }
  207. else {
  208. throw new Exception("Installation (Step 2 of 2) Problems! Please check output above for errors.");
  209. }
  210. }
  211. else {
  212. throw new Exception("ERROR: cannot install chado. Please check database permissions");
  213. }
  214. }
  215. /**
  216. * Upgrades Chado from v1.2 to v1.3
  217. */
  218. function tripal_core_upgrade_chado_1_2_to_1_3() {
  219. // Get the path to the diff schema and upgrade SQL files.
  220. $diff_file = drupal_get_path('module', 'tripal_core') .
  221. '/chado_schema/default_schema-1.2-1.3-diff.sql';
  222. $success = tripal_core_install_sql($diff_file);
  223. if ($success) {
  224. print "Upgrade from v1.2 to v1.3 Successful!\n";
  225. }
  226. else {
  227. throw new Exception("Upgrade problems! Please check output above for errors.");
  228. }
  229. }
  230. /**
  231. * Upgrades Chado from v1.11 to v1.2
  232. */
  233. function tripal_core_upgrade_chado_1_11_to_1_2() {
  234. // Get the path to the schema diff and upgarde SQL files.
  235. $schema_file = drupal_get_path('module', 'tripal_core') .
  236. '/chado_schema/default_schema-1.11-1.2-diff.sql';
  237. $init_file = drupal_get_path('module', 'tripal_core') .
  238. '/chado_schema/upgrade-1.11-1.2.sql';
  239. $success = tripal_core_install_sql($schema_file);
  240. if ($success) {
  241. print "Upgrade from v1.11 to v1.2 (Step 1 of 2) Successful!\n";
  242. }
  243. else {
  244. throw new Exception("Upgrade (Step 1 of 2) problems! Please check output above for errors.");
  245. }
  246. $success = tripal_core_install_sql($init_file);
  247. if ($success) {
  248. print "Upgrade from v1.11 to v1.2 (Step 2 of 2) Successful.\nUpgrade Complete!\n";
  249. }
  250. else {
  251. throw new Exception("Upgrade (Step 2 of 2) problems! Please check output above for errors.");
  252. }
  253. }
  254. /**
  255. * Reset the Chado Schema
  256. * This drops the current chado and chado-related schema and re-creates it
  257. *
  258. * @ingroup tripal_core
  259. */
  260. function tripal_core_reset_chado_schema() {
  261. // drop current chado and chado-related schema
  262. if (chado_dbschema_exists('genetic_code')) {
  263. print "Dropping existing 'genetic_code' schema\n";
  264. db_query("drop schema genetic_code cascade");
  265. }
  266. if (chado_dbschema_exists('so')) {
  267. print "Dropping existing 'so' schema\n";
  268. db_query("drop schema so cascade");
  269. }
  270. if (chado_dbschema_exists('frange')) {
  271. print "Dropping existing 'frange' schema\n";
  272. db_query("drop schema frange cascade");
  273. }
  274. if (chado_dbschema_exists('chado')) {
  275. print "Dropping existing 'chado' schema\n";
  276. db_query("drop schema chado cascade");
  277. }
  278. // create the new chado schema
  279. print "Creating 'chado' schema\n";
  280. db_query("create schema chado");
  281. if (chado_dbschema_exists('chado')) {
  282. // before creating the plpgsql language let's check to make sure
  283. // it doesn't already exists
  284. $sql = "SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'";
  285. $results = db_query($sql);
  286. $count = $results->fetchObject();
  287. if (!$count or $count->count == 0) {
  288. db_query("create language plpgsql");
  289. }
  290. return TRUE;
  291. }
  292. return FALSE;
  293. }
  294. /**
  295. * Execute the provided SQL
  296. *
  297. * @param $sql_file
  298. * Contains SQL statements to be executed
  299. *
  300. * @ingroup tripal_core
  301. */
  302. function tripal_core_install_sql($sql_file) {
  303. $chado_local = chado_dbschema_exists('chado');
  304. if ($chado_local) {
  305. db_query("set search_path to chado");
  306. }
  307. print "Loading $sql_file...\n";
  308. $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
  309. if (!$lines) {
  310. return 'Cannot open $schema_file';
  311. }
  312. $stack = array();
  313. $in_string = 0;
  314. $query = '';
  315. $i = 0;
  316. $success = 1;
  317. foreach ($lines as $line_num => $line) {
  318. $i++;
  319. $type = '';
  320. // find and remove comments except when inside of strings
  321. if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) {
  322. $line = preg_replace('/--.*$/', '', $line); // remove comments
  323. }
  324. if (preg_match('/\/\*.*?\*\//', $line)) {
  325. $line = preg_replace('/\/\*.*?\*\//', '', $line); // remove comments
  326. }
  327. // skip empty lines
  328. if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) {
  329. continue;
  330. }
  331. // Find SQL for new objects
  332. if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string) {
  333. $stack[] = 'table';
  334. $line = preg_replace("/public\./", "chado.", $line);
  335. }
  336. if (preg_match('/^\s*ALTER\s+TABLE/i', $line) and !$in_string) {
  337. $stack[] = 'alter table';
  338. $line = preg_replace("/public\./", "chado.", $line);
  339. }
  340. if (preg_match('/^\s*SET/i', $line) and !$in_string) {
  341. $stack[] = 'set';
  342. }
  343. if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string) {
  344. $stack[] = 'schema';
  345. }
  346. if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string) {
  347. $stack[] = 'sequence';
  348. $line = preg_replace("/public\./", "chado.", $line);
  349. }
  350. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string) {
  351. $stack[] = 'view';
  352. $line = preg_replace("/public\./", "chado.", $line);
  353. }
  354. if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0) {
  355. $stack[] = 'comment';
  356. $line = preg_replace("/public\./", "chado.", $line);
  357. }
  358. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string) {
  359. $stack[] = 'function';
  360. $line = preg_replace("/public\./", "chado.", $line);
  361. }
  362. if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string) {
  363. $stack[] = 'index';
  364. }
  365. if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string) {
  366. $stack[] = 'insert';
  367. $line = preg_replace("/public\./", "chado.", $line);
  368. }
  369. if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string) {
  370. $stack[] = 'type';
  371. }
  372. if (preg_match('/^\s*GRANT/i', $line) and !$in_string) {
  373. $stack[] = 'grant';
  374. }
  375. if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string) {
  376. $stack[] = 'aggregate';
  377. }
  378. // determine if we are in a string that spans a line
  379. $matches = preg_match_all("/[']/i", $line, $temp);
  380. $in_string = $in_string - ($matches % 2);
  381. $in_string = abs($in_string);
  382. // if we've reached the end of an object the pop the stack
  383. if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
  384. $type = array_pop($stack);
  385. }
  386. if (strcmp($stack[sizeof($stack)-1], 'alter table') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  387. $type = array_pop($stack);
  388. }
  389. if (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  390. $type = array_pop($stack);
  391. }
  392. if (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  393. $type = array_pop($stack);
  394. }
  395. if (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  396. $type = array_pop($stack);
  397. }
  398. if (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  399. $type = array_pop($stack);
  400. }
  401. if (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  402. $type = array_pop($stack);
  403. }
  404. if (strcmp($stack[sizeof($stack)-1], 'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i", $line)) {
  405. $type = array_pop($stack);
  406. }
  407. if (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  408. $type = array_pop($stack);
  409. }
  410. if (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
  411. $type = array_pop($stack);
  412. }
  413. if (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
  414. $type = array_pop($stack);
  415. }
  416. if (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  417. $type = array_pop($stack);
  418. }
  419. if (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
  420. $type = array_pop($stack);
  421. }
  422. // if we're in a recognized SQL statement then let's keep track of lines
  423. if ($type or sizeof($stack) > 0) {
  424. $query .= "$line";
  425. }
  426. else {
  427. print "UNHANDLED $i, $in_string: $line";
  428. tripal_core_chado_install_done();
  429. return FALSE;
  430. }
  431. if (preg_match_all("/\n/", $query, $temp) > 100) {
  432. print "SQL query is too long. Terminating:\n$query\n";
  433. tripal_core_chado_install_done();
  434. return FALSE;
  435. }
  436. if ($type and sizeof($stack) == 0) {
  437. //print "Adding $type: line $i\n";
  438. // rewrite the set search_path to make 'public' be 'chado', but only if the
  439. // chado schema exists
  440. if (strcmp($type, 'set') == 0 and $chado_local) {
  441. $query = preg_replace("/public/m", "chado", $query);
  442. }
  443. // execute the statement
  444. $result = db_query($query);
  445. if (!$result) {
  446. $error = pg_last_error();
  447. print "FAILED. Line $i, $in_string\n$error:\n$query\n\n";
  448. tripal_core_chado_install_done();
  449. $success = 0;
  450. return $success;
  451. }
  452. $query = '';
  453. }
  454. }
  455. tripal_core_chado_install_done();
  456. return $success;
  457. }
  458. /**
  459. * Finish the Chado Schema Installation
  460. *
  461. * @ingroup tripal_core
  462. */
  463. function tripal_core_chado_install_done() {
  464. db_query("set search_path to default");
  465. }