/[drupal]/contributions/modules/project_issue_file_review/pifr.install
ViewVC logotype

Contents of /contributions/modules/project_issue_file_review/pifr.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.38 - (show annotations) (download) (as text)
Tue Nov 3 16:22:22 2009 UTC (3 weeks, 3 days ago) by boombatower
Branch: MAIN
Branch point for: DRUPAL-7--1
Changes since 1.37: +2 -2 lines
File MIME type: text/x-php
#621580: pifr-2.x test master admin page wsod.
1 <?php
2 // $Id: pifr.install,v 1.37 2009/11/02 23:42:54 boombatower Exp $
3
4 /**
5 * @file
6 * Automatically review patches attached to issues.
7 *
8 * Copyright 2008-2009 by Jimmy Berry ("boombatower", http://drupal.org/user/214218)
9 */
10
11 /**
12 * Implementation of hook_uninstall().
13 */
14 function pifr_uninstall() {
15 variable_del('pifr_active');
16 variable_del('pifr_debug');
17 }
18
19 /**
20 * Generate a message that can be placed in $ret of update hook.
21 *
22 * @param string $message Message describing activity.
23 * @param boolean $success TRUE if successful, otherwise FALSE.
24 * @return array Array to be added to $ret.
25 */
26 function pifr_update_message($message, $success = TRUE) {
27 return array('success' => $success, 'query' => $message);
28 }
29
30 /**
31 * Remove old variables.
32 */
33 function pifr_update_6200() {
34 $ret = array();
35
36 // List of old variables.
37 $vars = array(
38 'pifr_review_key',
39 'pifr_server',
40 'pifr_file_id',
41 'pifr_file',
42 'pifr_role',
43 'pifr_test_master_active',
44 'pifr_test_timeout',
45 'pifr_test_master_cron_delay',
46 'pifr_test_master_cron_last',
47 'pifr_test_master_report',
48 'pifr_php_executable',
49 'pifr_debug',
50 'pifr_result_last',
51 'pifr_test_master_check_do',
52 'pifr_commit_last',
53 'pifr_commit_last_author',
54 'pifr_commit_head_passes',
55 'pifr_last_test_count',
56 );
57
58 // Cycle through variables and remove them.
59 foreach ($vars as $var) {
60 variable_del($var);
61 }
62
63 $ret[] = pifr_update_message('Deleted [' . count($vars) . '] variable(s).');
64
65 return $ret;
66 }
67
68 /**
69 * Rename old tables with suffix '_old'.
70 */
71 function pifr_update_6201() {
72 $ret = array();
73
74 // Rename old tables so new schema can be installed and data migrated.
75 foreach (array('file', 'result', 'log', 'server') as $table) {
76 db_rename_table($ret, "pifr_$table", "pifr_{$table}_old");
77 }
78
79 return $ret;
80 }
81
82 /**
83 * Enable equivilent 2.x modules.
84 */
85 function pifr_update_6202() {
86 $ret = array();
87
88 // Install equivilent 2.x modules and dependencies.
89 include_once './includes/install.inc';
90 $modules = array(
91 'chart',
92 'views',
93 'tabs',
94 'pifr_server',
95 'pifr_assertion',
96 'pifr_simpletest'
97 );
98 foreach ($modules as $module) {
99 drupal_install_modules(array($module));
100 $ret[] = pifr_update_message('Installed [' . $module . '].');
101 }
102
103 // Clear out statics.
104 module_list(TRUE, FALSE);
105 module_implements('', FALSE, TRUE);
106 drupal_get_schema(NULL, TRUE);
107
108 return $ret;
109 }
110
111 /**
112 * Setup default MySQL 5.0 ISAM environment.
113 */
114 function pifr_update_6203() {
115 $ret = array();
116
117 $environment = array(
118 'title' => 'MySQL 5.0 ISAM',
119 'description' => 'MySQL 5.0 ISAM environment for running Drupal SimpleTest tests.',
120 'require_pass' => TRUE,
121 'client' => array(),
122 'project' => array(),
123 'branch' => array(),
124 'type' => array(),
125 'plugin' => 'pifr_simpletest',
126 'plugin_argument' => array('database' => 'mysql-5.0-isam'),
127 );
128 $environment = pifr_server_environment_save($environment);
129
130 $ret[] = pifr_update_message('Added [' . $environment['title'] . '] environment.', !empty($environment['environment_id']));
131
132 return $ret;
133 }
134
135
136 /**
137 * Migrate pifr_server table.
138 */
139 function pifr_update_6204() {
140 $ret = array();
141
142 $result = db_query('SELECT * FROM {pifr_server_old} ORDER BY server_id');
143 while ($server_old = db_fetch_array($result)) {
144 $client = array(
145 'client_key' => $server_old['server_key'],
146 'type' => pifr_update_6204_type($server_old['type']),
147 'environment' => $server_old['type'] == 1 ? array() : array(1 => 1), // Assume MySQL will be environment ID 1.
148 'uid' => 1, // Not migration path, this will need to be manually fixed.
149 'url' => $server_old['url'],
150 'status' => $server_old['type'] == 4 ? PIFR_SERVER_CLIENT_STATUS_DISABLED : PIFR_SERVER_CLIENT_STATUS_TESTING,
151 );
152
153 // All project clients can remain enabled.
154 if ($server_old['type'] == 1) {
155 $client['status'] = PIFR_SERVER_CLIENT_STATUS_ENABLED;
156 }
157
158 if ($client['type'] == 0) {
159 // Test master which does not exist in 2.x.
160 continue;
161 }
162
163 // Save converted client data.
164 $client = pifr_server_client_save($client);
165
166 $ret[] = pifr_update_message('Updated server [' . $server_old['server_id'] . '] to client [' .
167 $client['client_id'] . '].', (bool) $client['client_id']);
168 }
169
170 return $ret;
171 }
172
173 /**
174 * Convert 1.x client status code to 2.x status code.
175 *
176 * @param integer $status 1.x client status code.
177 * @return integer 2.x client status code.
178 */
179 function pifr_update_6204_type($type) {
180 $map = array(
181 1 => 1, // PIFR_SERVER_PROJECT => PIFR_SERVER_CLIENT_TYPE_PROJECT.
182 2 => 0, // PIFR_SERVER_TEST_MASTER => Does not exist in 2.x.
183 3 => 2, // PIFR_SERVER_SLAVE => PIFR_SERVER_CLIENT_TYPE_TEST.
184 4 => 2, // PIFR_SERVER_DISABLED => PIFR_SERVER_CLIENT_TYPE_TEST.
185 );
186 return $map[$type];
187 }
188
189 /**
190 * Setup Drupal core project and 7.x HEAD branch.
191 */
192 function pifr_update_6205() {
193 $ret = array();
194
195 // Determine drupal.org project client ID.
196 $client_id = db_result(db_query("SELECT client_id FROM {pifr_client} WHERE url = '%s'", 'http://drupal.org/'));
197 $ret[] = pifr_update_message('Found drupal.org client with ID [' . $client_id . '].', (bool) $client_id);
198
199 $project = array(
200 'client_id' => $client_id,
201 'client_identifier' => 3060,
202 'name' => 'Drupal',
203 'repository_type' => 'cvs',
204 'repository_url' => ':pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal/drupal/',
205 'link' => 'http://drupal.org/node/3060',
206 );
207 $project = pifr_server_project_save($project, FALSE);
208 $ret[] = pifr_update_message('Created Drupal core project.' , (bool) $project['project_id']);
209
210 $branch = array(
211 'project_id' => $project['project_id'],
212 'client_identifier' => 156281,
213 'vcs_identifier' => 'HEAD',
214 'dependency' => '',
215 'plugin_argument' => array('core' => 6),
216 'link' => 'http://drupal.org/node/156281',
217 );
218 $branch = pifr_server_branch_save($branch, FALSE);
219 $ret[] = pifr_update_message('Created Drupal core 7.x branch.', (bool) $branch['branch_id']);
220
221 return $ret;
222 }
223
224 /**
225 * Migrate pifr_file and pifr_result tables.
226 */
227 function pifr_update_6206(&$sandbox = NULL) {
228 $ret = array();
229
230 if (!isset($sandbox['progress'])) {
231 $sandbox['progress'] = 0;
232 $sandbox['current_file_id'] = 0;
233 $sandbox['max'] = db_result(db_query('SELECT COUNT(file_id) FROM {pifr_file_old}'));
234
235 // Generate initial file map output file.
236 $sandbox['map_file'] = file_directory_path() . '/pifr.file.map';
237 file_put_contents($sandbox['map_file'], '');
238 }
239
240 $result = db_query_range('SELECT *
241 FROM {pifr_file_old}
242 WHERE file_id > %d
243 ORDER BY file_id ASC', $sandbox['current_file_id'], 0, 300);
244 $file_map = array();
245 while ($file_old = db_fetch_array($result)) {
246 // Insert file record and create test record.
247 $file = array(
248 'branch_id' => 1, // Assume Drupal 7.x core branch will be branch ID 1.
249 'client_identifier' => $file_old['server_file_id'],
250 'file_url' => 'http://drupal.org/files/issues/' . $file_old['filename'],
251 'file_name' => $file_old['filename'],
252 'link' => 'http://drupal.org/node/' . $file_old['nid'] . ($file_old['cid'] ? '#comment-' . $file_old['cid'] : ''),
253 );
254 $file = pifr_server_file_save($file, FALSE);
255
256 if (empty($file['file_id'])) {
257 $ret['#abort'] = pifr_update_message('Failed to migrate file test record [' . $file_old['file_id'] . '].', FALSE);
258 break;
259 }
260
261 // Keep track of new file test ID and server file ID.
262 $file_map[] = $file['test_id'] . ',' . $file_old['server_file_id'];
263
264 // Update test record with current state.
265 $test = pifr_server_test_get($file['test_id']);
266 $test = array(
267 'status' => pifr_update_6206_status($file_old['status']),
268 'last_recieved' => $file_old['received'],
269 'last_requested' => $file_old['sent'],
270 'last_tested' => $file_old['last_tested'],
271 'test_count' => $file_old['test_count'],
272 ) + $test;
273 $test = pifr_server_test_save($test);
274
275 // Only store a result record if test has a result.
276 if ($test['status'] == PIFR_SERVER_TEST_STATUS_RESULT) {
277 // Insert test result and details.
278 $test_result = array(
279 'test_id' => $test['test_id'],
280 'environment_id' => 1, // Assume MySQL ISAM will be environment ID 1.
281 'code' => $code = pifr_update_6206_code($file_old),
282 'details' => pifr_update_6206_details($code, $file_old),
283 'data' => array(),
284 );
285
286 if ($test_result['code'] >= PIFR_SERVER_TEST_RESULT_FAIL) {
287 // Some data is corrupted, ignore test results unless proper code.
288 $file_result_details = db_query('SELECT *
289 FROM {pifr_result_old}
290 WHERE file_id = %d', $file_old['file_id']);
291 while ($file_result_detail = db_fetch_array($file_result_details)) {
292 $test_result['data'][$file_result_detail['test_class']] = array(
293 'test_name' => $file_result_detail['test_class'],
294 'pass' => $file_result_detail['pass'],
295 'fail' => $file_result_detail['fail'],
296 'exception' => $file_result_detail['exception'],
297 'assertions' => array(), // Assertion data is not collected in 1.x.
298 );
299 }
300 }
301
302 pifr_server_result_save($test_result);
303 }
304
305 $sandbox['progress']++;
306 $sandbox['current_file_id'] = $file_old['file_id'];
307 }
308
309 // Store map of new file test ID to old server file ID.
310 if ($file_map) {
311 file_put_contents($sandbox['map_file'], implode("\n", $file_map) . "\n", FILE_APPEND);
312 }
313
314 $ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
315
316 // Upon completion of the migration check the number of rows in the new
317 // tables to ensure that all the data was migrated successfully.
318 if ($ret['#finished'] == 1) {
319 $test_count = db_result(db_query('SELECT COUNT(test_id) FROM {pifr_test} WHERE type = %d', PIFR_SERVER_TEST_TYPE_FILE));
320 $result_count = db_result(db_query('SELECT COUNT(result_id) FROM {pifr_result}'));
321
322 $success = ($test_count == $result_count && $test_count == $sandbox['max']);
323 $ret[] = pifr_update_message('Migrated [' . number_format($sandbox['max']) . '] tests and results.', $success);
324
325 // Ensure that generated map file has the proper number of lines.
326 $line_count = count(file($sandbox['map_file']));
327 $success = ($line_count == $test_count);
328 $ret[] = pifr_update_message('Generated map file [' . $sandbox['map_file'] . '].', $success);
329 }
330
331 return $ret;
332 }
333
334 /**
335 * Convert 1.x test status code to 2.x status code.
336 *
337 * @param integer $status 1.x test status code.
338 * @return integer 2.x test status code.
339 */
340 function pifr_update_6206_status($status) {
341 $map = array(
342 1 => 2, // PIFR_FILE_QUEUED => PIFR_SERVER_TEST_STATUS_QUEUED.
343 2 => 2, // PIFR_FILE_SENT => PIFR_SERVER_TEST_STATUS_QUEUED.
344 3 => 4, // PIFR_FILE_ERROR => PIFR_SERVER_TEST_STATUS_RESULT.
345 4 => 4, // PIFR_FILE_TEST_FAIL => PIFR_SERVER_TEST_STATUS_RESULT.
346 5 => 4, // PIFR_FILE_TEST_PASS => PIFR_SERVER_TEST_STATUS_RESULT.
347 );
348 return $map[$status];
349 }
350
351 /**
352 * Convert 1.x status message to 2.x test result code.
353 *
354 * @param array $file 1.x file information.
355 * @return integer 2.x test result code.
356 */
357 function pifr_update_6206_code(array $file) {
358 switch ($file['message']) {
359 case 'Failed to fetch file.':
360 return PIFR_SERVER_TEST_RESULT_FETCH;
361 case 'Failed to checkout HEAD.':
362 return PIFR_SERVER_TEST_RESULT_CVS;
363 case 'Failed to apply patch.':
364 return PIFR_SERVER_TEST_RESULT_APPLY;
365 case 'Failed to install HEAD.':
366 return PIFR_SERVER_TEST_RESULT_INSTALL;
367 case 'Failed to run tests.':
368 return PIFR_SERVER_TEST_RESULT_TEST;
369 }
370
371 if (substr($file['message'], 0, 17) == 'Invalid PHP synta') { // Ignore "in @file".
372 return PIFR_SERVER_TEST_RESULT_SYNTAX;
373 }
374 else {
375 // Detect pass/fail.
376 return $file['exception'] + $file['fail'] == 0 ?
377 PIFR_SERVER_TEST_RESULT_PASS : PIFR_SERVER_TEST_RESULT_FAIL;
378 }
379 }
380
381 /**
382 * Convert 1.x message to 2.x details array.
383 *
384 * @param integer $code 2.x test result code.
385 * @param array $file 1.x file information.
386 * @return array 2.x details.
387 */
388 function pifr_update_6206_details($code, array $file) {
389 switch ($code) {
390 case PIFR_SERVER_TEST_RESULT_FETCH:
391 return array('@reason' => 'failed to retrieve [' . $file['filename'] . '] from project client');
392 case PIFR_SERVER_TEST_RESULT_CVS:
393 return array('@reason' => 'failed to checkout from [:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal/drupal]');
394 case PIFR_SERVER_TEST_RESULT_APPLY:
395 return array('@filename' => $file['filename']);
396 case PIFR_SERVER_TEST_RESULT_INSTALL:
397 return array();
398 case PIFR_SERVER_TEST_RESULT_SYNTAX:
399 return array('@filename' => $file['filename']);
400 case PIFR_SERVER_TEST_RESULT_TEST:
401 return array('@reason' => 'failed during invocation of run-tests.sh');
402 case PIFR_SERVER_TEST_RESULT_FAIL:
403 case PIFR_SERVER_TEST_RESULT_PASS:
404 return array(
405 '@pass' => $file['pass'],
406 '@fail' => $file['fail'],
407 '@exception' => $file['exception'],
408 );
409 }
410 return array();
411 }
412
413
414 /**
415 * Migrate pifr_log table.
416 *
417 * Assumes pifr_log has been renamed to pifr_log_old.
418 */
419 function pifr_update_6207(&$sandbox = NULL) {
420 $ret = array();
421
422 if (!isset($sandbox['progress'])) {
423 $sandbox['progress'] = 0;
424 $sandbox['current_log_id'] = 0;
425 $sandbox['max'] = db_result(db_query('SELECT COUNT(log_id) FROM {pifr_log_old}'));
426 }
427
428 // Query is quite intensive, go through in large batches.
429 set_time_limit(0);
430
431 $result = db_query_range('SELECT l.*, f.test_id
432 FROM {pifr_log_old} l
433 JOIN {pifr_file_old} o
434 ON l.file_id = o.file_id
435 JOIN {pifr_file} f
436 ON o.server_file_id = f.client_identifier
437 WHERE l.log_id > %d
438 ORDER BY l.log_id ASC', $sandbox['current_log_id'], 0, 10000);
439 while ($log_old = db_fetch_array($result)) {
440 list($client_id, $code) = pifr_update_6207_details($log_old['message']);
441
442 // If log details are determined then migrate record.
443 if ($client_id !== FALSE) {
444 $log = array(
445 'test_id' => $log_old['test_id'],
446 'client_id' => pifr_update_6207_client_id($client_id),
447 'code' => $code,
448 'timestamp' => $log_old['timestamp'],
449 );
450 drupal_write_record('pifr_log', $log);
451 }
452 else {
453 $ret['#abort'] = pifr_update_message('Failed to migrate log record [' . $log_old['log_id'] . '].', FALSE);
454 }
455
456 $sandbox['progress']++;
457 $sandbox['current_log_id'] = $log_old['log_id'];
458 }
459
460 $ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
461
462 // Upon completion of the migration check the number of rows in the new
463 // tables to ensure that all the data was migrated successfully.
464 if ($ret['#finished'] == 1) {
465 $log_count = db_result(db_query('SELECT COUNT(log_id) FROM {pifr_log}'));
466
467 $success = ($log_count == $sandbox['max']);
468 $ret[] = pifr_update_message('Migrated [' . number_format($sandbox['max']) . '] log entries.', $success);
469 }
470
471 return $ret;
472 }
473
474 /**
475 * Convert 1.x log message to 2.x log message.
476 *
477 * @param string $message 1.x log message.
478 * @return string 2.x log message.
479 */
480 function pifr_update_6207_details($message) {
481 static $client_id;
482
483 if (!isset($client_id)) {
484 // Determine drupal.org project client ID.
485 $client_id = db_result(db_query("SELECT client_id FROM {pifr_client} WHERE url = '%s'", 'http://drupal.org/'));
486 $ret[] = pifr_update_message('Found drupal.org client with ID [' . $client_id . '].', (bool) $client_id);
487 }
488
489 switch ($message) {
490 case 'Results sent to project server.':
491 return array($client_id, PIFR_SERVER_LOG_CLIENT_RETRIEVE);
492 case 'Test request received from the project server.':
493 return array($client_id, PIFR_SERVER_LOG_CLIENT_REQUEST);
494 case 'Re-test request received from the project server.':
495 return array($client_id, PIFR_SERVER_LOG_CLIENT_REQUEST);
496 }
497
498 if (preg_match('/Test relayed to slave #(\d+)/', $message, $match)) {
499 return array($match[1], PIFR_SERVER_LOG_TEST_REQUEST);
500 }
501 else if (preg_match('/Result received from slave #(\d+) \(/', $message, $match)) {
502 return array($match[1], PIFR_SERVER_LOG_CLIENT_RESULT);
503 }
504 return array(FALSE, FALSE);
505 }
506
507 /**
508 * Convert 1.x server ID to 2.x client ID.
509 *
510 * @param integer $server_id 1.x server ID.
511 * @return integer 2.x client ID.
512 */
513 function pifr_update_6207_client_id($server_id) {
514 $result = db_query('SELECT c.client_id
515 FROM {pifr_client} c
516 JOIN {pifr_server_old} o
517 ON c.client_key = o.server_key
518 WHERE o.server_id = %d', $server_id);
519 return db_result($result);
520 }
521
522 /**
523 * Remove old tables.
524 */
525 function pifr_update_6208() {
526 $ret = array();
527
528 foreach (array('file', 'result', 'log', 'server') as $table) {
529 db_drop_table($ret, "pifr_{$table}_old");
530 }
531
532 return $ret;
533 }

  ViewVC Help
Powered by ViewVC 1.1.2