Skip to content

Commit

Permalink
Delete children automatically through foreign key definition;
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-thomas committed Sep 20, 2024
1 parent 3497137 commit aca858c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
70 changes: 35 additions & 35 deletions migrations/2021_5_29_100000_create_resources_table.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('resources', function (Blueprint $table) {
$table->id();
return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('resources', function (Blueprint $table) {
$table->id();

$table->foreignId('parent_id')->nullable()->constrained()->cascadeOnDelete();
$table->foreignId('parent_id')->nullable()->constrained('resources')->cascadeOnDelete();

$table->string('title');
$table->string('directory')->nullable();
$table->string('file')->nullable();
$table->string('hls')->nullable();
$table->string('link')->nullable();
$table->string('extension', 50);
$table->text('options')->nullable();
$table->boolean('external')->default(false);
$table->string('title');
$table->string('directory')->nullable();
$table->string('file')->nullable();
$table->string('hls')->nullable();
$table->string('link')->nullable();
$table->string('extension', 50);
$table->text('options')->nullable();
$table->boolean('external')->default(false);

$table->timestamps();
});
}
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('resources');
}
};
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists('resources');
}
};
5 changes: 0 additions & 5 deletions src/Services/Actions/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public function run(): Resource
DB::beginTransaction();

try {
if ($this->model->children()->exists()) {
foreach ($this->model->children()->select('id', 'directory', 'external')->get() as $child) {
( new self($child) )->run();
}
}
$this->model->delete();
if (!$this->model->isExternal() and alicia_storage()->exists($this->model->directory)) {
alicia_storage()->deleteDirectory($this->model->directory);
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\DB;
use Orchestra\Testbench\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
Expand All @@ -21,6 +22,7 @@ protected function setUp(): void
{
parent::setUp();

DB::statement('PRAGMA foreign_keys = ON;'); // It's disabled in Sqlite by default
$this->loadMigrationsFrom(__DIR__.'/Core/migrations');
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/ResourceModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Hans\Alicia\Facades\Alicia;
use Hans\Alicia\Facades\Signature;
use Hans\Alicia\Models\Resource;
use Hans\Alicia\Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
use Spatie\Image\Exceptions\InvalidManipulation;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ResourceModelTest extends TestCase
Expand Down Expand Up @@ -286,4 +289,23 @@ public function isNotExternal(): void
self::assertTrue($model->isNotExternal());
self::assertFalse($model->isExternal());
}

/**
* @test
*
* @return void
* @throws InvalidManipulation
*/
public function childrenDeletedAutomatically(): void
{
$model = Alicia::upload(
UploadedFile::fake()->image('g-eazy.png', 1080, 1080)
)
->export([540 => 540, 480 => 480])
->getData();

Alicia::delete($model->get('parents')->pluck('id')->first());

self::assertEmpty(DB::table('resources')->get()->pluck('id'));
}
}

0 comments on commit aca858c

Please sign in to comment.