Skip to content

Commit

Permalink
Adds a new console command to cleanup database and storage buckets
Browse files Browse the repository at this point in the history
Improves Makefile:
- Adds command to recreate local database (Doltdb)
- Adds commands to cleanup runtime
  • Loading branch information
butschster committed Jun 8, 2024
1 parent 6a693c3 commit 3e16c66
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ====== Docker ========

build:
if [ ! -d "runtime" ]; then \
mkdir runtime/configs -p; \
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions app/src/Application/Bootloader/PersistenceBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions app/src/Application/Database/CleanerInterface.php
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;
}
60 changes: 60 additions & 0 deletions app/src/Integration/CycleOrm/DatabaseCleaner.php
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();
}
}
40 changes: 40 additions & 0 deletions app/src/Interfaces/Console/CleanupCommand.php
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));
}
}
}

0 comments on commit 3e16c66

Please sign in to comment.