From 84cdc19fb02deaeb02f3d239bc4338f99f0ab8cd Mon Sep 17 00:00:00 2001 From: Kyrch Date: Mon, 5 Jan 2026 07:05:53 -0300 Subject: [PATCH] feat(dev): make db:sync command smarter (#1046) --- README.md | 3 +++ app/Actions/Storage/Admin/Dump/DumpAction.php | 8 +++--- .../Storage/Admin/Dump/DumpAdminAction.php | 2 +- .../Storage/Admin/Dump/DumpAuthAction.php | 2 +- .../Storage/Admin/Dump/DumpDiscordAction.php | 2 +- .../Storage/Admin/Dump/DumpDocumentAction.php | 2 +- .../Storage/Admin/Dump/DumpListAction.php | 2 +- .../Storage/Admin/Dump/DumpUserAction.php | 2 +- .../Storage/Admin/Dump/DumpWikiAction.php | 2 +- .../Commands/Database/DatabaseSyncCommand.php | 27 ++++++++++++++++--- app/GraphQL/Filter/EnumFilter.php | 16 ++++++----- 11 files changed, 48 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 2e537112d..6ac57b51d 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,9 @@ composer install # Set a value for `APP_KEY` php artisan key:generate +# Create the database +mysql -h localhost -u root -p -e "CREATE DATABASE IF NOT EXISTS ``animethemes`` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" + # Import dumps automatically, migrate the database and run seeders php artisan db:sync ``` diff --git a/app/Actions/Storage/Admin/Dump/DumpAction.php b/app/Actions/Storage/Admin/Dump/DumpAction.php index e908e3ca7..af4976b15 100644 --- a/app/Actions/Storage/Admin/Dump/DumpAction.php +++ b/app/Actions/Storage/Admin/Dump/DumpAction.php @@ -115,7 +115,7 @@ abstract class DumpAction $dumper = Sqlite::create(); $dumper->setDbName($connection->getDatabaseName()); - $dumper->includeTables($this->allowedTables()); + $dumper->includeTables(static::allowedTables()); return $dumper; } @@ -146,7 +146,7 @@ abstract class DumpAction $dumper->setPort(intval($port)); } - $dumper->includeTables($this->allowedTables()); + $dumper->includeTables(static::allowedTables()); if ($this->option('comments')) { $dumper->dontSkipComments(); @@ -226,7 +226,7 @@ abstract class DumpAction $dumper->setPort($port); } - $dumper->includeTables($this->allowedTables()); + $dumper->includeTables(static::allowedTables()); if ($this->option('inserts')) { $dumper->useInserts(); @@ -265,5 +265,5 @@ abstract class DumpAction /** * The list of tables to include in the dump. */ - abstract protected function allowedTables(): array; + abstract public static function allowedTables(): array; } diff --git a/app/Actions/Storage/Admin/Dump/DumpAdminAction.php b/app/Actions/Storage/Admin/Dump/DumpAdminAction.php index a109d8999..a41810254 100644 --- a/app/Actions/Storage/Admin/Dump/DumpAdminAction.php +++ b/app/Actions/Storage/Admin/Dump/DumpAdminAction.php @@ -23,7 +23,7 @@ class DumpAdminAction extends DumpAction /** * The list of tables to include in the dump. */ - protected function allowedTables(): array + public static function allowedTables(): array { return [ 'action_events', // Nova events diff --git a/app/Actions/Storage/Admin/Dump/DumpAuthAction.php b/app/Actions/Storage/Admin/Dump/DumpAuthAction.php index 7c6879868..7ec04cc85 100644 --- a/app/Actions/Storage/Admin/Dump/DumpAuthAction.php +++ b/app/Actions/Storage/Admin/Dump/DumpAuthAction.php @@ -22,7 +22,7 @@ class DumpAuthAction extends DumpAction /** * The list of tables to include in the dump. */ - protected function allowedTables(): array + public static function allowedTables(): array { return [ // This table stores tokens which are sensitive data. diff --git a/app/Actions/Storage/Admin/Dump/DumpDiscordAction.php b/app/Actions/Storage/Admin/Dump/DumpDiscordAction.php index a762e9c7b..72d59fb9e 100644 --- a/app/Actions/Storage/Admin/Dump/DumpDiscordAction.php +++ b/app/Actions/Storage/Admin/Dump/DumpDiscordAction.php @@ -19,7 +19,7 @@ class DumpDiscordAction extends DumpAction /** * The list of tables to include in the dump. */ - protected function allowedTables(): array + public static function allowedTables(): array { return [ DiscordThread::TABLE, diff --git a/app/Actions/Storage/Admin/Dump/DumpDocumentAction.php b/app/Actions/Storage/Admin/Dump/DumpDocumentAction.php index 491cc0901..8e07a6b72 100644 --- a/app/Actions/Storage/Admin/Dump/DumpDocumentAction.php +++ b/app/Actions/Storage/Admin/Dump/DumpDocumentAction.php @@ -19,7 +19,7 @@ class DumpDocumentAction extends DumpAction /** * The list of tables to include in the dump. */ - protected function allowedTables(): array + public static function allowedTables(): array { return [ Page::TABLE, diff --git a/app/Actions/Storage/Admin/Dump/DumpListAction.php b/app/Actions/Storage/Admin/Dump/DumpListAction.php index d4b0c64cb..f1a504b4e 100644 --- a/app/Actions/Storage/Admin/Dump/DumpListAction.php +++ b/app/Actions/Storage/Admin/Dump/DumpListAction.php @@ -22,7 +22,7 @@ class DumpListAction extends DumpAction /** * The list of tables to include in the dump. */ - protected function allowedTables(): array + public static function allowedTables(): array { return [ ExternalEntry::TABLE, diff --git a/app/Actions/Storage/Admin/Dump/DumpUserAction.php b/app/Actions/Storage/Admin/Dump/DumpUserAction.php index dcf496a00..cec348aa0 100644 --- a/app/Actions/Storage/Admin/Dump/DumpUserAction.php +++ b/app/Actions/Storage/Admin/Dump/DumpUserAction.php @@ -23,7 +23,7 @@ class DumpUserAction extends DumpAction /** * The list of tables to include in the dump. */ - protected function allowedTables(): array + public static function allowedTables(): array { return [ Like::TABLE, diff --git a/app/Actions/Storage/Admin/Dump/DumpWikiAction.php b/app/Actions/Storage/Admin/Dump/DumpWikiAction.php index f3db15f92..2ae57e84c 100644 --- a/app/Actions/Storage/Admin/Dump/DumpWikiAction.php +++ b/app/Actions/Storage/Admin/Dump/DumpWikiAction.php @@ -41,7 +41,7 @@ class DumpWikiAction extends DumpAction /** * The list of tables to include in the dump. */ - protected function allowedTables(): array + public static function allowedTables(): array { return [ Anime::TABLE, diff --git a/app/Console/Commands/Database/DatabaseSyncCommand.php b/app/Console/Commands/Database/DatabaseSyncCommand.php index 578ded9ab..66e522c70 100644 --- a/app/Console/Commands/Database/DatabaseSyncCommand.php +++ b/app/Console/Commands/Database/DatabaseSyncCommand.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace App\Console\Commands\Database; +use App\Actions\Storage\Admin\Dump\DumpDocumentAction; +use App\Actions\Storage\Admin\Dump\DumpWikiAction; use App\Console\Commands\BaseCommand; use Database\Seeders\Admin\Feature\FeatureSeeder; use Database\Seeders\Auth\Permission\PermissionSeeder; @@ -18,7 +20,8 @@ use Illuminate\Support\Facades\Validator as ValidatorFacade; class DatabaseSyncCommand extends BaseCommand { - protected $signature = 'db:sync'; + protected $signature = 'db:sync + {--drop : Determine whether the existing database should be re-created}'; protected $description = 'Sync the local database with the latest dumps'; @@ -32,8 +35,26 @@ class DatabaseSyncCommand extends BaseCommand $database = Schema::getConnection()->getDatabaseName(); - Schema::dropDatabaseIfExists($database); - Schema::createDatabase($database); + if ($this->option('drop')) { + $this->info("Dropping database {$database}"); + Schema::dropDatabaseIfExists($database); + + $this->info("Creating database {$database}"); + Schema::createDatabase($database); + } + + if (! $this->option('drop')) { + Schema::withoutForeignKeyConstraints(function () { + foreach ([ + ...DumpDocumentAction::allowedTables(), + ...DumpWikiAction::allowedTables(), + ] as $table) { + $this->info("Truncating table {$table}"); + DB::table($table)->truncate(); + } + }); + } + DB::statement("USE `{$database}`"); $this->info('Importing wiki dump'); diff --git a/app/GraphQL/Filter/EnumFilter.php b/app/GraphQL/Filter/EnumFilter.php index 87729ac64..f049ac8b9 100644 --- a/app/GraphQL/Filter/EnumFilter.php +++ b/app/GraphQL/Filter/EnumFilter.php @@ -37,14 +37,18 @@ class EnumFilter extends Filter $values = []; foreach ($filterValues as $filterValue) { - if (! $filterValue instanceof BackedEnum) { - $enum = Arr::first( - $this->enumClass::cases(), - fn (UnitEnum $enum): bool => $enum->name === $filterValue - ); + if ($filterValue instanceof BackedEnum) { + $values[] = $filterValue->value; + continue; } - $values[] = $enum?->value ?? $filterValue->value; + /** @var BackedEnum $enum */ + $enum = Arr::first( + $this->enumClass::cases(), + fn (UnitEnum $enum): bool => $enum->name === $filterValue + ); + + $values[] = $enum->value; } return $values;