Tripal v1.0 (6.x-1.0)
|
00001 <?php 00002 00013 function tripal_core_chado_load_form() { 00014 00015 // we want to force the version of Chado to be set properly 00016 $real_version = tripal_core_set_chado_version(); 00017 00018 // get the effective version. Pass true as second argument 00019 // to warn the user if the current version is not compatible 00020 $version = tripal_core_get_chado_version(FALSE, TRUE); 00021 00022 $form['current_version'] = array( 00023 '#type' => 'item', 00024 '#title' => t("Current installed version of Chado"), 00025 '#value' => $real_version, 00026 ); 00027 00028 $form['action_to_do'] = array( 00029 '#type' => 'radios', 00030 '#title' => 'Installation/Upgrade Action', 00031 '#options' => array( 00032 'Install Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'), 00033 'Upgrade Chado v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'), 00034 'Install Chado v1.11' => t('New Install of Chado v1.11 (erases all existing Chado data if Chado already exists)') 00035 ), 00036 '#description' => t('Select an action to perform'), 00037 '#required' => TRUE 00038 ); 00039 00040 $form['description'] = array( 00041 '#type' => 'item', 00042 '#value' => t("<font color=\"red\">WARNING:</font> A new install of Chado v1.2 or v1.11 " 00043 ."will install Chado within the Drupal database in a \"chado\" schema. If the \"chado\" schema already exists it will " 00044 ."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 " 00045 ."version of Tripal (e.g. v0.3b or v0.3.1). The update will not erase any data. " 00046 ."If you are using chado in a database external to the " 00047 ."Drupal database with a 'chado' entry in the 'settings.php' \$db_url argument " 00048 ."then Chado will be installed but will not be used . The external " 00049 ."database specified in the settings.php file takes precedence."), 00050 ); 00051 00052 $form['button'] = array( 00053 '#type' => 'submit', 00054 '#value' => t('Install/Upgrade Chado'), 00055 '#weight' => 2, 00056 ); 00057 00058 return $form; 00059 } 00060 00066 function tripal_core_chado_load_form_submit($form, &$form_state) { 00067 global $user; 00068 $action_to_do = trim($form_state['values']['action_to_do']); 00069 00070 $args = array($action_to_do); 00071 tripal_add_job($action_to_do, 'tripal_core', 00072 'tripal_core_install_chado', $args, $user->uid); 00073 } 00074 00080 function tripal_core_install_chado($action) { 00081 00082 $vsql = "INSERT INTO chadoprop (type_id, value) VALUES " . 00083 "((SELECT cvterm_id " . 00084 "FROM cvterm CVT " . 00085 " INNER JOIN cv CV on CVT.cv_id = CV.cv_id " . 00086 "WHERE CV.name = 'chado_properties' AND CVT.name = 'version'), " . 00087 "'%s') "; 00088 00089 if($action == 'Install Chado v1.2'){ 00090 $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.2.sql'; 00091 $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/initialize-1.2.sql'; 00092 if (tripal_core_reset_chado_schema()) { 00093 $success = tripal_core_install_sql($schema_file); 00094 if ($success) { 00095 print "Install of Chado v1.2 (Step 1 of 2) Successful!\n"; 00096 } 00097 else { 00098 print "Installation (Step 1 of 2) Problems! Please check output above for errors.\n"; 00099 exit; 00100 } 00101 $success = tripal_core_install_sql($init_file); 00102 if ($success) { 00103 print "Install of Chado v1.2 (Step 2 of 2) Successful.\nInstallation Complete\n"; 00104 } 00105 else { 00106 print "Installation (Step 2 of 2) Problems! Please check output above for errors.\n"; 00107 exit; 00108 } 00109 chado_query($vsql,'1.2'); # set the version 00110 } 00111 else { 00112 print "ERROR: cannot install chado. Please check database permissions\n"; 00113 exit; 00114 } 00115 } 00116 elseif($action == 'Upgrade Chado v1.11 to v1.2') { 00117 $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.11-1.2-diff.sql'; 00118 $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/upgrade-1.11-1.2.sql'; 00119 $success = tripal_core_install_sql($schema_file); 00120 if ($success) { 00121 print "Upgrade from v1.11 to v1.2 (Step 1 of 2) Successful!\n"; 00122 } 00123 else { 00124 print "Upgrade (Step 1 of 2) problems! Please check output above for errors.\n"; 00125 exit; 00126 } 00127 $success = tripal_core_install_sql($init_file); 00128 if ($success) { 00129 print "Upgrade from v1.11 to v1.2 (Step 2 of 2) Successful.\nUpgrade Complete!\n"; 00130 } 00131 else { 00132 print "Upgrade (Step 2 of 2) problems! Please check output above for errors.\n"; 00133 exit; 00134 } 00135 chado_query($vsql,'1.2'); # set the version 00136 } 00137 elseif($action == 'Install Chado v1.11'){ 00138 $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.11.sql'; 00139 $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/initialize-1.11.sql'; 00140 if (tripal_core_reset_chado_schema()) { 00141 $success = tripal_core_install_sql($schema_file); 00142 if ($success) { 00143 print "Install of Chado v1.11 (Step 1 of 2) Successful!\n"; 00144 } 00145 else { 00146 print "Installation (Step 1 of 2) Problems! Please check output above for errors.\n"; 00147 exit; 00148 } 00149 $success = tripal_core_install_sql($init_file); 00150 if ($success) { 00151 print "Install of Chado v1.11 (Step 2 of 2) Successful.\nInstallation Complete!\n"; 00152 } 00153 else { 00154 print "Installation (Step 2 of 2) Problems! Please check output above for errors.\n"; 00155 exit; 00156 } 00157 } 00158 else { 00159 print "ERROR: cannot install chado. Please check database permissions\n"; 00160 exit; 00161 } 00162 } 00163 } 00164 00171 function tripal_core_reset_chado_schema() { 00172 global $active_db; 00173 00174 // drop current chado and chado-related schema 00175 if (tripal_core_schema_exists('chado')) { 00176 print "Dropping existing 'chado' schema\n"; 00177 db_query("drop schema chado cascade"); 00178 } 00179 if (tripal_core_schema_exists('genetic_code')) { 00180 print "Dropping existing 'genetic_code' schema\n"; 00181 db_query("drop schema genetic_code cascade"); 00182 } 00183 if (tripal_core_schema_exists('so')) { 00184 print "Dropping existing 'so' schema\n"; 00185 db_query("drop schema so cascade"); 00186 } 00187 if (tripal_core_schema_exists('frange')) { 00188 print "Dropping existing 'frange' schema\n"; 00189 db_query("drop schema frange cascade"); 00190 } 00191 00192 // create the new chado schema 00193 print "Creating 'chado' schema\n"; 00194 db_query("create schema chado"); 00195 if (tripal_core_schema_exists('chado')) { 00196 // before creating the plpgsql language let's check to make sure 00197 // it doesn't already exists 00198 $sql = "SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'"; 00199 $count = db_fetch_object(db_query($sql)); 00200 if (!$count or $count->count == 0) { 00201 db_query("create language plpgsql"); 00202 } 00203 return TRUE; 00204 } 00205 00206 return FALSE; 00207 } 00208 00217 function tripal_core_install_sql($sql_file) { 00218 00219 $chado_local = tripal_core_schema_exists('chado'); 00220 00221 if($chado_local) { 00222 db_query("set search_path to chado"); 00223 } 00224 print "Loading $sql_file...\n"; 00225 $lines = file($sql_file, FILE_SKIP_EMPTY_LINES); 00226 00227 if (!$lines) { 00228 return 'Cannot open $schema_file'; 00229 } 00230 00231 $stack = array(); 00232 $in_string = 0; 00233 $query = ''; 00234 $i = 0; 00235 $success = 1; 00236 foreach ($lines as $line_num => $line) { 00237 $i++; 00238 $type = ''; 00239 // find and remove comments except when inside of strings 00240 if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) { 00241 $line = preg_replace('/--.*$/', '', $line); // remove comments 00242 } 00243 if (preg_match('/\/\*.*?\*\//', $line)) { 00244 $line = preg_replace('/\/\*.*?\*\//', '', $line); // remove comments 00245 } 00246 // skip empty lines 00247 if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) { 00248 continue; 00249 } 00250 // Find SQL for new objects 00251 if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string) { 00252 $stack[] = 'table'; 00253 $line = preg_replace("/public./", "chado.", $line); 00254 } 00255 if (preg_match('/^\s*ALTER\s+TABLE/i', $line) and !$in_string) { 00256 $stack[] = 'alter table'; 00257 $line = preg_replace("/public./", "chado.", $line); 00258 } 00259 if (preg_match('/^\s*SET/i', $line) and !$in_string) { 00260 $stack[] = 'set'; 00261 } 00262 if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string) { 00263 $stack[] = 'schema'; 00264 } 00265 if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string) { 00266 $stack[] = 'sequence'; 00267 $line = preg_replace("/public./", "chado.", $line); 00268 } 00269 if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string) { 00270 $stack[] = 'view'; 00271 $line = preg_replace("/public./", "chado.", $line); 00272 } 00273 if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0) { 00274 $stack[] = 'comment'; 00275 $line = preg_replace("/public./", "chado.", $line); 00276 } 00277 if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string) { 00278 $stack[] = 'function'; 00279 $line = preg_replace("/public./", "chado.", $line); 00280 } 00281 if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string) { 00282 $stack[] = 'index'; 00283 } 00284 if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string) { 00285 $stack[] = 'insert'; 00286 $line = preg_replace("/public./", "chado.", $line); 00287 } 00288 if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string) { 00289 $stack[] = 'type'; 00290 } 00291 if (preg_match('/^\s*GRANT/i', $line) and !$in_string) { 00292 $stack[] = 'grant'; 00293 } 00294 if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string) { 00295 $stack[] = 'aggregate'; 00296 } 00297 00298 // determine if we are in a string that spans a line 00299 $matches = preg_match_all("/[']/i", $line, $temp); 00300 $in_string = $in_string - ($matches % 2); 00301 $in_string = abs($in_string); 00302 00303 // if we've reached the end of an object the pop the stack 00304 if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) { 00305 $type = array_pop($stack); 00306 } 00307 if (strcmp($stack[sizeof($stack)-1], 'alter table') == 0 and preg_match('/;\s*$/', $line) and !$in_string) { 00308 $type = array_pop($stack); 00309 } 00310 if (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) { 00311 $type = array_pop($stack); 00312 } 00313 if (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) { 00314 $type = array_pop($stack); 00315 } 00316 if (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) { 00317 $type = array_pop($stack); 00318 } 00319 if (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) { 00320 $type = array_pop($stack); 00321 } 00322 if (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) { 00323 $type = array_pop($stack); 00324 } 00325 if (strcmp($stack[sizeof($stack)-1], 'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i", $line)) { 00326 $type = array_pop($stack); 00327 } 00328 if (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) { 00329 $type = array_pop($stack); 00330 } 00331 if (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) { 00332 $type = array_pop($stack); 00333 } 00334 if (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) { 00335 $type = array_pop($stack); 00336 } 00337 if (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) { 00338 $type = array_pop($stack); 00339 } 00340 if (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) { 00341 $type = array_pop($stack); 00342 } 00343 // if we're in a recognized SQL statement then let's keep track of lines 00344 if ($type or sizeof($stack) > 0) { 00345 $query .= "$line"; 00346 } 00347 else { 00348 print "UNHANDLED $i, $in_string: $line"; 00349 tripal_core_chado_install_done(); 00350 return FALSE; 00351 } 00352 if (preg_match_all("/\n/", $query, $temp) > 100) { 00353 print "SQL query is too long. Terminating:\n$query\n"; 00354 tripal_core_chado_install_done(); 00355 return FALSE; 00356 } 00357 if ($type and sizeof($stack) == 0) { 00358 //print "Adding $type: line $i\n"; 00359 // rewrite the set search_path to make 'public' be 'chado', but only if the 00360 // chado schema exists 00361 if (strcmp($type, 'set')==0 and $chado_local){ 00362 $query = preg_replace("/public/m", "chado", $query); 00363 } 00364 00365 if (!$chado_local) { 00366 $previous = tripal_db_set_active('chado'); 00367 } 00368 $result = db_query($query); 00369 if (!$chado_local) { 00370 tripal_db_set_active($previous); 00371 } 00372 if (!$result) { 00373 $error = pg_last_error(); 00374 print "FAILED. Line $i, $in_string\n$error:\n$query\n\n"; 00375 tripal_core_chado_install_done(); 00376 $success = 0; 00377 return $success; 00378 } 00379 $query = ''; 00380 } 00381 } 00382 tripal_core_chado_install_done(); 00383 return $success; 00384 } 00385 00391 function tripal_core_chado_install_done() { 00392 00393 // return the search path to normal 00394 db_query("set search_path to public"); 00395 }