imporve import export
This commit is contained in:
@@ -4,12 +4,14 @@ namespace Lucent\Commands;
|
|||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Lucent\File\FileService;
|
use Lucent\File\FileService;
|
||||||
|
use Lucent\ResultType\Error;
|
||||||
|
use Lucent\ResultType\Result;
|
||||||
|
use Lucent\ResultType\Success;
|
||||||
use ZipArchive;
|
use ZipArchive;
|
||||||
|
|
||||||
class ExportCommand extends Command
|
class ExportCommand extends Command
|
||||||
{
|
{
|
||||||
protected $signature = "lucent:export";
|
protected $signature = "lucent:export";
|
||||||
protected $prefix = "lucent_";
|
|
||||||
|
|
||||||
protected $description = "Export data and files";
|
protected $description = "Export data and files";
|
||||||
|
|
||||||
@@ -32,8 +34,30 @@ class ExportCommand extends Command
|
|||||||
$stamp = now()->format("Y_m_d_His");
|
$stamp = now()->format("Y_m_d_His");
|
||||||
$sqlFile = $exportDir . "/dump_{$stamp}.sql";
|
$sqlFile = $exportDir . "/dump_{$stamp}.sql";
|
||||||
$zipFile = $exportDir . "/export_{$stamp}.zip";
|
$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(" ");
|
$tableArgs = collect($tables)->map(fn($t) => "-t {$t}")->join(" ");
|
||||||
$command = sprintf(
|
$command = sprintf(
|
||||||
"PGPASSWORD=%s pg_dump -h %s -p %s -U %s -d %s %s --no-owner --no-acl > %s",
|
"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);
|
exec($command, result_code: $code);
|
||||||
|
|
||||||
if ($code !== 0) {
|
if ($code !== 0) {
|
||||||
$this->error("pg_dump failed");
|
return Error::create("pg_dump failed.");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->info("Database dumped.");
|
$this->info("Database dumped.");
|
||||||
|
|
||||||
// Zip SQL + files
|
return Success::create($sqlFile);
|
||||||
$publicDisk = $fileService->loadPublicDisk();
|
}
|
||||||
|
|
||||||
$filesDir = $publicDisk->path("lucent/files");
|
|
||||||
|
|
||||||
|
/** @return Result<string, string> */
|
||||||
|
private function buildZip(
|
||||||
|
string $sqlFile,
|
||||||
|
string $filesDir,
|
||||||
|
string $zipFile,
|
||||||
|
): Result {
|
||||||
$zip = new ZipArchive();
|
$zip = new ZipArchive();
|
||||||
if (
|
if (
|
||||||
$zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !==
|
$zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !==
|
||||||
true
|
true
|
||||||
) {
|
) {
|
||||||
$this->error("Could not create zip archive.");
|
return Error::create("Could not create zip archive.");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$zip->addFile($sqlFile, "dump_{$stamp}.sql");
|
$zip->addFile($sqlFile, basename($sqlFile));
|
||||||
|
|
||||||
if (is_dir($filesDir)) {
|
if (is_dir($filesDir)) {
|
||||||
$iterator = new \RecursiveIteratorIterator(
|
$iterator = new \RecursiveIteratorIterator(
|
||||||
@@ -91,9 +117,6 @@ class ExportCommand extends Command
|
|||||||
|
|
||||||
$zip->close();
|
$zip->close();
|
||||||
|
|
||||||
// Clean up originals
|
return Success::create($zipFile);
|
||||||
unlink($sqlFile);
|
|
||||||
|
|
||||||
$this->info("Exported to {$zipFile}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,9 +110,6 @@ class ImportCommand extends Command
|
|||||||
);
|
);
|
||||||
|
|
||||||
exec($truncateCmd, result_code: $truncateCode);
|
exec($truncateCmd, result_code: $truncateCode);
|
||||||
if ($truncateCode !== 0) {
|
|
||||||
return Error::create("Failed to truncate existing tables.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$restoreCmd = sprintf(
|
$restoreCmd = sprintf(
|
||||||
"PGPASSWORD=%s psql -h %s -p %s -U %s -d %s -f %s",
|
"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> */
|
/** @return Result<null, string> */
|
||||||
private function restoreFiles(string $tempDir, FileService $fileService): Result
|
private function restoreFiles(
|
||||||
{
|
string $tempDir,
|
||||||
|
FileService $fileService,
|
||||||
|
): Result {
|
||||||
$srcFilesDir = $tempDir . "/files";
|
$srcFilesDir = $tempDir . "/files";
|
||||||
|
|
||||||
if (!is_dir($srcFilesDir)) {
|
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);
|
return Success::create(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user