Skip to content

Instantly share code, notes, and snippets.

@stojg
Created June 13, 2012 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stojg/2920975 to your computer and use it in GitHub Desktop.
Save stojg/2920975 to your computer and use it in GitHub Desktop.
Remove SilverStripe test databases
<?php
/**
* This script will drop all the databases that contains this string 'tmpdb'.
* Those databases are likely to be left over from a SilverStripe test run
* Usage: Make sure you change the db connection string below and run the
* script like:
* php remove-tmp-db.php
*/
// Change this to your db connection
$host = '127.0.0.1';
$user = 'root';
$password = '';
error_reporting(E_ALL | E_STRICT);
$dbConn = mysql_connect($host, $user, $password);
$result = mysql_query('SHOW DATABASES;');
$toDrop = array();
while($row = mysql_fetch_assoc($result)) {
$schema = $row['Database'];
if(stristr($schema,'tmpdb')) {
$toDrop[] = $schema;
}
}
for($idx=1; $idx <= count($toDrop); $idx++) {
$schema = $toDrop[($idx-1)];
echo '[-] '.$idx.'/'.count($toDrop).' Dropping '.$schema.PHP_EOL;
mysql_query('DROP DATABASE '.$schema);
}
echo '[+] Done, all temporary databases has been dropped.'.PHP_EOL;
@wilr
Copy link

wilr commented Jun 13, 2012

And dev/tests/cleanupdb doesn't do this?

@stojg
Copy link
Author

stojg commented Jun 13, 2012

Sneaky Will, I did not know that, :D thanks!

@Firesphere
Copy link

Can this please pretty please be documented somewhere? I have to google this gist every time :|

@Firesphere
Copy link

Also, moved to /dev/tasks/ in SS4

@wilr
Copy link

wilr commented Sep 10, 2021

I love when 2012 Will helps 2021 Will. For reference and anyone googling in SilverStripe 4 this is now ./vendor/bin/sake dev/tasks/CleanupTestDatabasesTask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment