From 31b772fe18117ffae9e070e89e865bacfb191e6a Mon Sep 17 00:00:00 2001 From: Igor Nadj Date: Mon, 19 Oct 2015 16:44:31 +1300 Subject: [PATCH] ENH: add clean up old test databases task --- code/tasks/DeleteOldTestDatabases.php | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 code/tasks/DeleteOldTestDatabases.php diff --git a/code/tasks/DeleteOldTestDatabases.php b/code/tasks/DeleteOldTestDatabases.php new file mode 100644 index 0000000..5b39481 --- /dev/null +++ b/code/tasks/DeleteOldTestDatabases.php @@ -0,0 +1,44 @@ +getVar('dry') !== '0'; + if($dry) echo 'DRY run, add ?dry=0 to run for realsies

'; + + $rows = DB::query('show databases;'); + $numDeleted = 0; + foreach($rows as $row){ + $db = $row['Database']; + echo 'Database: '.$db; + + if($this->isTempDb($db)){ + echo ' ... TEMP DB FOUND'; + if(!$dry){ + DB::query('DROP DATABASE '.$db.';'); + echo ' ... DELETED'; + $numDeleted++; + } + } + + echo '
'; + } + + echo '
Done, deleted '.$numDeleted.' temporary database(s)'; + } + + + /** + * @see framework/dev/SapphireTest.php::create_temp_db() + * @return boolean + */ + protected function isTempDb($name){ + $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_'; + $dbNameStartsWith = strtolower(sprintf('%stmpdb', $prefix)); + + return strpos($name, $dbNameStartsWith) === 0; + } + +} \ No newline at end of file