mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-01-31 16:50:17 +08:00
Refactor _prune_orphaned_assets for readability
Amp-Thread-ID: https://ampcode.com/threads/T-019c0917-0dc3-75ab-870d-a32b3fdc1927 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
parent
e7bebcc8d0
commit
03ddcaa3fa
@ -107,11 +107,12 @@ def _prune_orphaned_assets(roots: tuple[RootType, ...]) -> int:
|
|||||||
if not all_prefixes:
|
if not all_prefixes:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
prefix_conds = [
|
def make_prefix_condition(prefix: str):
|
||||||
AssetCacheState.file_path.like(escaped + "%", escape=esc)
|
base = prefix if prefix.endswith(os.sep) else prefix + os.sep
|
||||||
for p in all_prefixes
|
escaped, esc = escape_like_prefix(base)
|
||||||
for escaped, esc in [escape_like_prefix(p if p.endswith(os.sep) else p + os.sep)]
|
return AssetCacheState.file_path.like(escaped + "%", escape=esc)
|
||||||
]
|
|
||||||
|
matches_valid_prefix = sqlalchemy.or_(*[make_prefix_condition(p) for p in all_prefixes])
|
||||||
|
|
||||||
orphan_subq = (
|
orphan_subq = (
|
||||||
sqlalchemy.select(Asset.id)
|
sqlalchemy.select(Asset.id)
|
||||||
@ -120,7 +121,7 @@ def _prune_orphaned_assets(roots: tuple[RootType, ...]) -> int:
|
|||||||
).scalar_subquery()
|
).scalar_subquery()
|
||||||
|
|
||||||
with create_session() as sess:
|
with create_session() as sess:
|
||||||
sess.execute(sqlalchemy.delete(AssetCacheState).where(sqlalchemy.not_(sqlalchemy.or_(*prefix_conds))))
|
sess.execute(sqlalchemy.delete(AssetCacheState).where(~matches_valid_prefix))
|
||||||
sess.execute(sqlalchemy.delete(AssetInfo).where(AssetInfo.asset_id.in_(orphan_subq)))
|
sess.execute(sqlalchemy.delete(AssetInfo).where(AssetInfo.asset_id.in_(orphan_subq)))
|
||||||
result = sess.execute(sqlalchemy.delete(Asset).where(Asset.id.in_(orphan_subq)))
|
result = sess.execute(sqlalchemy.delete(Asset).where(Asset.id.in_(orphan_subq)))
|
||||||
sess.commit()
|
sess.commit()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user