a8c4a2ebe1
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>
70 lines
1.9 KiB
PHP
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(),
|
|
]);
|
|
}
|
|
}
|