Add command to sync the database notes to the disk

This commit is contained in:
brufdev
2025-02-13 13:51:52 +00:00
parent 68c5240ba3
commit 60e246e855
2 changed files with 75 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ final class RunCommand extends Command
{
$this->init();
$this->processLinks();
$this->syncDatabaseNotes();
}
private function init(): void
@@ -58,4 +59,20 @@ final class RunCommand extends Command
'executed' => 1,
]);
}
private function syncDatabaseNotes(): void
{
/** @var object{executed: int} $upgrades */
$upgrades = DB::table('upgrades')->first();
if ($upgrades->executed !== 1) {
return;
}
$this->call('upgrade:sync-database-notes');
DB::table('upgrades')->update([
'executed' => 2,
]);
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Console\Commands\Upgrade;
use App\Actions\GetPathFromVaultNode;
use App\Models\Vault;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Throwable;
final class SyncDatabaseNotesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'upgrade:sync-database-notes';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync database notes to disk';
/**
* Execute the console command.
*/
public function handle(GetPathFromVaultNode $getPathFromVaultNode): int
{
try {
$vaults = Vault::all();
foreach ($vaults as $vault) {
$nodes = $vault->nodes()
->where('is_file', true)
->where('extension', 'md')
->get();
foreach ($nodes as $node) {
$path = $getPathFromVaultNode->handle($node);
Storage::disk('local')->put($path, $node->content ?? '');
}
}
} catch (Throwable) {
$this->error('Something went wrong syncing database notes to disk');
return self::FAILURE;
}
$this->info('All notes were successfully synced to disk');
return self::SUCCESS;
}
}