-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from buggregator/feature/cleanup-command
New Cleanup Commands for Database and Storage Management
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\Database; | ||
|
||
interface CleanerInterface | ||
{ | ||
public function clean(?string $database = null): \Generator; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Integration\CycleOrm; | ||
|
||
use App\Application\Database\CleanerInterface; | ||
use Cycle\Database\DatabaseProviderInterface; | ||
use Cycle\Migrations\Config\MigrationConfig; | ||
|
||
final readonly class DatabaseCleaner implements CleanerInterface | ||
{ | ||
public function __construct( | ||
private MigrationConfig $config, | ||
private DatabaseProviderInterface $provider, | ||
) {} | ||
|
||
public function clean(?string $database = null): \Generator | ||
{ | ||
$db = $this->provider->database($database); | ||
|
||
|
||
foreach ($db->getTables() as $table) { | ||
$this->disableForeignKeyConstraints($database); | ||
|
||
// Skip the table that stores the list of executed migrations | ||
if ($table->getName() === $this->config->getTable()) { | ||
continue; | ||
} | ||
|
||
/** | ||
* @psalm-suppress UndefinedInterfaceMethod | ||
*/ | ||
$db->getDriver()->getSchemaHandler()->eraseTable($db->table($table->getFullName())->getSchema()); | ||
yield $table->getName(); | ||
|
||
$this->enableForeignKeyConstraints($database); | ||
} | ||
} | ||
|
||
public function disableForeignKeyConstraints(?string $database = null): void | ||
{ | ||
$db = $this->provider->database($database); | ||
|
||
/** | ||
* @psalm-suppress UndefinedInterfaceMethod | ||
*/ | ||
$db->getDriver()->getSchemaHandler()->disableForeignKeyConstraints(); | ||
} | ||
|
||
public function enableForeignKeyConstraints(?string $database = null): void | ||
{ | ||
$db = $this->provider->database($database); | ||
|
||
/** | ||
* @psalm-suppress UndefinedInterfaceMethod | ||
*/ | ||
$db->getDriver()->getSchemaHandler()->enableForeignKeyConstraints(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Interfaces\Console; | ||
|
||
use App\Application\Database\CleanerInterface; | ||
use Spiral\Boot\DirectoriesInterface; | ||
use Spiral\Console\Attribute\AsCommand; | ||
use Spiral\Console\Command; | ||
use Spiral\Storage\Config\StorageConfig; | ||
use Spiral\Storage\StorageInterface; | ||
|
||
#[AsCommand( | ||
name: 'cleanup', | ||
description: 'Clean database and storage data', | ||
)] | ||
final class CleanupCommand extends Command | ||
{ | ||
public function __invoke( | ||
CleanerInterface $cleaner, | ||
StorageInterface $storage, | ||
DirectoriesInterface $dirs, | ||
StorageConfig $config, | ||
): void { | ||
$this->info('Cleaning database...'); | ||
|
||
foreach ($cleaner->clean() as $table) { | ||
$this->writeln(\sprintf('- Table %s cleaned', $table)); | ||
} | ||
|
||
$this->newLine(); | ||
|
||
$this->info('Cleaning storage...'); | ||
foreach ($config->getAdapters() as $bucket => $adapter) { | ||
$adapter->deleteDirectory('*'); | ||
$this->writeln(\sprintf('- Bucket %s cleaned', $bucket)); | ||
} | ||
} | ||
} |