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 foreach ($result as $line) {
155 show($db_path.$filename." FAILED!\n\n");
158 } else if ($ext == 'sql') {
159 if (//$filename == '20021124-3_gforge-debian-sf-sync.sql' ||
160 $filename == '20021223-drops.sql') {
162 echo "\nskipping $filename";
165 // run the sql script
167 if (run_sql_script($filename)) {
168 show($db_path.$filename." ran correctly\n\n");
171 show($db_path.$filename." FAILED!\n\n");
175 // something went wrong
176 show("\nThe script is not a PHP file nor an SQL file. Something went wrong. Please report this bug\n");
181 function run_sql_script($filename) {
184 $sql_file = $db_path.$filename;
185 $file = @fopen($sql_file, 'rb');
189 $queries = parse_sql_file($db_path.$filename);
195 foreach ($queries as $query) {
196 $res = db_query_params($query, array());
198 show(db_error()."\n");
199 show("QUERY: $query\n");
200 show("Continue executing ([Y]es/[N]o)?\n");
202 $answer = strtolower(trim(fgets(STDIN)));
203 if ($answer != 'y' && $answer != 'yes') {
213 // Patch for some 3.0preX versions
214 if ($filename == '20021216.sql') {
215 db_query_params ('SELECT setval($1, (SELECT MAX(theme_id) FROM themes), true)',
216 array('themes_theme_id_key')) ;
218 show("Applying fix for some 3.0preX versions\n");
225 function apply_fixes($version) {
227 if ($version == 'sfee3.3') {
228 $res = db_query_params('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1', array ('sfee3.3fixes')) ;
229 if ($res && db_result($res, 0, 'applied') == '0') {
230 show("Converting SFEE3.3 to SFEE3.0\n");
231 run_script(array('filename'=>'sfee3.3-3.0-1.sql','ext'=>'sql'));
232 run_script(array('filename'=>'sfee3.3-3.0-2.php','ext'=>'php'));
233 run_script(array('filename'=>'sfee3.3-3.0-3.sql','ext'=>'sql'));
234 show("Converting SFEE3.0 to SF2.6\n");
236 run_script(array('filename'=>'sfee3.0-sf26-1.sql','ext'=>'sql'));
237 run_script(array('filename'=>'sfee3.0-sf26-2.php','ext'=>'php'));
238 $queries[] = "INSERT INTO database_changes (filename) VALUES ('sfee3.3fixes')";
240 } else if ($version == 'sfee3.0') {
241 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
242 array ('sfee3.0fixes')) ;
243 if ($res && db_result($res, 0, 'applied') == '0') {
244 run_script(array('filename'=>'sfee3.0-sf26-1.sql','ext'=>'sql'));
245 run_script(array('filename'=>'sfee3.0-sf26-2.php','ext'=>'php'));
246 $queries[] = "INSERT INTO database_changes (filename) VALUES ('sfee3.0fixes')";
248 } else if ($version == '2.5') {
249 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
251 if ($res && db_result($res, 0, 'applied') == '0') {
252 show("Applying fixes for version 2.5\n");
253 $queries[] = "ALTER TABLE project_task ADD CONSTRAINT project_task_group_project_id_f CHECK (1 = 1)";
254 $queries[] = "INSERT INTO database_changes (filename) VALUES ('2.5fixes')";
256 } else if ($version == '2.6') {
257 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
258 array ('2.6fixes')) ;
259 if ($res && db_result($res, 0, 'applied') == '0') {
260 show("Applying fixes for version 2.6\n");
261 $queries[] = "ALTER TABLE project_task ADD CONSTRAINT project_task_group_project_id_f CHECK (1 = 1)";
262 $queries[] = "INSERT INTO database_changes (filename) VALUES ('2.6fixes')";
264 } else if ($version == '3.0pre5') {
265 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
266 array ('3.0pre5fixes')) ;
267 if ($res && db_result($res, 0, 'applied') == '0') {
268 show("Applying fixes for version 3.0pre5\n");
269 if (!run_sql_script('fix-gforge3.0pre5.sql')) {
270 show("Error applying fixes for version 3.0pre5\n");
273 $queries[] = "INSERT INTO database_changes (filename) VALUES ('3.0pre5fixes')";
275 } else if ($version == '3.0pre6') {
276 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1', array ('3.0pre6fixes')) ;
277 if ($res && db_result($res, 0, 'applied') == '0') {
278 show("Applying fixes for version 3.0pre6\n");
279 $queries[] = "ALTER TABLE project_task ADD CONSTRAINT project_task_group_project_id_f CHECK (1 = 1)";
280 $queries[] = "INSERT INTO database_changes (filename) VALUES ('3.0pre6fixes')";
282 } else if ($version == '3.0pre7') {
283 $res = db_query_params ('SELECT COUNT(*) AS applied FROM database_changes WHERE filename=$1',
284 array ('3.0pre7fixes')) ;
285 if ($res && db_result($res, 0, 'applied') == '0') {
286 show("Applying fixes for version 3.0pre7\n");
287 $queries[] = "ALTER TABLE project_task ADD CONSTRAINT project_task_group_project_id_f CHECK (1 = 1)";
288 $queries[] = "INSERT INTO database_changes (filename) VALUES ('3.0pre7fixes')";
290 } else if ($version == '4.7') {
291 run_script(array('filename'=>'20070924-project-perm.sql','ext'=>'sql'));
292 run_script(array('filename'=>'20070924-forum-perm.sql','ext'=>'sql'));
293 run_script(array('filename'=>'20070924-artifact-perm.sql','ext'=>'sql'));
297 foreach ($queries as $query) {
298 $res = db_query_params($query, array());
300 show("ERROR: ".db_error()."\n");
309 function in_string($haystack, $needle, $case_sensitive = false) {
310 if (!$case_sensitive) {
311 $haystack = strtolower($haystack);
313 if (strpos($haystack, $needle) !== false) {
320 function show($text) {
322 fwrite(STDOUT, $text);
327 // c-file-style: "bsd"