tripal_core.chado_install.inc 21 KB

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