feat(dev): make db:sync command smarter (#1046)

This commit is contained in:
Kyrch
2026-01-05 07:05:53 -03:00
committed by GitHub
parent 22918e3adb
commit 84cdc19fb0
11 changed files with 48 additions and 20 deletions
+3
View File
@@ -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
```
@@ -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;
}
@@ -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
@@ -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.
@@ -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,
@@ -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,
@@ -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,
@@ -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,
@@ -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,
@@ -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');
+10 -6
View File
@@ -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;