2 // Copyright (C) 2008-2010 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This may be run after an upgraded OpenEMR has been installed.
10 // Its purpose is to upgrade the MySQL OpenEMR database as needed
11 // for the new release.
13 // Disable PHP timeout. This will not work in safe mode.
14 ini_set('max_execution_time', '0');
16 $ignoreAuth = true; // no login required
18 require_once('interface/globals.php');
19 require_once('library/sql.inc');
22 $GLOBALS["enable_auditlog"]=0;
24 function tableExists($tblname) {
25 $row = sqlQuery("SHOW TABLES LIKE '$tblname'");
26 if (empty($row)) return false;
30 function columnExists($tblname, $colname) {
31 $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
32 if (empty($row)) return false;
36 function columnHasType($tblname, $colname, $coltype) {
37 $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
38 if (empty($row)) return true;
39 return (strcasecmp($row['Type'], $coltype) == 0);
42 function tableHasRow($tblname, $colname, $value) {
43 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
44 "$colname LIKE '$value'");
45 return $row['count'] ?
true : false;
48 function tableHasRow2D($tblname, $colname, $value, $colname2, $value2) {
49 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
50 "$colname LIKE '$value' AND $colname2 LIKE '$value2'");
51 return $row['count'] ?
true : false;
54 function upgradeFromSqlFile($filename) {
55 global $webserver_root;
58 echo "<font color='green'>Processing $filename ...</font><br />\n";
60 $fullname = "$webserver_root/sql/$filename";
62 $fd = fopen($fullname, 'r');
64 echo "ERROR. Could not open '$fullname'.\n";
74 $line = fgets($fd, 2048);
77 if (preg_match('/^\s*--/', $line)) continue;
78 if ($line == "") continue;
80 if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
81 $skipping = tableExists($matches[1]);
82 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
84 else if (preg_match('/^#IfTable\s+(\S+)/', $line, $matches)) {
85 $skipping = ! tableExists($matches[1]);
86 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
88 else if (preg_match('/^#IfMissingColumn\s+(\S+)\s+(\S+)/', $line, $matches)) {
89 if (tableExists($matches[1])) {
90 $skipping = columnExists($matches[1], $matches[2]);
93 // If no such table then the column is deemed not "missing".
96 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
98 else if (preg_match('/^#IfNotColumnType\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
99 if (tableExists($matches[1])) {
100 $skipping = columnHasType($matches[1], $matches[2], $matches[3]);
103 // If no such table then the column type is deemed not "missing".
106 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
108 else if (preg_match('/^#IfNotRow\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
109 if (tableExists($matches[1])) {
110 $skipping = tableHasRow($matches[1], $matches[2], $matches[3]);
113 // If no such table then the row is deemed not "missing".
116 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
118 else if (preg_match('/^#IfNotRow2D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
119 if (tableExists($matches[1])) {
120 $skipping = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
123 // If no such table then the row is deemed not "missing".
126 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
128 else if (preg_match('/^#IfNotRow2Dx2\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
129 if (tableExists($matches[1])) {
130 // If either check exist, then will skip
131 $firstCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
132 $secondCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[6], $matches[7]);
133 if ($firstCheck ||
$secondCheck) {
141 // If no such table then the row is deemed not "missing".
144 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
146 else if (preg_match('/^#EndIf/', $line)) {
150 if (preg_match('/^\s*#/', $line)) continue;
151 if ($skipping) continue;
153 $query = $query . $line;
154 if (substr($query, -1) == ';') {
155 $query = rtrim($query, ';');
156 echo "$query<br />\n";
157 if (!sqlStatement($query)) {
158 echo "<font color='red'>The above statement failed: " .
159 mysql_error() . "<br />Upgrading will continue.<br /></font>\n";
168 $sqldir = "$webserver_root/sql";
169 $dh = opendir($sqldir);
170 if (! $dh) die("Cannot read $sqldir");
171 while (false !== ($sfname = readdir($dh))) {
172 if (substr($sfname, 0, 1) == '.') continue;
173 if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
174 $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
175 $versions[$version] = $sfname;
183 <title
>OpenEMR Database Upgrade
</title
>
184 <link rel
='STYLESHEET' href
='interface/themes/style_blue.css'>
188 <span
class='title'>OpenEMR Database Upgrade
</span
>
192 if (!empty($_POST['form_submit'])) {
193 $form_old_version = $_POST['form_old_version'];
195 foreach ($versions as $version => $filename) {
196 if (strcmp($version, $form_old_version) < 0) continue;
197 upgradeFromSqlFile($filename);
200 if (!empty($GLOBALS['ippf_specific'])) {
201 // Upgrade custom stuff for IPPF.
202 upgradeFromSqlFile('ippf_upgrade.sql');
206 echo "<font color='green'>Updating global configuration defaults...</font><br />\n";
207 require_once("library/globals.inc.php");
208 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
209 foreach ($grparr as $fldid => $fldarr) {
210 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
211 if (substr($fldtype, 0, 2) !== 'm_') {
212 $row = sqlQuery("SELECT count(*) AS count FROM globals WHERE gl_name = '$fldid'");
213 if (empty($row['count'])) {
214 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
215 "VALUES ( '$fldid', '0', '$flddef' )");
221 echo "<font color='green'>Updating version indicators...</font><br />\n";
222 sqlStatement("UPDATE version SET v_major = '$v_major', v_minor = '$v_minor', " .
223 "v_patch = '$v_patch', v_tag = '$v_tag', v_database = '$v_database'");
225 echo "<p><font color='green'>Database upgrade finished.</font></p>\n";
226 echo "</body></html>\n";
232 <form method
='post' action
='sql_upgrade.php'>
233 <p
>Please select the prior release you are converting from
:
234 <select name
='form_old_version'>
236 foreach ($versions as $version => $filename) {
237 echo " <option value='$version'";
238 // Defaulting to most recent version, which is now 3.2.0.
239 if ($version === '3.2.0') echo " selected";
240 echo ">$version</option>\n";
245 <p
>If you are unsure
or were using a development version between two
246 releases
, then choose the older of possible releases
.</p
>
247 <p
><input type
='submit' name
='form_submit' value
='Upgrade Database' /></p
>