cleanup commands

This commit is contained in:
2024-10-05 18:48:38 +03:00
parent 52a1ec5c5a
commit bb27811ddf
14 changed files with 119 additions and 123 deletions
+40
View File
@@ -0,0 +1,40 @@
<?php
namespace Lucent\Commands;
use Illuminate\Console\Command;
use Lucent\Database\Database;
use Lucent\Edge\EdgeService;
use Lucent\Query\Query;
class EmptyTrash extends Command
{
protected $signature = 'lucent:empty-trash {--force}';
protected $description = 'Removes all trashed documents';
public function __construct()
{
parent::__construct();
}
public function handle(): void
{
$trashedIds = Database::make()->table("records")->select("id")->where("status", "trashed")->get()->pluck("id")->toArray();
$count = count($trashedIds);
if (!($this->option('force') || $this->confirm("$count trashed records found. Delete?"))) {
return;
}
$numOfRowsDeleted = Database::make()->table("records")->whereIn("id", $trashedIds)->delete();
Database::make()->table("revisions")->whereIn("recordId", $trashedIds)->delete();
Database::make()->table("edges")->whereIn("source", $trashedIds)->delete();
Database::make()->table("edges")->whereIn("target", $trashedIds)->delete();
$this->info("$numOfRowsDeleted records were deleted");
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ use Lucent\Schema\CollectionSchema;
class GenerateCollectionSchema extends Command
{
protected $signature = 'lucent:generate:collection {name}';
protected $signature = 'lucent:make:collection-schema {name}';
protected $description = 'Generate a lucent collection';
+1 -1
View File
@@ -9,7 +9,7 @@ use Lucent\Schema\FilesSchema;
class GenerateFileSchema extends Command
{
protected $signature = 'lucent:generate:file {name}';
protected $signature = 'lucent:make:file-schema {name}';
protected $description = 'Generate a lucent file schema';
+1 -1
View File
@@ -16,7 +16,7 @@ use Lucent\Schema\Type;
class RebuildThumbnails extends Command
{
protected $signature = 'lucent:rebuild:thumbnails';
protected $signature = 'lucent:rebuild-thumbnails';
protected $description = 'Rebuilds thumbnails for path';
+13 -7
View File
@@ -9,14 +9,12 @@ use Lucent\Query\Query;
class RemoveOrphanEdges extends Command
{
protected $signature = 'lucent:removeOrphanEdges';
protected $signature = 'lucent:remove-orphan-edges {--force}';
protected $description = 'Searches and remove orphan edges';
public function __construct(
)
public function __construct()
{
parent::__construct();
}
@@ -24,17 +22,25 @@ class RemoveOrphanEdges extends Command
public function handle(EdgeService $edgeService, Query $query): void
{
$edges = $edgeService->findAll();
foreach ($edges as $edge){
if (!($this->option('force') || $this->confirm("Delete orphan edges?"))) {
return;
}
$edges = $edgeService->findAll();
$counter = 0;
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()){
if ($source->isEmpty() || $target->isEmpty()) {
$this->info("Edge is orphan");
$edgeService->remove($edge);
$counter++;
}
}
$this->info("$counter edges were deleted");
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace Lucent\Commands;
use Illuminate\Console\Command;
use Lucent\Channel\ChannelService;
use Lucent\Database\Database;
use Lucent\Edge\EdgeService;
use Lucent\Query\Query;
class RemoveOrphanRecords extends Command
{
protected $signature = 'lucent:remove-orphan-records {--force}';
protected $description = 'Searches and removes records without an active schema';
public function __construct()
{
parent::__construct();
}
public function handle(ChannelService $channelService): void
{
$schemas = $channelService->channel->schemas->pluck("name");
$count = Database::make()->table("revisions")->whereNotIn("schema",$schemas)->count();
if (!($this->option('force') || $this->confirm("$count records found. Delete?"))) {
return;
}
$numOfRowsDeleted = Database::make()->table("records")->whereNotIn("schema",$schemas)->delete();
Database::make()->table("revisions")->whereNotIn("schema",$schemas)->delete();
$this->info("$numOfRowsDeleted records were deleted");
}
}
+1 -7
View File
@@ -5,14 +5,12 @@ namespace Lucent\Http\Controller;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Lucent\Account\AccountService;
use Lucent\Account\AuthService;
use Lucent\Channel\ChannelService;
use Lucent\LucentException;
use Lucent\Query\Operator\OperatorRegistry;
use Lucent\Query\Query;
use Lucent\Record\InputData\EdgeInputData;
use Lucent\Record\InputData\RecordInputData;
use Lucent\Record\Manager;
use Lucent\Record\QueryRecord;
use Lucent\Record\RecordService;
use Lucent\Record\Status;
@@ -31,7 +29,6 @@ class RecordController extends Controller
private readonly ChannelService $channelService,
private readonly Svelte $svelte,
private readonly Query $query,
private readonly Manager $recordManager,
private readonly OperatorRegistry $operatorRegistry
)
{
@@ -174,7 +171,6 @@ class RecordController extends Controller
}
$schema = $this->channelService->channel->schemas->where("name", $request->input("schema"))->first();
$recordHistory = $this->recordManager->fromSession($request->session())->getRecords();
$record = $this->recordService->createEmpty($schema);
$queryRecord = QueryRecord::fromRecord($record);
return $this->svelte->render(
@@ -184,7 +180,6 @@ class RecordController extends Controller
data: [
"schema" => $schema,
"record" => $queryRecord,
"recordHistory" => $recordHistory,
"isCreateMode" => true,
"isWritable" => in_array($record->schema, $this->accountService->currentWritableSchemas())
]
@@ -238,6 +233,7 @@ class RecordController extends Controller
$record = $graph->records->first();
if (!in_array($record->schema, $this->accountService->currentReadableSchemas())) {
return $this->svelte->render(
layout: "channel",
@@ -247,7 +243,6 @@ class RecordController extends Controller
}
$schema = $this->channelService->getSchema($record->schema)->get();
$recordHistory = $this->recordManager->fromSession($request->session())->push($rid)->getRecords($rid);
return $this->svelte->render(
layout: "channel",
view: "recordEdit",
@@ -257,7 +252,6 @@ class RecordController extends Controller
"graph" => toArray($graph),
"record" => toArray($record),
"users" => $this->accountService->all(),
"recordHistory" => $recordHistory,
"isWritable" => in_array($record->schema, $this->accountService->currentWritableSchemas())
]
);
+4
View File
@@ -9,10 +9,12 @@ use Illuminate\Support\ServiceProvider;
use Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService;
use Lucent\Commands\CompileSchemas;
use Lucent\Commands\EmptyTrash;
use Lucent\Commands\GenerateCollectionSchema;
use Lucent\Commands\GenerateFileSchema;
use Lucent\Commands\RebuildThumbnails;
use Lucent\Commands\RemoveOrphanEdges;
use Lucent\Commands\RemoveOrphanRecords;
use Lucent\Commands\Setup;
use Lucent\Commands\SetupDatabase;
use Lucent\Commands\UpgradeFiles122;
@@ -80,10 +82,12 @@ class LucentServiceProvider extends ServiceProvider
CompileSchemas::class,
RebuildThumbnails::class,
RemoveOrphanEdges::class,
RemoveOrphanRecords::class,
SetupDatabase::class,
GenerateCollectionSchema::class,
GenerateFileSchema::class,
UpgradeFiles122::class,
EmptyTrash::class,
]);
}
-93
View File
@@ -1,93 +0,0 @@
<?php
namespace Lucent\Record;
use Illuminate\Contracts\Session\Session;
use Lucent\Query\Query;
class Manager
{
public array $records = [];
public Session $session;
public function fromSession(Session $session): Manager
{
$this->session = $session;
$this->records = $session->get("manager") ?? [];
return $this;
}
public function __construct(
private readonly Query $query
)
{
}
/**
* @param string[] $records
*/
public function addRecords(array $records): Manager
{
$this->records = $records;
return $this;
}
public function push(string $recordId): Manager
{
$records = $this->getIdsExcept($recordId);
$records[] = $recordId;
$records = array_unique($records);
$records = array_values($records);
$records = array_slice($records, -5);
$records = array_values($records);
$this->records = $records;
$this->save();
return $this;
}
private function save(): void
{
$this->session->put("manager", $this->records);
}
public function getIdsExcept(?string $id): array
{
return collect($this->records)->filter(fn($arec) => $arec !== $id)->values()->toArray();
}
/**
* @param QueryRecord[] $records
* @return QueryRecord[] $records
*/
public function order(array $records): array
{
$recordsById = collect($records)->keyBy("id")->toArray();
return collect($this->records)->reverse()->values()->reduce(function ($carry, $arecId) use ($recordsById) {
if (isset($recordsById[$arecId])) {
$carry[] = $recordsById[$arecId];
}
return $carry;
}, []);
}
public function getRecords(?string $ignoreId = null): array
{
$graph = $this->query
->filter(["id_in" => $this->getIdsExcept($ignoreId)])
->limit(7)
->run();
return $this->order($graph->records->toArray());
}
}