Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
scripts: Implement manageProject.php "--delete" option
Browse files Browse the repository at this point in the history
  • Loading branch information
Krinkle committed Jul 9, 2016
1 parent 7b9ec02 commit 31191f8
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 27 deletions.
6 changes: 5 additions & 1 deletion inc/MaintenanceScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ public function run() {
if ( $this->getOption( 'help' ) ) {
$this->displayHelp();
} else {
$this->execute();
try {
$this->execute();
} catch ( Exception $e ) {
$this->error( $e );
}
}
$this->out( '' );
exit;
Expand Down
56 changes: 37 additions & 19 deletions inc/actions/WipejobAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,40 @@ public function doAction() {
return;
}

$runRows = $db->getRows(str_queryf(
$this->doWipeJobs( $wipeType, [ $jobID ] );

$this->setData( array(
'jobID' => $jobID,
'type' => $wipeType,
'result' => 'ok',
) );
}

public function doWipeJobs( $wipeType, array $jobIDs, $batchSize = 100 ) {
$db = $this->getContext()->getDB();
$stats = array(
'jobs' => count( $jobIDs ),
'runs' => 0,
'run_useragent' => 0,
);

$allRunRows = $db->getRows(str_queryf(
'SELECT id
FROM runs
WHERE job_id = %u;',
$jobID
WHERE job_id IN %l;',
$jobIDs
));

if ( $runRows ) {
foreach ( $runRows as $runRow ) {
if ( $allRunRows ) {
$chunks = array_chunk( $allRunRows, $batchSize );
foreach ( $chunks as $runRows ) {
$runIDs = array_map( function ( $row ) { return $row->id; }, $runRows );
if ( $wipeType === 'delete' ) {
$db->query(str_queryf(
'DELETE
FROM run_useragent
WHERE run_id = %u;',
$runRow->id
WHERE run_id in %l;',
$runIDs
));
} elseif ( $wipeType === 'reset' ) {
$db->query(str_queryf(
Expand All @@ -73,37 +92,36 @@ public function doAction() {
completed = 0,
results_id = NULL,
updated = %s
WHERE run_id = %u;',
WHERE run_id in %l;',
swarmdb_dateformat( SWARM_NOW ),
$runRow->id
$runIDs
));
}
$stats['run_useragent'] += $db->getAffectedRows();
}
}

// This should be outside the if for $runRows, because jobs
// This should be outside the if for $allRunRows, because jobs
// can sometimes be created without any runs (by accidently).
// Those should be deletable as well, thus this has to be outside the loop.
// Also, no need to do this in a loop, just delete them all in one query.
if ( $wipeType === 'delete' ) {
$db->query(str_queryf(
'DELETE
FROM runs
WHERE job_id = %u;',
$jobID
WHERE job_id IN %l;',
$jobIDs
));
$stats['runs'] += $db->getAffectedRows();
$db->query(str_queryf(
'DELETE
FROM jobs
WHERE id = %u;',
$jobID
WHERE id IN %l;',
$jobIDs
));
$stats['jobs'] += $db->getAffectedRows();
}

$this->setData( array(
'jobID' => $jobID,
'type' => $wipeType,
'result' => 'ok',
) );
return $stats;
}
}
2 changes: 1 addition & 1 deletion inc/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function swarmAutoLoader( $className ) {
* Custom PHP settings
* @{
*/
if ( $swarmConfig->debug->phpErrorReporting ) {
if ( $swarmConfig->debug->phpErrorReporting || SWARM_ENTRY === 'SCRIPT' ) {
error_reporting( E_ALL & ~E_DEPRECATED );
ini_set( 'display_errors', 1 );
} else {
Expand Down
78 changes: 72 additions & 6 deletions scripts/manageProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ class ManageProjectScript extends MaintenanceScript {

protected function init() {
$this->setDescription(
'Create a new TestSwarm project. Returns the auth token (can be re-created with refreshProjectToken.php).'
'Create or update a TestSwarm project.'
);
$this->registerOption( 'create', 'boolean', 'Pass this to the create if it doesn\'t exist.' );
$this->registerOption( 'delete', 'boolean', 'Pass this to remove a project and recursively delete all its jobs and runs.' );
$this->registerOption( 'id', 'value', 'ID of project (must be in format: "' . LoginAction::getNameValidationRegex() . '").' );
$this->registerOption( 'display-title', 'value', 'Display title (free form text, max: 255 chars)' );
$this->registerOption( 'password', 'value', 'Password for this project (omit to enter in interactive mode)' );
$this->registerOption( 'site-url', 'value', 'URL for this project (optional)' );
}

protected function execute() {
$create = $this->getOption( 'create' );

if ( $create ) {
if ( $this->getOption( 'create' ) ) {
$this->create();
} elseif ( $this->getOption( 'delete' ) ) {
$this->delete();
} else {
$this->update();

}
}

Expand Down Expand Up @@ -88,7 +88,6 @@ protected function create() {
protected function update() {
$db = $this->getContext()->getDB();


$id = $this->getOption( 'id' );
$displayTitle = $this->getOption( 'display-title' );
$siteUrl = $this->getOption( 'site-url' );
Expand Down Expand Up @@ -133,6 +132,73 @@ protected function update() {

$this->out( 'Project has been updated.' );
}

public function delete() {
$db = $this->getContext()->getDB();

$projectID = $this->getOption( 'id' );
if ( !$projectID ) {
$this->error( '--id is required.' );
}

// Ensure the project exists.
$field = $db->getOne(str_queryf( 'SELECT id FROM projects WHERE id = %s;', $projectID ));
if ( !$field ) {
$this->error( 'Project does not exist.' );
}

$this->out( "Are you sure you want to remove project '$projectID' and all associated jobs and runs? (Y/N)" );
$answer = $this->cliInput();
if ( $answer !== 'Y' ) {
$this->error( 'Aborted.' );
return;
}

$batchSize = 100;
$stats = array();
while ( true ) {
$jobRows = $db->getRows(str_queryf(
'SELECT id
FROM jobs
WHERE project_id = %s
ORDER BY id
LIMIT %u;',
$projectID,
$batchSize
));
if ( !$jobRows ) {
// Done
break;
}
$jobIDs = array_map( function ( $row ) { return $row->id; }, $jobRows );
$this->out( '...deleting ' . count( $jobIDs ) . ' jobs' );
$action = WipejobAction::newFromContext( $this->getContext() );
$result = $action->doWipeJobs( 'delete', $jobIDs, $batchSize );
$this->mergeStats( $stats, $result );
}
foreach ( $stats as $key => $val ) {
$this->out( "deleted $key rows: $val");
}

$this->out( '' );
$this->out( '...deleting project credentials' );
$db->query(str_queryf( 'DELETE FROM projects WHERE id = %s;', $projectID ));

$this->out( 'project has been deleted.' );

$this->out( '' );
$this->out( 'Done!' );
}

protected function mergeStats( array &$target, array $add ) {
foreach ( $add as $key => $val ) {
if ( isset( $target[$key] ) ) {
$target[$key] += $val;
} else {
$target[$key] = $val;
}
}
}
}

$script = ManageProjectScript::newFromContext( $swarmContext );
Expand Down

0 comments on commit 31191f8

Please sign in to comment.