From 03ddcaa3fab6ee373ecdb7c96a68f2b9d62b6dc3 Mon Sep 17 00:00:00 2001 From: DrJKL Date: Thu, 29 Jan 2026 01:35:56 -0800 Subject: [PATCH] Refactor _prune_orphaned_assets for readability Amp-Thread-ID: https://ampcode.com/threads/T-019c0917-0dc3-75ab-870d-a32b3fdc1927 Co-authored-by: Amp --- app/assets/scanner.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/assets/scanner.py b/app/assets/scanner.py index 83bf5cac3..0172a5c2f 100644 --- a/app/assets/scanner.py +++ b/app/assets/scanner.py @@ -107,11 +107,12 @@ def _prune_orphaned_assets(roots: tuple[RootType, ...]) -> int: if not all_prefixes: return 0 - prefix_conds = [ - AssetCacheState.file_path.like(escaped + "%", escape=esc) - for p in all_prefixes - for escaped, esc in [escape_like_prefix(p if p.endswith(os.sep) else p + os.sep)] - ] + def make_prefix_condition(prefix: str): + base = prefix if prefix.endswith(os.sep) else prefix + os.sep + escaped, esc = escape_like_prefix(base) + return AssetCacheState.file_path.like(escaped + "%", escape=esc) + + matches_valid_prefix = sqlalchemy.or_(*[make_prefix_condition(p) for p in all_prefixes]) orphan_subq = ( sqlalchemy.select(Asset.id) @@ -120,7 +121,7 @@ def _prune_orphaned_assets(roots: tuple[RootType, ...]) -> int: ).scalar_subquery() 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))) result = sess.execute(sqlalchemy.delete(Asset).where(Asset.id.in_(orphan_subq))) sess.commit()