corrected link from time to create a new event on the currently selected date
[openemr.git] / sql_upgrade.php
blob1d578e1b0f93874593ebc2cada0b0e5e9c49b997
1 <?php
2 // Copyright (C) 2008 Rod Roark <rod@sunsetsystems.com>
3 //
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.
8 //
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;
24 return true;
27 function columnExists($tblname, $colname) {
28 $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
29 if (empty($row)) return false;
30 return true;
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 $versions = array();
46 $sqldir = "$webserver_root/sql";
47 $dh = opendir($sqldir);
48 if (! $dh) die("Cannot read $sqldir");
49 while (false !== ($sfname = readdir($dh))) {
50 if (substr($sfname, 0, 1) == '.') continue;
51 if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
52 $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
53 $versions[$version] = $sfname;
56 closedir($dh);
57 ksort($versions);
59 <html>
60 <head>
61 <title>OpenEMR Database Upgrade</title>
62 <link rel='STYLESHEET' href='interface/themes/style_blue.css'>
63 </head>
64 <body>
65 <center>
66 <span class='title'>OpenEMR Database Upgrade</span>
67 <br>
68 </center>
69 <?php
70 if (!empty($_POST['form_submit'])) {
71 $form_old_version = $_POST['form_old_version'];
73 foreach ($versions as $version => $filename) {
74 if (strcmp($version, $form_old_version) < 0) continue;
76 echo "<font color='green'>Processing $filename ...</font><br />\n";
78 flush();
79 $fullname = "$webserver_root/sql/$filename";
80 $fd = fopen($fullname, 'r');
81 if ($fd == FALSE) {
82 echo "ERROR. Could not open '$fullname'.\n";
83 flush();
84 break;
87 $query = "";
88 $line = "";
89 $skipping = false;
91 while (!feof ($fd)){
92 $line = fgets($fd, 2048);
93 $line = rtrim($line);
95 if (preg_match('/^\s*--/', $line)) continue;
96 if ($line == "") continue;
98 if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
99 $skipping = tableExists($matches[1]);
100 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
102 else if (preg_match('/^#IfMissingColumn\s+(\S+)\s+(\S+)/', $line, $matches)) {
103 if (tableExists($matches[1])) {
104 $skipping = columnExists($matches[1], $matches[2]);
106 else {
107 // If no such table then the column is deemed not "missing".
108 $skipping = true;
110 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
112 else if (preg_match('/^#IfNotColumnType\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
113 if (tableExists($matches[1])) {
114 $skipping = columnHasType($matches[1], $matches[2], $matches[3]);
116 else {
117 // If no such table then the column type is deemed not "missing".
118 $skipping = true;
120 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
122 else if (preg_match('/^#IfNotRow\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
123 if (tableExists($matches[1])) {
124 $skipping = tableHasRow($matches[1], $matches[2], $matches[3]);
126 else {
127 // If no such table then the row is deemed not "missing".
128 $skipping = true;
130 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
132 else if (preg_match('/^#EndIf/', $line)) {
133 $skipping = false;
136 if (preg_match('/^\s*#/', $line)) continue;
137 if ($skipping) continue;
139 $query = $query . $line;
140 if (substr($query, -1) == ';') {
141 $query = rtrim($query, ';');
142 echo "$query<br />\n";
143 if (!sqlStatement($query)) {
144 echo "<font color='red'>The above statement failed: " .
145 mysql_error() . "<br />Upgrading will continue.<br /></font>\n";
147 $query = '';
150 flush();
153 echo "<p><font color='green'>Database upgrade finished.</font></p>\n";
154 echo "</body></html>\n";
155 exit();
159 <center>
160 <form method='post' action='sql_upgrade.php'>
161 <p>Please select the prior release you are converting from:
162 <select name='form_old_version'>
163 <?php
164 foreach ($versions as $version => $filename) {
165 echo " <option value='$version'>$version</option>\n";
168 </select>
169 </p>
170 <p>If you were using a development version between two releases,
171 then choose the older of those releases.</p>
172 <p><input type='submit' name='form_submit' value='Upgrade Database' /></p>
173 </form>
174 </center>
175 </body>
176 </html>