Execute storage migration copies
这个提交存在于:
父节点
859ef913ec
当前提交
eb70493a00
共有 2 个文件被更改,包括 39 次插入 和 12 次删除
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
|
@ -41,26 +42,23 @@ class VfsStorage:
|
|||
}
|
||||
|
||||
def migration_plan(self, source_uri: str, target_uri: str, *, dry_run: bool = True) -> dict[str, Any]:
|
||||
if not dry_run:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"storage migration execution is not enabled; run with dry_run=true",
|
||||
{"field": "dry_run"},
|
||||
)
|
||||
source_path = self.resolve_uri(source_uri)
|
||||
target_path = self.resolve_uri(target_uri)
|
||||
if not source_path.exists():
|
||||
raise ApiError(ErrorCode.MEDIA_NOT_FOUND, "source media not found", {"uri": source_uri})
|
||||
files, total_bytes = _path_file_stats(source_path)
|
||||
will_overwrite = target_path.exists()
|
||||
if not dry_run:
|
||||
_copy_path(source_path, target_path)
|
||||
return {
|
||||
"dry_run": True,
|
||||
"status": "planned",
|
||||
"dry_run": dry_run,
|
||||
"status": "planned" if dry_run else "completed",
|
||||
"source_uri": source_uri,
|
||||
"target_uri": target_uri,
|
||||
"files": files,
|
||||
"bytes": total_bytes,
|
||||
"will_overwrite": target_path.exists(),
|
||||
"execution_enabled": False,
|
||||
"will_overwrite": will_overwrite,
|
||||
"execution_enabled": True,
|
||||
}
|
||||
|
||||
def resolve_uri(self, uri: str) -> Path:
|
||||
|
|
@ -151,6 +149,27 @@ def _path_file_stats(path: Path) -> tuple[int, int]:
|
|||
return 0, 0
|
||||
|
||||
|
||||
def _copy_path(source_path: Path, target_path: Path) -> None:
|
||||
if source_path.is_file():
|
||||
target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(source_path, target_path)
|
||||
return
|
||||
|
||||
source_root = source_path.resolve()
|
||||
target_root = target_path.resolve()
|
||||
if target_root == source_root or source_root in target_root.parents:
|
||||
raise ApiError(ErrorCode.FORBIDDEN, "target path cannot be inside source directory")
|
||||
|
||||
target_root.mkdir(parents=True, exist_ok=True)
|
||||
for child in source_root.rglob("*"):
|
||||
if not child.is_file():
|
||||
continue
|
||||
relative = child.relative_to(source_root)
|
||||
destination = target_root / relative
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(child, destination)
|
||||
|
||||
|
||||
def _is_writable(path: Path) -> bool:
|
||||
target = path if path.exists() else path.parent
|
||||
return target.exists() and target.is_dir()
|
||||
|
|
|
|||
|
|
@ -694,8 +694,16 @@ database:
|
|||
},
|
||||
self.media_service,
|
||||
)
|
||||
self.assertFalse(execution["ok"])
|
||||
self.assertEqual(execution["error_code"], "VALIDATION_FAILED")
|
||||
self.assertTrue(execution["ok"])
|
||||
self.assertFalse(execution["data"]["dry_run"])
|
||||
self.assertEqual(execution["data"]["status"], "completed")
|
||||
self.assertEqual(execution["data"]["files"], 1)
|
||||
self.assertEqual(execution["data"]["bytes"], 5)
|
||||
self.assertTrue(execution["data"]["will_overwrite"])
|
||||
self.assertTrue(execution["data"]["execution_enabled"])
|
||||
self.assertEqual((self.root / "archives" / "sample.bin").read_bytes(), b"12345")
|
||||
self.assertEqual((self.root / "images" / "sample.bin").read_bytes(), b"12345")
|
||||
self.assertNotIn(str(self.root), str(execution["data"]))
|
||||
|
||||
def test_asset_import_pack_dry_run_is_safe(self):
|
||||
pack = self.root / "materials" / "pack"
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用