1 | <?php |
||
2 | |||
3 | namespace Sunnysideup\DeleteAllTables; |
||
4 | |||
5 | use SilverStripe\Control\Director; |
||
6 | use SilverStripe\Dev\BuildTask; |
||
7 | use SilverStripe\ORM\DB; |
||
8 | use Sunnysideup\Flush\FlushNow; |
||
9 | use Sunnysideup\Flush\FlushNowImplementor; |
||
10 | |||
11 | class DeleteAllVersionedData extends BuildTask |
||
12 | { |
||
13 | protected $title = 'CAREFUL: delete all versioned data'; |
||
14 | |||
15 | protected $description = 'Delete versioned data!'; |
||
16 | |||
17 | public function run($request) |
||
18 | { |
||
19 | if (!Director::isLive()) { |
||
20 | $rows = DB::query('SHOW TABLES;'); |
||
21 | foreach ($rows as $row) { |
||
22 | if ($row) { |
||
0 ignored issues
–
show
|
|||
23 | if (is_array($row)) { |
||
24 | foreach ($row as $db => $table) { |
||
25 | $this->truncateTable($table); |
||
26 | } |
||
27 | } else { |
||
28 | $table = $row['table'] ?? ''; |
||
29 | if ($table) { |
||
30 | $this->truncateTable($table); |
||
31 | } |
||
32 | } |
||
33 | } |
||
34 | } |
||
35 | FlushNowImplementor::do_flush('TRUNCATING ChangeSet'); |
||
36 | DB::query('TRUNCATE TABLE "ChangeSet";'); |
||
37 | FlushNowImplementor::do_flush('TRUNCATING ChangeSetItem'); |
||
38 | DB::query('TRUNCATE TABLE "ChangeSetItem";'); |
||
39 | } else { |
||
40 | FlushNowImplementor::do_flush('You need to set the environment to TEST or DEV to run this task.'); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | |||
45 | private function truncateTable(string $table) |
||
46 | { |
||
47 | if ('_Versions' === substr((string) $table, -1 * strlen('_Versions'))) { |
||
48 | FlushNowImplementor::do_flush('TRUNCATING ' . $table); |
||
49 | DB::query('TRUNCATE TABLE "' . $table . '";'); |
||
50 | } |
||
51 | } |
||
52 | } |
||
53 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.