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)); + } + } +}