mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-09-20 18:51:26 -03:00
2ceb1e2850
A LoRA named `lora-sd1.5-backlight_slider_v10.safetensors` showed up in the manager as `lora-sd1`, hid itself from searches for the rest of its name, and collapsed into the same lora syntax tag as every sibling sharing the prefix. The name was cut twice. `_process_model_file()` imports a third-party `.civitai.info` sidecar by handing `from_civitai_info()` the local stem with the extension already stripped, and the builder then stripped a second "extension" from it -- `os.path.splitext` reads everything after the last dot as one, so the version dot in `1.5` ended the name. The download path never hit this because API filenames keep their extension and only need one strip. Pass the real basename from the migration site, and make the builder strip only a recognized model extension (`strip_model_extension`), so both input shapes resolve to the same stem. The `model_name` fallback that reused the same expression is fixed with it: on a sidecar without `model.name` the display name was truncated too. Libraries already corrupted do not heal on their own: the incremental Refresh skips paths already in the cache (only a full rebuild reloads metadata) and startup hydrates rows from SQLite as-is, so the wrong name survives restarts. Reconcile now compares each cached row against the stem of its file path -- one string compare per file and no extra syscall, so a clean library pays nothing -- and repairs mismatching rows through `load_metadata()` (which normalizes the sidecar) and the existing in-place `_sync_cache_from_metadata_impl()` path, which writes a targeted single-row SQL delta instead of a full save. Repairs are one-shot, and a missing or corrupt sidecar keeps its row so a full rebuild can recreate it without losing tags or civitai data. Tests: the builder keeps dotted stems for all four model classes and still strips real extensions; the migration writes the full local name to the sidecar; and reconcile repairs memory, sidecar and SQLite row, runs exactly once, and never reads metadata on a clean library.
90 lines
2.5 KiB
Python
90 lines
2.5 KiB
Python
"""End-to-end regression for issue #1112.
|
|
|
|
Importing a third-party ``.civitai.info`` sidecar (a migration path) must keep
|
|
the local, dotted file name intact instead of cutting it at the model-version
|
|
dot (``lora-sd1.5-backlight_slider_v10`` -> ``lora-sd1``).
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from py.services import model_scanner
|
|
from py.services.lora_scanner import LoraScanner
|
|
from py.services.model_scanner import ModelScanner
|
|
|
|
DOTTED_STEM = "lora-sd1.5-backlight_slider_v10"
|
|
|
|
CIVITAI_INFO = {
|
|
"id": 12345,
|
|
"baseModel": "SD 1.5",
|
|
"name": "v1.0",
|
|
"model": {
|
|
"id": 999,
|
|
"name": "Light Control",
|
|
"type": "LORA",
|
|
"description": "",
|
|
"tags": ["lighting"],
|
|
},
|
|
"files": [
|
|
{
|
|
"id": 1,
|
|
# Remote name from the CivitAI payload; the local file was renamed.
|
|
"name": "backlight_slider_v10.safetensors",
|
|
"primary": True,
|
|
"sizeKB": 1024,
|
|
"hashes": {"SHA256": "a" * 64},
|
|
}
|
|
],
|
|
}
|
|
|
|
|
|
def _normalize(path: Path) -> str:
|
|
return str(path).replace(os.sep, "/")
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_model_scanner_singletons():
|
|
ModelScanner._instances.clear()
|
|
ModelScanner._locks.clear()
|
|
yield
|
|
ModelScanner._instances.clear()
|
|
ModelScanner._locks.clear()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_civitai_info_migration_keeps_dotted_local_name(tmp_path, monkeypatch):
|
|
loras_root = tmp_path / "loras"
|
|
loras_root.mkdir()
|
|
|
|
model_file = loras_root / f"{DOTTED_STEM}.safetensors"
|
|
model_file.write_text("fake lora weights", encoding="utf-8")
|
|
(loras_root / f"{DOTTED_STEM}.civitai.info").write_text(
|
|
json.dumps(CIVITAI_INFO), encoding="utf-8"
|
|
)
|
|
|
|
normalized_root = _normalize(loras_root)
|
|
monkeypatch.setattr(
|
|
model_scanner.config, "loras_roots", [normalized_root], raising=False
|
|
)
|
|
monkeypatch.setattr(
|
|
model_scanner.config, "extra_loras_roots", [], raising=False
|
|
)
|
|
|
|
scanner = LoraScanner()
|
|
entry = await scanner._process_model_file(_normalize(model_file), normalized_root)
|
|
|
|
assert entry is not None
|
|
assert entry["file_name"] == DOTTED_STEM
|
|
assert entry["model_name"] == "Light Control"
|
|
|
|
sidecar = loras_root / f"{DOTTED_STEM}.metadata.json"
|
|
assert sidecar.exists()
|
|
saved = json.loads(sidecar.read_text(encoding="utf-8"))
|
|
assert saved["file_name"] == DOTTED_STEM
|
|
assert saved["model_name"] == "Light Control"
|
|
# The migration must not silently drop the CivitAI payload.
|
|
assert saved["civitai"]["name"] == "v1.0"
|