4 // upgrade-db.php => Upgrade the main database.
5 // upgrade-db.php all => Upgrade the main database and active plugins.
6 // upgrade-db.php <plugin> => Upgrade only the database of the given active plugin.
7 echo "Entering upgrade-db.php\n";
9 require_once dirname(__FILE__).'/../common/include/env.inc.php';
10 require_once $gfcommon.'include/pre.php';
11 require_once $gfcommon.'include/sqlparser.php';
13 $db_path = dirname(__FILE__).'/';
17 // a huge message will warn not to run it without doing a backup first // warning.
19 // Check if table 'database_startpoint' exists
20 if (!db_check_table_exists('database_startpoint')) {
21 show("ERROR: table 'database_startpoint' does not exist.\nRun startpoint.php first.\n");
25 // Check if table 'database_startpoint' has proper values
26 $res = db_query_params('SELECT * FROM database_startpoint',
29 if (!$res) { // db error
30 show("DB-ERROR-3: ".db_error()."\n");
32 } else if (db_numrows($res) == 0) { // table 'database_startpoint' is empty
33 show("ERROR: table 'database_startpoint' is empty.\nRun startpoint.php first.\n");
35 } else { // get the start date from the db
36 $date = (int) db_result($res, 0, 'db_start_date');
37 $version = db_result($res, 0, 'db_version');
40 if (!apply_fixes($version)) {
41 show("ERROR applying fixes to version $version!\n");
45 // Upgrade main database if no argument or if all)
46 if ($argc == 1 || $argv[1] == 'all') {
47 $scripts = get_scripts($db_path);
48 foreach ($scripts as $script) {
49 if ((int) $script['date'] > $date) {
50 $res = db_query_params('SELECT * FROM database_changes WHERE filename=$1',
51 array ("{$script['filename']}"));
54 show("ERROR-2: ".db_error()."\n");
56 } else if (db_numrows($res) == 0) {
57 show("Running script: {$script['filename']}\n");
59 $result = run_script($script);
61 $res = db_query_params ('INSERT INTO database_changes (filename) VALUES ($1)',
62 array ("{$script['filename']}")) ;
65 show("ERROR-3: ".db_error()."\n");
75 // show("Skipping script: {$script['filename']}\n");
81 // Upgrade activated plugins.
83 require_once $gfcommon.'include/DatabaseInstaller.class.php';
84 $plugins = get_installed_plugins();
85 foreach ($plugins as $plugin) {
86 if ($argv[1] == 'all' || $argv[1] == $plugin) {
87 $di = new DatabaseInstaller($plugin, dirname($db_path) . '/plugins/' . $plugin . '/db');
93 function get_installed_plugins() {
95 $res = db_query_params ('SELECT plugin_name FROM plugins', array ());
96 while ($row = db_fetch_array($res)) {
97 $plugins[] = $row['plugin_name'];
102 function get_scripts($dir) {
105 if ($dh = opendir($dir)) {
106 while (($file = readdir($dh)) !== false) {
107 $pos = strrpos($file, '.');
108 if ($pos !== false && $pos > 0) {
109 $name = substr($file, 0, $pos);
110 if (strlen($name) >= 8) {
111 $date_aux = substr($name, 0, 8);
112 $type_aux = substr($file, $pos + 1);
113 if ((int) $date_aux > 20000000 && ($type_aux=='sql' || $type_aux=='php') && strpos($file, 'debian') === false) {
114 $data[] = array('date'=>$date_aux, 'filename'=>$file, 'ext'=>$type_aux);
121 usort($data, 'compare_scripts');
128 function compare_scripts($script1, $script2) {
129 return strcmp($script1['filename'], $script2['filename']);
132 function run_script($script) {
136 $ext = strtolower($script['ext']);
137 $filename = $script['filename'];
139 // run the php script
141 $exec = 'php -f '.$db_path.$filename;
142 exec($exec, $result);
144 if (count($result)) { // the script produced an output
145 if ($result[count($result)-1] == 'SUCCESS') {
146 show($db_path.$filename." ran correctly\n\n");
149 show($db_path.$filename." FAILED!\n\n");
150 show("Script output follows:\n");
151 foreach ($result as $line) {
154 show("[End of script output]\n");
157 show($db_path.$filename." FAILED!\n\n");
160 } else if ($ext == 'sql') {
161 if (//$filename == '20021124-3_gforge-debian-sf-sync.sql' ||
162 $filename == '20021223-drops.sql') {
164 echo "\nskipping $filename";
167 // run the sql script
169 if (run_sql_script($filename)) {
170 show($db_path.$filename." ran correctly\n\n");
173 show($db_path.$filename." FAILED!\n\n");
177 // something went wrong
178 show("\nThe script is not a PHP file nor an SQL file. Something went wrong. Please report this bug\n");
183 function run_sql_script($filename) {
186 $sql_file = $db_path.$filename;
187 $file = @fopen($sql_file, 'rb');
191 $queries = parse_sql_file($db_path.$filename);
197 foreach ($queries as $query) {
198 $res = db_query_params($query, array());
200 show(db_error()."\n");
201 show("QUERY: $query\n");
202 show("Continue executing ([Y]es/[N]o)?\n");
204 $answer = strtolower(trim(fgets(STDIN)));
205 if ($answer != 'y' && $answer != 'yes') {
215 // Patch for some 3.0preX versions
216 if ($filename == '20021216.sql') {
217 db_query_params ('SELECT setval($1, (SELECT MAX(theme_id) FROM themes), true)',
218 array('themes_theme_id_key')) ;
220 show("Applying fix for some 3.0preX versions\n");
227 function apply_fixes($version) {
229 if ($version == 'sfee3.3') {
230 $res = db_query_params('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1', array ('sfee3.3fixes')) ;
231 if ($res && db_result($res, 0, 'applied') == '0') {
232 show("Converting SFEE3.3 to SFEE3.0\n");
233 run_script(array('filename'=>'sfee3.3-3.0-1.sql','ext'=>'sql'));
234 run_script(array('filename'=>'sfee3.3-3.0-2.php','ext'=>'php'));
235 run_script(array('filename'=>'sfee3.3-3.0-3.sql','ext'=>'sql'));
236 show("Converting SFEE3.0 to SF2.6\n");
238 run_script(array('filename'=>'sfee3.0-sf26-1.sql','ext'=>'sql'));
239 run_script(array('filename'=>'sfee3.0-sf26-2.php','ext'=>'php'));
240 $queries[] = "INSERT INTO database_changes (filename) VALUES ('sfee3.3fixes')";
242 } else if ($version == 'sfee3.0') {
243 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
244 array ('sfee3.0fixes')) ;
245 if ($res && db_result($res, 0, 'applied') == '0') {
246 run_script(array('filename'=>'sfee3.0-sf26-1.sql','ext'=>'sql'));
247 run_script(array('filename'=>'sfee3.0-sf26-2.php','ext'=>'php'));
248 $queries[] = "INSERT INTO database_changes (filename) VALUES ('sfee3.0fixes')";
250 } else if ($version == '2.5') {
251 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
253 if ($res && db_result($res, 0, 'applied') == '0') {
254 show("Applying fixes for version 2.5\n");
255 $queries[] = "ALTER TABLE project_task ADD CONSTRAINT project_task_group_project_id_f CHECK (1 = 1)";
256 $queries[] = "INSERT INTO database_changes (filename) VALUES ('2.5fixes')";
258 } else if ($version == '2.6') {
259 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
260 array ('2.6fixes')) ;
261 if ($res && db_result($res, 0, 'applied') == '0') {
262 show("Applying fixes for version 2.6\n");
263 $queries[] = "ALTER TABLE project_task ADD CONSTRAINT project_task_group_project_id_f CHECK (1 = 1)";
264 $queries[] = "INSERT INTO database_changes (filename) VALUES ('2.6fixes')";
266 } else if ($version == '3.0pre5') {
267 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
268 array ('3.0pre5fixes')) ;
269 if ($res && db_result($res, 0, 'applied') == '0') {
270 show("Applying fixes for version 3.0pre5\n");
271 if (!run_sql_script('fix-gforge3.0pre5.sql')) {
272 show("Error applying fixes for version 3.0pre5\n");
275 $queries[] = "INSERT INTO database_changes (filename) VALUES ('3.0pre5fixes')";
277 } else if ($version == '3.0pre6') {
278 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1', array ('3.0pre6fixes')) ;
279 if ($res && db_result($res, 0, 'applied') == '0') {
280 show("Applying fixes for version 3.0pre6\n");
281 $queries[] = "ALTER TABLE project_task ADD CONSTRAINT project_task_group_project_id_f CHECK (1 = 1)";
282 $queries[] = "INSERT INTO database_changes (filename) VALUES ('3.0pre6fixes')";
284 } else if ($version == '3.0pre7') {
285 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
286 array ('3.0pre7fixes')) ;
287 if ($res && db_result($res, 0, 'applied') == '0') {
288 show("Applying fixes for version 3.0pre7\n");
289 $queries[] = "ALTER TABLE project_task ADD CONSTRAINT project_task_group_project_id_f CHECK (1 = 1)";
290 $queries[] = "INSERT INTO database_changes (filename) VALUES ('3.0pre7fixes')";
292 } else if ($version == '4.7') {
293 run_script(array('filename'=>'20070924-project-perm.sql','ext'=>'sql'));
294 run_script(array('filename'=>'20070924-forum-perm.sql','ext'=>'sql'));
295 run_script(array('filename'=>'20070924-artifact-perm.sql','ext'=>'sql'));
299 foreach ($queries as $query) {
300 $res = db_query_params($query, array());
302 show("ERROR: ".db_error()."\n");
311 function in_string($haystack, $needle, $case_sensitive = false) {
312 if (!$case_sensitive) {
313 $haystack = strtolower($haystack);
315 if (strpos($haystack, $needle) !== false) {
322 function show($text) {
324 fwrite(STDOUT, $text);
329 // c-file-style: "bsd"