From 3e16c666dd9d8ef400d214824a69230d5e684811 Mon Sep 17 00:00:00 2001 From: butschster Date: Sat, 8 Jun 2024 10:50:40 +0400 Subject: [PATCH] Adds a new console command to cleanup database and storage buckets Improves Makefile: - Adds command to recreate local database (Doltdb) - Adds commands to cleanup runtime --- Makefile | 23 +++++++ .../Bootloader/PersistenceBootloader.php | 9 +++ .../Application/Database/CleanerInterface.php | 10 ++++ .../Integration/CycleOrm/DatabaseCleaner.php | 60 +++++++++++++++++++ app/src/Interfaces/Console/CleanupCommand.php | 40 +++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 app/src/Application/Database/CleanerInterface.php create mode 100644 app/src/Integration/CycleOrm/DatabaseCleaner.php create mode 100644 app/src/Interfaces/Console/CleanupCommand.php diff --git a/Makefile b/Makefile index 9b04977..99dfea4 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +# ====== Docker ======== + build: if [ ! -d "runtime" ]; then \ mkdir runtime/configs -p; \ @@ -49,6 +51,27 @@ pull-latest: build-server: docker compose build buggregator-server --no-cache; +# ====== Database ======== + +recreate-db: + rm -rf .db; + mkdir .db; + chmod 0777 -R .db; + bin/dolt --data-dir=.db sql -q "create database buggregator;"; + +# ======= Runtime ======== + +cleanup-attachments: + rm -rf runtime/attachments/*; + +cleanup-cache: + rm -rf runtime/cache; + +cleanup-snapshots: + rm -rf runtime/snapshots; + +cleanup: cleanup-attachments cleanup-cache cleanup-snapshots; + # ========================= bash: diff --git a/app/src/Application/Bootloader/PersistenceBootloader.php b/app/src/Application/Bootloader/PersistenceBootloader.php index 7f36c4a..28705f3 100644 --- a/app/src/Application/Bootloader/PersistenceBootloader.php +++ b/app/src/Application/Bootloader/PersistenceBootloader.php @@ -4,7 +4,9 @@ namespace App\Application\Bootloader; +use App\Application\Database\CleanerInterface; use App\Application\Persistence\DriverEnum; +use App\Integration\CycleOrm\DatabaseCleaner; use App\Interfaces\Console\RegisterModulesCommand; use Spiral\Boot\Bootloader\Bootloader; use Spiral\Console\Bootloader\ConsoleBootloader; @@ -26,6 +28,13 @@ public function defineDependencies(): array ]; } + public function defineSingletons(): array + { + return [ + CleanerInterface::class => DatabaseCleaner::class, + ]; + } + public function init(ConsoleBootloader $console, DriverEnum $driver): void { if ($driver === DriverEnum::Database) { diff --git a/app/src/Application/Database/CleanerInterface.php b/app/src/Application/Database/CleanerInterface.php new file mode 100644 index 0000000..7896483 --- /dev/null +++ b/app/src/Application/Database/CleanerInterface.php @@ -0,0 +1,10 @@ +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(); + } +} diff --git a/app/src/Interfaces/Console/CleanupCommand.php b/app/src/Interfaces/Console/CleanupCommand.php new file mode 100644 index 0000000..412ab47 --- /dev/null +++ b/app/src/Interfaces/Console/CleanupCommand.php @@ -0,0 +1,40 @@ +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)); + } + } +}