remove orphan edges
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Lucent\Edge\EdgeService;
|
||||
use Lucent\Query\Query;
|
||||
|
||||
class RemoveOrphanEdges extends Command
|
||||
{
|
||||
|
||||
protected $signature = 'lucent:removeOrphanEdges';
|
||||
|
||||
protected $description = 'Searches and remove orphan edges';
|
||||
|
||||
|
||||
public function __construct(
|
||||
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
public function handle(EdgeService $edgeService, Query $query): void
|
||||
{
|
||||
$edges = $edgeService->findAll();
|
||||
|
||||
foreach ($edges as $edge){
|
||||
$source = $query->filter(["id" => $edge->source])->run()->records;
|
||||
$target = $query->filter(["id" => $edge->target])->run()->records;
|
||||
if($source->isEmpty() || $target->isEmpty()){
|
||||
$this->info("Edge is orphan");
|
||||
$edgeService->remove($edge);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+38
-2
@@ -3,12 +3,16 @@
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Lucent\LucentException;
|
||||
use PDOException;
|
||||
use stdClass;
|
||||
|
||||
class EdgeRepo
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function insert(Edge $edge): void
|
||||
public function insert(Edge $edge): void
|
||||
{
|
||||
try {
|
||||
DB::table("edges")->insert($edge->toDB());
|
||||
@@ -21,7 +25,7 @@ class EdgeRepo
|
||||
|
||||
}
|
||||
|
||||
public static function update(string $from, EdgeCollection $edges): void
|
||||
public function update(string $from, EdgeCollection $edges): void
|
||||
{
|
||||
$edgesDB = collect($edges)->map(fn($e) => $e->toDB())->toArray();
|
||||
DB::table("edges")->where("source", $from)->delete();
|
||||
@@ -29,4 +33,36 @@ class EdgeRepo
|
||||
}
|
||||
|
||||
|
||||
public function findAll(): EdgeCollection
|
||||
{
|
||||
$edges = DB::table("edges")->get();
|
||||
return new EdgeCollection(...$edges->map([$this, 'mapEdge'])->toArray());
|
||||
}
|
||||
|
||||
public function mapEdge(stdClass $edge): Edge
|
||||
{
|
||||
|
||||
return new Edge(
|
||||
source: $edge->source,
|
||||
target: $edge->target,
|
||||
sourceSchema: $edge->sourceSchema,
|
||||
targetSchema: $edge->targetSchema,
|
||||
field: $edge->field,
|
||||
rank: $edge->rank,
|
||||
depth: $edge->depth ?? 0
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public function remove(Edge $edge): void
|
||||
{
|
||||
DB::table("edges")
|
||||
->where("source", $edge->source)
|
||||
->where("target", $edge->target)
|
||||
->where("sourceSchema", $edge->sourceSchema)
|
||||
->where("targetSchema", $edge->targetSchema)
|
||||
->where("field", $edge->field)
|
||||
->delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,12 +4,14 @@ use Lucent\LucentException;
|
||||
|
||||
class EdgeService
|
||||
{
|
||||
|
||||
public function __construct(public EdgeRepo $edgeRepo)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LucentException
|
||||
*/
|
||||
public static function create(
|
||||
public function create(
|
||||
string $source,
|
||||
string $target,
|
||||
string $sourceSchema,
|
||||
@@ -29,9 +31,23 @@ class EdgeService
|
||||
field: $field,
|
||||
rank: $rank,
|
||||
);
|
||||
EdgeRepo::insert($edge);
|
||||
$this->edgeRepo->insert($edge);
|
||||
return $edge;
|
||||
}
|
||||
|
||||
public function update(string $from, EdgeCollection $edges): void
|
||||
{
|
||||
$this->edgeRepo->update($from, $edges);
|
||||
}
|
||||
|
||||
public function findAll(): EdgeCollection
|
||||
{
|
||||
return $this->edgeRepo->findAll();
|
||||
}
|
||||
|
||||
public function remove(Edge $edge): void
|
||||
{
|
||||
$this->edgeRepo->remove($edge);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ use Lucent\Channel\ChannelService;
|
||||
use Lucent\Commands\CompileSchemas;
|
||||
use Lucent\Commands\LiveLink;
|
||||
use Lucent\Commands\RebuildThumbnails;
|
||||
use Lucent\Commands\RemoveOrphanEdges;
|
||||
use Lucent\File\FileService;
|
||||
use Lucent\File\ImageService;
|
||||
use Lucent\Query\DatabaseGraph\DatabaseGraph;
|
||||
@@ -68,6 +69,7 @@ class LucentServiceProvider extends ServiceProvider
|
||||
CompileSchemas::class,
|
||||
RebuildThumbnails::class,
|
||||
LiveLink::class,
|
||||
RemoveOrphanEdges::class,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ use Lucent\Account\AuthService;
|
||||
use Lucent\Channel\ChannelService;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Edge\EdgeCollection;
|
||||
use Lucent\Edge\EdgeRepo;
|
||||
use Lucent\Edge\EdgeService;
|
||||
use Lucent\File\FileService;
|
||||
use Lucent\Id\Id;
|
||||
use Lucent\LucentException;
|
||||
@@ -28,7 +28,8 @@ readonly class RecordService
|
||||
private Validator $recordValidator,
|
||||
private Query $query,
|
||||
private InputFormatter $inputFormatter,
|
||||
private RecordRepo $recordRepo
|
||||
private RecordRepo $recordRepo,
|
||||
private EdgeService $edgeService
|
||||
)
|
||||
{
|
||||
}
|
||||
@@ -66,7 +67,8 @@ readonly class RecordService
|
||||
$uniqueEdgesCollection = EdgeCollection::fromArray($uniqueEdges);
|
||||
|
||||
if ($uploadResult->isDuplicate) {
|
||||
EdgeRepo::update($uploadResult->duplicateId, $uniqueEdgesCollection);
|
||||
|
||||
$this->edgeService->update($uploadResult->duplicateId, $uniqueEdgesCollection);
|
||||
return $uploadResult->duplicateId;
|
||||
}
|
||||
|
||||
@@ -87,8 +89,8 @@ readonly class RecordService
|
||||
}
|
||||
|
||||
RecordRepo::create($record);
|
||||
EdgeRepo::update($record->id, $uniqueEdgesCollection);
|
||||
$this->revisionService->create($record,$uniqueEdgesCollection);
|
||||
$this->edgeService->update($record->id, $uniqueEdgesCollection);
|
||||
$this->revisionService->create($record, $uniqueEdgesCollection);
|
||||
return $record->id;
|
||||
|
||||
}
|
||||
@@ -150,10 +152,10 @@ readonly class RecordService
|
||||
|
||||
RecordRepo::update($newRecord);
|
||||
if ($updateEdges) {
|
||||
EdgeRepo::update($newRecord->id, $uniqueEdgesCollection);
|
||||
$this->edgeService->update($newRecord->id, $uniqueEdgesCollection);
|
||||
}
|
||||
|
||||
$this->revisionService->create($newRecord,$uniqueEdgesCollection);
|
||||
$this->revisionService->create($newRecord, $uniqueEdgesCollection);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user