Tests for clean up S3 orphans

This commit is contained in:
= 2021-06-13 20:09:52 +10:00
parent a42223a0be
commit 2b6f0870b3
2 changed files with 49 additions and 13 deletions

View File

@ -43,23 +43,20 @@ class S3Cleanup extends Command
$c1 = Company::on('db-ninja-01')->pluck('company_key');
$c2 = Company::on('db-ninja-02')->pluck('company_key');
$merged = $c1->merge($c2);
$merged = $c1->merge($c2)->toArray();
$c3 = Storage::disk(config('filesystems.default'))->directories();
$diff = $merged->diff($c3);
$directories = Storage::disk(config('filesystems.default'))->directories();
$this->LogMessage("Disk Cleanup");
$this->logMessage("Folders to delete = ". $c1->count());
$diff->each(function ($dir){
$this->logMessage("Deleting $dir");
Storage::deleteDirectory($dir);
});
foreach($directories as $dir)
{
if(!in_array($dir, $merged))
{
$this->logMessage("Deleting $dir");
Storage::disk(config('filesystems.default'))->deleteDirectory($dir);
}
}
$this->logMessage("exiting");

View File

@ -0,0 +1,39 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Unit;
use App\DataMapper\ClientSettings;
use Tests\TestCase;
/**
* @test
*/
class S3CleanupTest extends TestCase
{
public function setUp() :void
{
parent::setUp();
}
public function testMergeCollections()
{
$c1 = collect(["1","2","3","4"]);
$c2 = collect(["5","6","7","8"]);
$c3 = collect(["1","2","10"]);
$merged = $c1->merge($c2)->toArray();
$this->assertTrue(in_array("1", $merged));
$this->assertFalse(in_array("10", $merged));
}
}