From c49acd74deddeadb29c1e3f4b62161f804ab5749 Mon Sep 17 00:00:00 2001 From: Alex Lingris Date: Mon, 18 May 2026 19:56:11 +0300 Subject: [PATCH] imporve import export --- src/Commands/ExportCommand.php | 53 ++++++++++++++++++++++++---------- src/Commands/ImportCommand.php | 13 +++++---- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/Commands/ExportCommand.php b/src/Commands/ExportCommand.php index 9ebefae..e09c3c8 100644 --- a/src/Commands/ExportCommand.php +++ b/src/Commands/ExportCommand.php @@ -4,12 +4,14 @@ namespace Lucent\Commands; use Illuminate\Console\Command; use Lucent\File\FileService; +use Lucent\ResultType\Error; +use Lucent\ResultType\Result; +use Lucent\ResultType\Success; use ZipArchive; class ExportCommand extends Command { protected $signature = "lucent:export"; - protected $prefix = "lucent_"; protected $description = "Export data and files"; @@ -32,8 +34,30 @@ class ExportCommand extends Command $stamp = now()->format("Y_m_d_His"); $sqlFile = $exportDir . "/dump_{$stamp}.sql"; $zipFile = $exportDir . "/export_{$stamp}.zip"; + $filesDir = $fileService->loadPublicDisk()->path("lucent/files"); - // Dump database + $result = $this->dumpDatabase($db, $tables, $sqlFile)->flatMap( + fn($sql) => $this->buildZip($sql, $filesDir, $zipFile), + ); + + if (file_exists($sqlFile)) { + unlink($sqlFile); + } + + if ($result->error()->isDefined()) { + $this->error($result->error()->get()); + return; + } + + $this->info("Exported to {$zipFile}"); + } + + /** @return Result */ + private function dumpDatabase( + array $db, + array $tables, + string $sqlFile, + ): Result { $tableArgs = collect($tables)->map(fn($t) => "-t {$t}")->join(" "); $command = sprintf( "PGPASSWORD=%s pg_dump -h %s -p %s -U %s -d %s %s --no-owner --no-acl > %s", @@ -49,27 +73,29 @@ class ExportCommand extends Command exec($command, result_code: $code); if ($code !== 0) { - $this->error("pg_dump failed"); - return; + return Error::create("pg_dump failed."); } $this->info("Database dumped."); - // Zip SQL + files - $publicDisk = $fileService->loadPublicDisk(); - - $filesDir = $publicDisk->path("lucent/files"); + return Success::create($sqlFile); + } + /** @return Result */ + private function buildZip( + string $sqlFile, + string $filesDir, + string $zipFile, + ): Result { $zip = new ZipArchive(); if ( $zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true ) { - $this->error("Could not create zip archive."); - return; + return Error::create("Could not create zip archive."); } - $zip->addFile($sqlFile, "dump_{$stamp}.sql"); + $zip->addFile($sqlFile, basename($sqlFile)); if (is_dir($filesDir)) { $iterator = new \RecursiveIteratorIterator( @@ -91,9 +117,6 @@ class ExportCommand extends Command $zip->close(); - // Clean up originals - unlink($sqlFile); - - $this->info("Exported to {$zipFile}"); + return Success::create($zipFile); } } diff --git a/src/Commands/ImportCommand.php b/src/Commands/ImportCommand.php index a106ca2..7c983c6 100644 --- a/src/Commands/ImportCommand.php +++ b/src/Commands/ImportCommand.php @@ -110,9 +110,6 @@ class ImportCommand extends Command ); exec($truncateCmd, result_code: $truncateCode); - if ($truncateCode !== 0) { - return Error::create("Failed to truncate existing tables."); - } $restoreCmd = sprintf( "PGPASSWORD=%s psql -h %s -p %s -U %s -d %s -f %s", @@ -135,12 +132,16 @@ class ImportCommand extends Command } /** @return Result */ - private function restoreFiles(string $tempDir, FileService $fileService): Result - { + private function restoreFiles( + string $tempDir, + FileService $fileService, + ): Result { $srcFilesDir = $tempDir . "/files"; if (!is_dir($srcFilesDir)) { - $this->warn("No files directory found in archive — skipping file restore."); + $this->warn( + "No files directory found in archive — skipping file restore.", + ); return Success::create(null); }