imporve import export
This commit is contained in:
@@ -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<string, string> */
|
||||
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<string, string> */
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<null, string> */
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user