chado_install.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. // 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. '#value' => $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',
  61. 'tripal_core_install_chado', $args, $user->uid);
  62. }
  63. /**
  64. * Install Chado Schema
  65. *
  66. * @ingroup tripal_core
  67. */
  68. function tripal_core_install_chado($action) {
  69. $vsql = "INSERT INTO chadoprop (type_id, value) VALUES "
  70. ."((SELECT cvterm_id "
  71. ."FROM cvterm CVT "
  72. ." INNER JOIN cv CV on CVT.cv_id = CV.cv_id "
  73. ."WHERE CV.name = 'chado_properties' AND CVT.name = 'version'), "
  74. ."'%s') ";
  75. if($action == 'Install Chado v1.2'){
  76. $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.2.sql';
  77. $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/initialize-1.2.sql';
  78. if (tripal_core_reset_chado_schema()) {
  79. tripal_core_install_sql($schema_file);
  80. tripal_core_install_sql($init_file);
  81. db_query($vsql,'1.2'); # set the version
  82. }
  83. else {
  84. print "ERROR: cannot install chado. Please check database permissions\n";
  85. exit;
  86. }
  87. }
  88. elseif($action == 'Upgrade Chado v1.11 to v1.2') {
  89. $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.11-1.2-diff.sql';
  90. $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/upgrade-1.11-1.2.sql';
  91. tripal_core_install_sql($schema_file);
  92. tripal_core_install_sql($init_file);
  93. db_query($vsql,'1.2'); # set the version
  94. }
  95. elseif($action == 'Install Chado v1.11'){
  96. $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.11.sql';
  97. $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/initialize-1.11.sql';
  98. if (tripal_core_reset_chado_schema()) {
  99. tripal_core_install_sql($schema_file);
  100. tripal_core_install_sql($init_file);
  101. }
  102. else {
  103. print "ERROR: cannot install chado. Please check database permissions\n";
  104. exit;
  105. }
  106. }
  107. }
  108. /**
  109. * Reset the Chado Schema
  110. * This drops the current chado and chado-related schema and re-creates it
  111. *
  112. * @ingroup tripal_core
  113. */
  114. function tripal_core_reset_chado_schema() {
  115. global $active_db;
  116. // drop current chado and chado-related schema
  117. if (tripal_core_schema_exists('chado')) {
  118. print "Dropping existing 'chado' schema\n";
  119. pg_query($active_db, "drop schema chado cascade");
  120. }
  121. if (tripal_core_schema_exists('genetic_code')) {
  122. print "Dropping existing 'genetic_code' schema\n";
  123. pg_query($active_db, "drop schema genetic_code cascade");
  124. }
  125. if (tripal_core_schema_exists('so')) {
  126. print "Dropping existing 'so' schema\n";
  127. pg_query($active_db, "drop schema so cascade");
  128. }
  129. if (tripal_core_schema_exists('frange')) {
  130. print "Dropping existing 'frange' schema\n";
  131. pg_query($active_db, "drop schema frange cascade");
  132. }
  133. // create the new chado schema
  134. print "Creating 'chado' schema\n";
  135. pg_query($active_db, "create schema chado");
  136. if (tripal_core_schema_exists('chado')) {
  137. pg_query($active_db, "create language plpgsql");
  138. return TRUE;
  139. }
  140. return FALSE;
  141. }
  142. /**
  143. * Check that a given schema exists
  144. *
  145. * @param $schema
  146. * The name of the schema to check the existence of
  147. *
  148. * @return
  149. * TRUE/FALSE depending upon whether or not the schema exists
  150. *
  151. * @ingroup tripal_core
  152. */
  153. function tripal_core_schema_exists($schema) {
  154. // check that the chado schema now exists
  155. $sql = "SELECT nspname
  156. FROM pg_namespace
  157. WHERE has_schema_privilege(nspname, 'USAGE') and nspname = '%s'
  158. ORDER BY nspname";
  159. $name = db_fetch_object(db_query($sql, $schema));
  160. if (strcmp($name->nspname, $schema) != 0) {
  161. return FALSE;
  162. }
  163. return TRUE;
  164. }
  165. /**
  166. * Execute the provided SQL
  167. *
  168. * @param $sql_file
  169. * Contains SQL statements to be executed
  170. *
  171. * @ingroup tripal_core
  172. */
  173. function tripal_core_install_sql($sql_file) {
  174. global $active_db;
  175. pg_query($active_db, "set search_path to chado,public");
  176. print "Loading $sql_file...\n";
  177. $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
  178. if (!$lines) {
  179. return 'Cannot open $schema_file';
  180. }
  181. $stack = array();
  182. $in_string = 0;
  183. $query = '';
  184. $i = 0;
  185. foreach ($lines as $line_num => $line) {
  186. $i++;
  187. $type = '';
  188. // find and remove comments except when inside of strings
  189. if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) {
  190. $line = preg_replace('/--.*$/', '', $line); // remove comments
  191. }
  192. if (preg_match('/\/\*.*?\*\//', $line)) {
  193. $line = preg_replace('/\/\*.*?\*\//', '', $line); // remove comments
  194. }
  195. // skip empty lines
  196. if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) {
  197. continue;
  198. }
  199. // Find SQL for new objects
  200. if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string) {
  201. $stack[] = 'table';
  202. $line = preg_replace("/public./", "chado.", $line);
  203. }
  204. if (preg_match('/^\s*ALTER\s+TABLE/i', $line) and !$in_string) {
  205. $stack[] = 'alter table';
  206. $line = preg_replace("/public./", "chado.", $line);
  207. }
  208. if (preg_match('/^\s*SET/i', $line) and !$in_string) {
  209. $stack[] = 'set';
  210. }
  211. if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string) {
  212. $stack[] = 'schema';
  213. }
  214. if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string) {
  215. $stack[] = 'sequence';
  216. $line = preg_replace("/public./", "chado.", $line);
  217. }
  218. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string) {
  219. $stack[] = 'view';
  220. $line = preg_replace("/public./", "chado.", $line);
  221. }
  222. if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0) {
  223. $stack[] = 'comment';
  224. $line = preg_replace("/public./", "chado.", $line);
  225. }
  226. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string) {
  227. $stack[] = 'function';
  228. $line = preg_replace("/public./", "chado.", $line);
  229. }
  230. if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string) {
  231. $stack[] = 'index';
  232. }
  233. if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string) {
  234. $stack[] = 'insert';
  235. $line = preg_replace("/public./", "chado.", $line);
  236. }
  237. if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string) {
  238. $stack[] = 'type';
  239. }
  240. if (preg_match('/^\s*GRANT/i', $line) and !$in_string) {
  241. $stack[] = 'grant';
  242. }
  243. if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string) {
  244. $stack[] = 'aggregate';
  245. }
  246. // determine if we are in a string that spans a line
  247. $matches = preg_match_all("/[']/i", $line, $temp);
  248. $in_string = $in_string - ($matches % 2);
  249. $in_string = abs($in_string);
  250. // if we've reached the end of an object the pop the stack
  251. if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
  252. $type = array_pop($stack);
  253. }
  254. if (strcmp($stack[sizeof($stack)-1], 'alter table') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  255. $type = array_pop($stack);
  256. }
  257. if (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  258. $type = array_pop($stack);
  259. }
  260. if (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  261. $type = array_pop($stack);
  262. }
  263. if (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  264. $type = array_pop($stack);
  265. }
  266. if (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  267. $type = array_pop($stack);
  268. }
  269. if (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  270. $type = array_pop($stack);
  271. }
  272. if (strcmp($stack[sizeof($stack)-1], 'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i", $line)) {
  273. $type = array_pop($stack);
  274. }
  275. if (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  276. $type = array_pop($stack);
  277. }
  278. if (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
  279. $type = array_pop($stack);
  280. }
  281. if (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
  282. $type = array_pop($stack);
  283. }
  284. if (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  285. $type = array_pop($stack);
  286. }
  287. if (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
  288. $type = array_pop($stack);
  289. }
  290. // if we're in a recognized SQL statement then let's keep track of lines
  291. if ($type or sizeof($stack) > 0) {
  292. $query .= "$line";
  293. }
  294. else {
  295. print "UNHANDLED $i, $in_string: $line";
  296. return tripal_core_chado_install_done();
  297. }
  298. if (preg_match_all("/\n/", $query, $temp) > 100) {
  299. print "SQL query is too long. Terminating:\n$query\n";
  300. return tripal_core_chado_install_done();
  301. }
  302. if ($type and sizeof($stack) == 0) {
  303. print "Adding $type: line $i\n";
  304. // rewrite the set serach_path to make 'public' be 'chado'
  305. if (strcmp($type, 'set')==0) {
  306. $query = preg_replace("/public/m", "chado", $query);
  307. }
  308. $result = pg_query($active_db, $query);
  309. if (!$result) {
  310. $error = pg_last_error();
  311. print "Installation failed:\nSQL $i, $in_string: $query\n$error\n";
  312. pg_query($active_db, "set search_path to public,chado");
  313. return tripal_core_chado_install_done();
  314. }
  315. $query = '';
  316. }
  317. }
  318. print "Installation Complete!\n";
  319. tripal_core_chado_install_done();
  320. }
  321. /**
  322. * Finish the Chado Schema Installation
  323. *
  324. * @ingroup tripal_core
  325. */
  326. function tripal_core_chado_install_done() {
  327. // return the search path to normal
  328. global $active_db;
  329. pg_query($active_db, "set search_path to public,chado");
  330. }