This repository has been archived on 2026-07-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
DataMigration/files/custom/Espo/Modules/DataMigration/Api/Import.php
T
chaim a8c4a2ebe1 feat: add skip attachment files option for import
Allows importing only database records without restoring attachment
files. Useful when migrating data between instances.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 20:06:12 +00:00

70 lines
1.9 KiB
PHP

<?php
namespace Espo\Modules\DataMigration\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Entities\User;
use Espo\Modules\DataMigration\Services\ImportService;
use Espo\Modules\DataMigration\Services\BufferedIO;
class Import implements Action
{
public function __construct(
private ImportService $importService,
private User $user
) {}
public function process(Request $request): Response
{
if (!$this->user->isAdmin()) {
throw new Forbidden();
}
$body = $request->getParsedBody();
$backupName = $body->backupName ?? null;
if (!$backupName) {
throw new BadRequest('backupName is required.');
}
$inputDir = 'data/backups/' . basename($backupName);
if (!is_dir($inputDir)) {
throw new BadRequest("Backup not found: {$backupName}");
}
if (!file_exists($inputDir . '/manifest.json')) {
throw new BadRequest("Invalid backup: no manifest.json found.");
}
$preserveIds = $body->preserveIds ?? true;
$skipDuplicates = $body->skipDuplicates ?? true;
$dryRun = $body->dryRun ?? false;
$noFiles = $body->noFiles ?? false;
$io = new BufferedIO();
$this->importService->import($inputDir, [
'batchSize' => 500,
'preserveIds' => $preserveIds,
'skipDuplicates' => $skipDuplicates,
'dryRun' => $dryRun,
'noFiles' => $noFiles,
'verbose' => true,
], $io);
return ResponseComposer::json([
'success' => true,
'backupName' => $backupName,
'dryRun' => $dryRun,
'log' => $io->getLines(),
]);
}
}