2 // Copyright (C) 2008-2009 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');
21 function tableExists($tblname) {
22 $row = sqlQuery("SHOW TABLES LIKE '$tblname'");
23 if (empty($row)) return false;
27 function columnExists($tblname, $colname) {
28 $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
29 if (empty($row)) return false;
33 function columnHasType($tblname, $colname, $coltype) {
34 $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
35 if (empty($row)) return true;
36 return (strcasecmp($row['Type'], $coltype) == 0);
39 function tableHasRow($tblname, $colname, $value) {
40 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
41 "$colname LIKE '$value'");
42 return $row['count'] ?
true : false;
45 function upgradeFromSqlFile($filename) {
46 global $webserver_root;
49 echo "<font color='green'>Processing $filename ...</font><br />\n";
51 $fullname = "$webserver_root/sql/$filename";
53 $fd = fopen($fullname, 'r');
55 echo "ERROR. Could not open '$fullname'.\n";
65 $line = fgets($fd, 2048);
68 if (preg_match('/^\s*--/', $line)) continue;
69 if ($line == "") continue;
71 if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
72 $skipping = tableExists($matches[1]);
73 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
75 else if (preg_match('/^#IfMissingColumn\s+(\S+)\s+(\S+)/', $line, $matches)) {
76 if (tableExists($matches[1])) {
77 $skipping = columnExists($matches[1], $matches[2]);
80 // If no such table then the column is deemed not "missing".
83 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
85 else if (preg_match('/^#IfNotColumnType\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
86 if (tableExists($matches[1])) {
87 $skipping = columnHasType($matches[1], $matches[2], $matches[3]);
90 // If no such table then the column type is deemed not "missing".
93 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
95 else if (preg_match('/^#IfNotRow\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
96 if (tableExists($matches[1])) {
97 $skipping = tableHasRow($matches[1], $matches[2], $matches[3]);
100 // If no such table then the row is deemed not "missing".
103 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
105 else if (preg_match('/^#EndIf/', $line)) {
109 if (preg_match('/^\s*#/', $line)) continue;
110 if ($skipping) continue;
112 $query = $query . $line;
113 if (substr($query, -1) == ';') {
114 $query = rtrim($query, ';');
115 echo "$query<br />\n";
116 if (!sqlStatement($query)) {
117 echo "<font color='red'>The above statement failed: " .
118 mysql_error() . "<br />Upgrading will continue.<br /></font>\n";
127 $sqldir = "$webserver_root/sql";
128 $dh = opendir($sqldir);
129 if (! $dh) die("Cannot read $sqldir");
130 while (false !== ($sfname = readdir($dh))) {
131 if (substr($sfname, 0, 1) == '.') continue;
132 if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
133 $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
134 $versions[$version] = $sfname;
142 <title
>OpenEMR Database Upgrade
</title
>
143 <link rel
='STYLESHEET' href
='interface/themes/style_blue.css'>
147 <span
class='title'>OpenEMR Database Upgrade
</span
>
151 if (!empty($_POST['form_submit'])) {
152 $form_old_version = $_POST['form_old_version'];
154 foreach ($versions as $version => $filename) {
155 if (strcmp($version, $form_old_version) < 0) continue;
156 upgradeFromSqlFile($filename);
159 if (!empty($GLOBALS['ippf_specific'])) {
160 // Reload custom lists, layouts and codes for IPPF.
161 upgradeFromSqlFile('ippf_layout.sql');
164 echo "<p><font color='green'>Database upgrade finished.</font></p>\n";
165 echo "</body></html>\n";
171 <form method
='post' action
='sql_upgrade.php'>
172 <p
>Please select the prior release you are converting from
:
173 <select name
='form_old_version'>
175 foreach ($versions as $version => $filename) {
176 echo " <option value='$version'";
177 // Defaulting to 2.8.3 only because it was out there for a long time,
178 // and nothing should go wrong if that's too old.
179 if ($version === '2.8.3') echo " selected";
180 echo ">$version</option>\n";
185 <p
>If you are unsure
or were using a development version between two
186 releases
, then choose the older of possible releases
.</p
>
187 <p
><input type
='submit' name
='form_submit' value
='Upgrade Database' /></p
>