diff --git a/alembic/versions/2026_09_20_1200_drop_file_group_idempotency.py b/alembic/versions/2026_09_20_1200_drop_file_group_idempotency.py new file mode 100644 index 00000000..f62e2e7d --- /dev/null +++ b/alembic/versions/2026_09_20_1200_drop_file_group_idempotency.py @@ -0,0 +1,24 @@ +"""Drop unused file_group.idempotency. + +FileWorker no longer stamps this column (refresh diffs in SQL). directory.idempotency +is unchanged. SQLite 3.35+ DROP COLUMN does not rebuild the table, so file_group_fts +(external content on file_group.id) is left intact. + +Revision ID: 2026_09_20_1200 +Revises: 2026_09_18_1200 +""" +from alembic import op + +# revision identifiers, used by Alembic. +revision = '2026_09_20_1200' +down_revision = '2026_09_18_1200' +branch_labels = None +depends_on = None + + +def upgrade(): + op.execute('ALTER TABLE file_group DROP COLUMN idempotency') + + +def downgrade(): + op.execute('ALTER TABLE file_group ADD COLUMN idempotency DATETIME') diff --git a/wrolpi/files/models.py b/wrolpi/files/models.py index 838f41b5..667abade 100644 --- a/wrolpi/files/models.py +++ b/wrolpi/files/models.py @@ -99,9 +99,6 @@ class FileGroup(ModelHelper, Base): data = Column(FancyJSON) # populated by the modeler download_datetime = Column(TZDateTime) # the date WROLPi downloaded this file. files = Column(FancyJSON) # populated during discovery - # Leftover from the idempotency-stamp refresh. FileWorker no longer writes this. - # directory.idempotency is still used. Do not drop: file_group is the FTS content table. - idempotency = Column(TZDateTime) indexed = Column(Boolean, default=lambda: False) # see indexing state machine on apply_indexers # Cached output of split_path_stem_and_suffix(primary_path). Identity of a group is # (directory, stem); primary_path is the preferred file inside the group. Recomputed on diff --git a/wrolpi/files/refresh.py b/wrolpi/files/refresh.py new file mode 100644 index 00000000..b58756d2 --- /dev/null +++ b/wrolpi/files/refresh.py @@ -0,0 +1,714 @@ +"""Discover filesystem FileGroups and compare them to the DB. + +Extracted from the file worker so refresh (find, SQL diff, classify) is not mixed +with move/reorganize/queue. FileWorker still orchestrates upsert/delete/model. + +Identity of a group is (directory, stem). Compare streams `find` into a TEMP +table and diffs in SQL so unchanged files are never stat()'d a second time. +""" +import asyncio +import json +import pathlib +import shlex +import uuid +from dataclasses import dataclass, field +from datetime import datetime, timezone +from pathlib import Path +from typing import AsyncGenerator, Callable, Dict, List, Set, Tuple + +from sqlalchemy import text + +from wrolpi.common import get_media_directory, logger +from wrolpi.db import get_db_session +from wrolpi.vars import PYTEST, IS_MACOS +from wrolpi.files.lib import ( + split_path_stem_and_suffix, + get_normalized_ignored_directories, + ensure_file_group_stems, +) + +logger = logger.getChild(__name__) + +async def _await_db(func, *args): + """Run a function that uses get_db_curs. + + Tests share one SQLite connection via get_db_curs. sqlite3 is not safe for concurrent + use of that connection, so asyncio.to_thread segfaults in CI (xdist + session commit + on the event loop while the worker thread is still in get_stem_algorithm_version / + _upsert_files). Production opens a dedicated raw connection and can use a thread. + """ + if PYTEST: + return func(*args) + return await asyncio.to_thread(func, *args) + + +# Maximum mtime difference (seconds) before a file is considered "modified". +# 0.5s accommodates SD card / FAT32 / exFAT timestamp granularity on Pi 4. +MTIME_TOLERANCE_SECONDS = 0.5 + +# GNU find -printf format. `\0` here is the two-character find escape, NOT a Python +# NUL: subprocess arguments cannot contain embedded null bytes. +GNU_FIND_PRINTF = r'%h\0%f\0%T@\0' + +__all__ = [ + 'FileGroupDiff', + 'FileComparisonResult', + 'classify_file_group_diffs', + 'compare_file_groups', + 'count_files', + 'count_files_with_progress', + 'find_directories', + 'GNU_FIND_PRINTF', +] + + +@dataclass +class FileGroupDiff: + """Represents changes needed for a FileGroup.""" + directory: Path | str + stem: str + db_files: Set[str] = field(default_factory=set) # Filenames in DB + fs_files: Set[str] = field(default_factory=set) # Filenames on filesystem + file_group_id: int | None = None + + @property + def added_files(self) -> Set[str]: + """Files on disk but not in DB.""" + return self.fs_files - self.db_files + + @property + def removed_files(self) -> Set[str]: + """Files in DB but not on disk.""" + return self.db_files - self.fs_files + + @property + def is_new(self) -> bool: + """FileGroup doesn't exist in DB.""" + return self.file_group_id is None and bool(self.fs_files) + + @property + def is_deleted(self) -> bool: + """All files deleted from disk.""" + return not self.fs_files and bool(self.db_files) + + @property + def needs_update(self) -> bool: + """FileGroup exists but files changed (not completely deleted).""" + return (self.file_group_id is not None + and bool(self.added_files or self.removed_files) + and bool(self.fs_files)) # Not deleted - still has files on disk + + @property + def is_unchanged(self) -> bool: + """FileGroup matches filesystem exactly.""" + return self.db_files == self.fs_files and bool(self.db_files) + + +@dataclass +class FileComparisonResult: + """Result of comparing DB to filesystem at FileGroup level.""" + unchanged: list # FileGroupDiff where is_unchanged + new: list # FileGroupDiff where is_new + deleted: list # FileGroupDiff where is_deleted + modified: list # FileGroupDiff where needs_update + + +def _deduplicate_db_groups( + db_groups: Dict[Tuple[str, str], List[Tuple[int, Set[str], float]]], +) -> Tuple[Dict[Tuple[str, str], Tuple[int, Set[str], float]], List[FileGroupDiff]]: + """Pick one FileGroup per (directory, stem) key, mark the rest for deletion. + + Prefers the FG that has files (non-empty), then highest ID as a stable tiebreaker. + """ + deduplicated: Dict[Tuple[str, str], Tuple[int, Set[str], float]] = {} + duplicate_diffs: List[FileGroupDiff] = [] + for key, entries in db_groups.items(): + # Sort: prefer non-empty files first, then highest ID + entries.sort(key=lambda e: (bool(e[1]), e[0]), reverse=True) + deduplicated[key] = entries[0] + for fg_id, fg_files, _ in entries[1:]: + directory, stem = key + duplicate_diffs.append(FileGroupDiff( + directory=Path(directory), + stem=stem, + db_files=fg_files or {'__empty__'}, + fs_files=set(), + file_group_id=fg_id, + )) + return deduplicated, duplicate_diffs + + +def _filenames_from_files_json(raw) -> Set[str]: + """Filenames from a FileGroup.files JSON value (relative or absolute paths).""" + if not raw: + return set() + if isinstance(raw, str): + raw = json.loads(raw) + if not isinstance(raw, list): + return set() + names = set() + for entry in raw: + path = entry.get('path') if isinstance(entry, dict) else entry + if path: + names.add(Path(path).name) + return names + + +def classify_file_group_diffs( + fs_groups: Dict[Tuple[str, str], Set[str]], + db_groups: Dict[Tuple[str, str], List[Tuple[int, Set[str], float]]], + fs_mtimes: Dict[Tuple[str, str], float] | None = None, +) -> FileComparisonResult: + """Classify filesystem vs DB groups into new/deleted/modified/unchanged. + + `db_groups` may contain duplicates per (directory, stem); extras are marked deleted. + If `fs_mtimes` is provided (max mtime per key), unchanged-by-name groups with a newer + filesystem mtime are classified modified without a second stat(). If omitted, those + groups are stat()'d (used by the small direct-file path). + """ + deduplicated, duplicate_diffs = _deduplicate_db_groups(db_groups) + + unchanged = [] + new = [] + deleted = list(duplicate_diffs) + modified = [] + + all_keys = set(fs_groups.keys()) | set(deduplicated.keys()) + + for key in all_keys: + directory, stem = key + fs_files = fs_groups.get(key, set()) + db_id, db_files, db_mtime = deduplicated.get(key, (None, set(), 0)) + + diff = FileGroupDiff( + directory=Path(directory), + stem=stem, + db_files=db_files, + fs_files=fs_files, + file_group_id=db_id, + ) + + if diff.is_new: + new.append(diff) + elif diff.is_deleted: + deleted.append(diff) + elif diff.needs_update: + modified.append(diff) + elif diff.is_unchanged: + if fs_mtimes is not None: + fs_mtime = fs_mtimes.get(key, 0) + if fs_mtime > float(db_mtime) + MTIME_TOLERANCE_SECONDS: + modified.append(diff) + else: + unchanged.append(diff) + else: + try: + dir_path = Path(directory) + fs_mtime = max( + (dir_path / filename).stat().st_mtime + for filename in fs_files + ) + if fs_mtime > float(db_mtime) + MTIME_TOLERANCE_SECONDS: + modified.append(diff) + else: + unchanged.append(diff) + except (OSError, ValueError): + unchanged.append(diff) + elif db_id is not None and not fs_files and not db_files: + deleted.append(diff) + + return FileComparisonResult( + unchanged=unchanged, + new=new, + deleted=deleted, + modified=modified, + ) + + +def _normalize_roots(roots: Path | list[Path] | None) -> list[Path]: + """Always at least the media directory. An empty list is treated like None.""" + if roots is None: + return [get_media_directory()] + if isinstance(roots, (str, Path)): + return [Path(roots)] + root_list = [Path(r) for r in roots] + return root_list or [get_media_directory()] + + +def _directory_in_roots_sql(roots: list[Path], column: str = 'fg.directory') -> Tuple[str, dict]: + """SQL fragment: `column` is any of the roots or a subdirectory of one.""" + clauses = [] + params = {} + for i, root in enumerate(roots): + root_str = str(root) + clauses.append(f'({column} = :root_{i} OR {column} LIKE :root_pat_{i})') + params[f'root_{i}'] = root_str + params[f'root_pat_{i}'] = f'{root_str}/%' + return '(' + ' OR '.join(clauses) + ')', params + + +def _diff_to_paths(diff: FileGroupDiff) -> list[pathlib.Path]: + """Convert a FileGroupDiff to full file paths.""" + directory = pathlib.Path(diff.directory) + return [directory / filename for filename in diff.fs_files] + + +def _get_normalized_ignored_directories() -> list[str]: + """Get ignored directories as absolute paths. + + Delegates to files.lib so special directories (zims, videos, …) are never excluded. + """ + return get_normalized_ignored_directories() + + +async def count_files(directories: list[Path]) -> int: + """Lightning-fast file count using find | wc -l. + + Uses the same find arguments as _stream_filesystem_paths for consistency. + Excludes directories listed in get_wrolpi_config().ignored_directories. + Pipes to wc -l to avoid loading all paths into Python memory. + """ + if not directories: + return 0 + + # Build find arguments with ignored directory exclusions + find_args = [shlex.quote(str(d)) for d in directories] + find_args.extend(['-type', 'f', '-not', '-path', "'*/.*'"]) + + # Add exclusions for each ignored directory + for ignored in _get_normalized_ignored_directories(): + find_args.extend(['-not', '-path', shlex.quote(f'{ignored}/*')]) + + cmd = f"find {' '.join(find_args)} | wc -l" + proc = await asyncio.create_subprocess_shell( + cmd, + stdout=asyncio.subprocess.PIPE, + ) + try: + stdout, _ = await proc.communicate() + return int(stdout.strip()) + finally: + if proc.returncode is None: + proc.kill() + await proc.wait() + + +async def count_files_with_progress( + directories: list[Path], + callback: Callable[[int], None] = None, + batch_size: int = 1000, +) -> int: + """Count files with progress updates. + + Streams find output and calls callback with running count every batch_size files. + Excludes hidden files and directories listed in ignored_directories config. + """ + if not directories: + return 0 + + find_args = [str(d) for d in directories] + find_args.extend(['-type', 'f', '-not', '-path', '*/.*']) + for ignored in _get_normalized_ignored_directories(): + find_args.extend(['-not', '-path', f'{ignored}/*']) + + proc = await asyncio.create_subprocess_exec( + 'find', *find_args, + stdout=asyncio.subprocess.PIPE, + ) + try: + count = 0 + async for line in proc.stdout: + count += 1 + if callback and count % batch_size == 0: + callback(count) + await asyncio.sleep(0) # Yield for cancellation + if callback: + callback(count) # Final count + await proc.wait() + return count + finally: + if proc.returncode is None: + proc.kill() + await proc.wait() + + +async def find_directories(directories: list[Path]) -> set[Path]: + """Find all subdirectories using find command. + + Excludes hidden directories and ignored directories from config. + """ + if not directories: + return set() + + find_args = [str(d) for d in directories] + find_args.extend(['-type', 'd', '-not', '-path', '*/.*']) + for ignored in _get_normalized_ignored_directories(): + # Exclude the ignored directory itself as well as its contents. + find_args.extend(['-not', '-path', ignored, '-not', '-path', f'{ignored}/*']) + + proc = await asyncio.create_subprocess_exec( + 'find', *find_args, + stdout=asyncio.subprocess.PIPE, + ) + try: + found = set() + count = 0 + async for line in proc.stdout: + found.add(Path(line.decode().strip())) + count += 1 + if count % 100 == 0: + await asyncio.sleep(0) # Yield for cancellation + await proc.wait() + return found + finally: + if proc.returncode is None: + proc.kill() + await proc.wait() + + +def _find_args_for_roots(roots: list[Path]) -> list[str]: + """Common `find` arguments: files under roots, no hidden paths, no ignored dirs, no DB file.""" + find_args = [str(r) for r in roots] + find_args.extend(['-type', 'f', '-not', '-path', '*/.*']) + for ignored in _get_normalized_ignored_directories(): + find_args.extend(['-not', '-path', f'{ignored}/*']) + from wrolpi.db import get_db_file + find_args.extend(['-not', '-path', f'{get_db_file()}*']) + return find_args + + +async def _stream_filesystem_paths(root: Path | list[Path]) -> AsyncGenerator[str, None]: + """Stream file paths using find. Kept for tests that patch this generator.""" + async for directory, filename, stem, mtime in _stream_filesystem_entries(root): + yield str(Path(directory) / filename) + + +def _split_nul_field_triples(buf: bytes) -> Tuple[list, bytes]: + """Pull complete NUL-separated (directory, filename, mtime) triples out of `buf`. + + Returns (triples, leftover_bytes). A filename may contain tabs or newlines; only NUL + is the delimiter (GNU find `-printf '%h\\0%f\\0%T@\\0'`). + """ + triples = [] + while True: + parts = buf.split(b'\0', 3) + if len(parts) < 4: + return triples, buf + directory_b, filename_b, mtime_b, buf = parts + triples.append((directory_b, filename_b, mtime_b)) + + +def _entry_from_printf_fields(directory_b: bytes, filename_b: bytes, mtime_b: bytes): + """Decode one find -printf record. Returns None and logs if the record is unusable.""" + try: + directory = directory_b.decode() + filename = filename_b.decode() + mtime = float(mtime_b.decode()) + except (UnicodeDecodeError, ValueError) as e: + logger.warning(f'Skipping unreadable find record {directory_b!r}/{filename_b!r}: {e}') + return None + if not directory or not filename: + return None + stem, _ = split_path_stem_and_suffix(Path(directory) / filename) + return directory, filename, stem, mtime + + +async def _stream_gnu_find_entries(find_args: list[str]) -> AsyncGenerator[Tuple[str, str, str, float], None]: + """Stream (directory, filename, stem, mtime) using GNU find -printf with NUL delimiters.""" + proc = await asyncio.create_subprocess_exec( + 'find', *find_args, '-printf', GNU_FIND_PRINTF, + stdout=asyncio.subprocess.PIPE, + ) + buf = b'' + try: + while True: + chunk = await proc.stdout.read(65536) + if not chunk: + break + buf += chunk + triples, buf = _split_nul_field_triples(buf) + for fields in triples: + entry = _entry_from_printf_fields(*fields) + if entry: + yield entry + if buf.strip(b'\0'): + logger.warning(f'Incomplete find -printf record discarded: {buf[:200]!r}') + await proc.wait() + finally: + if proc.returncode is None: + proc.kill() + await proc.wait() + + +async def _stream_filesystem_entries( + roots: Path | list[Path], +) -> AsyncGenerator[Tuple[str, str, str, float], None]: + """Yield (directory, filename, stem, mtime) for files under roots. + + GNU find `-printf` records mtime in the same walk (Debian/RPi). macOS find has no + `-printf`, so each path is stat()'d once while streaming. Either way the mtime is + stored in the TEMP table and unchanged groups are never stat()'d again. + """ + root_list = _normalize_roots(roots) + find_args = _find_args_for_roots(root_list) + + if not IS_MACOS: + async for entry in _stream_gnu_find_entries(find_args): + yield entry + return + + proc = await asyncio.create_subprocess_exec( + 'find', *find_args, + stdout=asyncio.subprocess.PIPE, + ) + try: + async for line in proc.stdout: + path = line.decode().rstrip('\n') + if not path: + continue + p = Path(path) + try: + mtime = p.stat().st_mtime + except OSError: + continue + stem, _ = split_path_stem_and_suffix(p) + yield str(p.parent), p.name, stem, mtime + await proc.wait() + finally: + if proc.returncode is None: + proc.kill() + await proc.wait() + + +def _datetime_text_to_epoch(value) -> float: + """Convert a raw DB datetime (TEXT `YYYY-MM-DD HH:MM:SS.ffffff`, UTC) to a Unix epoch. + + Preserves microsecond precision (SQL `unixepoch()` truncates to whole seconds, which + would break sub-second mtime comparisons).""" + if not value: + return 0 + dt = value if isinstance(value, datetime) else datetime.fromisoformat(value) + if dt.tzinfo is None: + # TZDateTime stores all datetimes as naive UTC text. + dt = dt.replace(tzinfo=timezone.utc) + return dt.timestamp() + + +def _insert_fs_batch(conn, table_name: str, batch: list): + """Bulk insert filesystem files using executemany for speed.""" + if not batch: + return + conn.execute( + text(f""" + INSERT INTO {table_name} (directory, filename, stem, mtime) + VALUES (:directory, :filename, :stem, :mtime) + ON CONFLICT DO NOTHING + """), + [{"directory": b[0], "filename": b[1], "stem": b[2], "mtime": b[3]} for b in batch], + ) + + +def _diff_from_sql_row(row, fs_files: Set[str], db_files: Set[str]) -> FileGroupDiff: + return FileGroupDiff( + directory=Path(row.directory), + stem=row.stem, + db_files=db_files, + fs_files=fs_files, + file_group_id=row.id if getattr(row, 'id', None) else None, + ) + + +async def compare_file_groups( + roots: Path | list[Path] | None = None, + batch_size: int = 10000, + progress_callback: Callable[[int], None] = None, + root: Path | None = None, +) -> FileComparisonResult: + """ + Compare FileGroups in DB to filesystem. Memory efficient for Raspberry Pi. + + `roots` may be a single path or a list. Only FileGroups under those directories + are considered; a two-directory refresh must not delete a stale row in a third. + + Uses a uniquely-named SQLite TEMP table on a dedicated connection so the set-diff + runs in SQL (the join must use this connection; TEMP tables are per-connection). + Unchanged groups are never stat()'d a second time: mtime is recorded during the + find walk. + + This function is async and cancellable - when cancelled, it will terminate + the subprocess scanning the filesystem. + """ + if root is not None and roots is None: + roots = root + root_list = _normalize_roots(roots) + await _await_db(ensure_file_group_stems) + + # Per-call table name so concurrent scans don't collide. The work table is a TEMP table on + # its own dedicated connection: it never shares a transaction (or locks) with the session, + # it is invisible to other connections, and it is dropped automatically when the connection + # closes — orphaned work tables are impossible. + table_name = f'fs_files_{uuid.uuid4().hex[:12]}' + groups_table = f'{table_name}_groups' + roots_sql, root_params = _directory_in_roots_sql(root_list) + + with get_db_session() as session: + work_conn = session.get_bind().connect() + try: + work_conn.execute(text(f""" + CREATE TEMP TABLE {table_name} + ( + directory TEXT NOT NULL, + filename TEXT NOT NULL, + stem TEXT NOT NULL, + mtime REAL NOT NULL DEFAULT 0, + PRIMARY KEY (directory, filename) + ) + """)) + + batch = [] + file_count = 0 + path_gen = _stream_filesystem_entries(root_list) + try: + async for directory, filename, stem, mtime in path_gen: + batch.append((directory, filename, stem, mtime)) + + if len(batch) >= batch_size: + _insert_fs_batch(work_conn, table_name, batch) + file_count += len(batch) + batch = [] + if progress_callback: + progress_callback(file_count) + await asyncio.sleep(0) # Cancellation yield point + finally: + await path_gen.aclose() # Ensure subprocess is killed on cancellation + + if batch: + _insert_fs_batch(work_conn, table_name, batch) + file_count += len(batch) + + logger.info(f'Scanned {file_count} files from filesystem') + + work_conn.execute( + text(f"CREATE INDEX IF NOT EXISTS {table_name}_dir_stem_idx ON {table_name}(directory, stem)")) + work_conn.execute(text(f""" + CREATE TEMP TABLE {groups_table} AS + SELECT directory, stem, + json_group_array(filename) AS files, + COUNT(*) AS file_count, + MAX(mtime) AS mtime + FROM {table_name} + GROUP BY directory, stem + """)) + work_conn.execute(text(f"CREATE INDEX IF NOT EXISTS {groups_table}_idx ON {groups_table}(directory, stem)")) + work_conn.execute(text(f"ANALYZE {table_name}")) + + # Canonical DB row per (directory, stem). Duplicates (rn > 1) are deleted. + # The join runs on work_conn so it can see the TEMP tables. + db_canonical = f'{table_name}_db' + work_conn.execute(text(f""" + CREATE TEMP TABLE {db_canonical} AS + SELECT id, directory, stem, files, modification_datetime, file_count, rn + FROM ( + SELECT fg.id, + fg.directory, + COALESCE(fg.stem, '') AS stem, + fg.files, + fg.modification_datetime, + CASE + WHEN fg.files IS NULL OR json_type(fg.files) != 'array' + THEN 0 + ELSE json_array_length(fg.files) + END AS file_count, + ROW_NUMBER() OVER ( + PARTITION BY fg.directory, COALESCE(fg.stem, '') + ORDER BY ( + CASE + WHEN fg.files IS NULL OR json_type(fg.files) != 'array' + THEN 0 + ELSE json_array_length(fg.files) + END > 0 + ) DESC, fg.id DESC + ) AS rn + FROM file_group fg + WHERE fg.directory IS NOT NULL + AND {roots_sql} + ) + """), root_params) + + new = [] + deleted = [] + modified = [] + unchanged = [] + + # Duplicates: extra FileGroups for the same (directory, stem). + for row in work_conn.execute(text(f""" + SELECT id, directory, stem, files FROM {db_canonical} WHERE rn > 1 + """)): + deleted.append(_diff_from_sql_row(row, set(), _filenames_from_files_json(row.files))) + + # Deleted: canonical DB group with no filesystem group. + for row in work_conn.execute(text(f""" + SELECT c.id, c.directory, c.stem, c.files + FROM {db_canonical} c + LEFT JOIN {groups_table} fs ON fs.directory = c.directory AND fs.stem = c.stem + WHERE c.rn = 1 AND fs.stem IS NULL + """)): + deleted.append(_diff_from_sql_row(row, set(), _filenames_from_files_json(row.files))) + + # New: filesystem group with no DB group. + for row in work_conn.execute(text(f""" + SELECT fs.directory, fs.stem, fs.files, NULL AS id + FROM {groups_table} fs + LEFT JOIN {db_canonical} c ON c.directory = fs.directory AND c.stem = fs.stem AND c.rn = 1 + WHERE c.id IS NULL + """)): + fs_files = set(json.loads(row.files) if isinstance(row.files, str) else (row.files or [])) + new.append(_diff_from_sql_row(row, fs_files, set())) + + # Both sides present: compare names and mtime. Only pull file lists for these rows; + # unchanged file contents stay in SQL. + for row in work_conn.execute(text(f""" + SELECT c.id, c.directory, c.stem, c.files AS db_files, c.modification_datetime, + fs.files AS fs_files, fs.mtime AS fs_mtime, fs.file_count AS fs_count, + c.file_count AS db_count + FROM {db_canonical} c + JOIN {groups_table} fs ON fs.directory = c.directory AND fs.stem = c.stem + WHERE c.rn = 1 + """)): + fs_files = set(json.loads(row.fs_files) if isinstance(row.fs_files, str) else (row.fs_files or [])) + db_files = _filenames_from_files_json(row.db_files) + diff = FileGroupDiff( + directory=Path(row.directory), + stem=row.stem, + db_files=db_files, + fs_files=fs_files, + file_group_id=row.id, + ) + db_mtime = _datetime_text_to_epoch(row.modification_datetime) + if diff.needs_update: + modified.append(diff) + elif diff.is_unchanged: + if float(row.fs_mtime or 0) > db_mtime + MTIME_TOLERANCE_SECONDS: + modified.append(diff) + else: + unchanged.append(diff) + elif not fs_files and not db_files: + deleted.append(diff) + + logger.info( + f'Comparison complete: {len(unchanged)} unchanged, {len(new)} new, ' + f'{len(deleted)} deleted, {len(modified)} modified' + ) + + return FileComparisonResult( + unchanged=unchanged, + new=new, + deleted=deleted, + modified=modified, + ) + finally: + # Closing the dedicated connection drops its TEMP work table. + try: + work_conn.close() + except Exception: + logger.exception(f'Failed to close work table connection for {table_name}') diff --git a/wrolpi/files/test/test_worker.py b/wrolpi/files/test/test_worker.py index 4883bb28..cfa9690d 100644 --- a/wrolpi/files/test/test_worker.py +++ b/wrolpi/files/test/test_worker.py @@ -20,6 +20,8 @@ from wrolpi.files.worker import ( FileTaskType, FileWorkerJobFailed, QUEUE_STALL_SECONDS, +) +from wrolpi.files.refresh import ( _split_nul_field_triples, _entry_from_printf_fields, _normalize_roots, @@ -333,8 +335,8 @@ async def test_compare_file_groups_survives_concurrent_commit( """ make_files_structure(['docs/file1.txt', 'docs/file2.txt']) - from wrolpi.files import worker as worker_mod - original = worker_mod._stream_filesystem_entries + from wrolpi.files import refresh as refresh_mod + original = refresh_mod._stream_filesystem_entries async def commit_after_first(roots): first = True @@ -344,7 +346,7 @@ async def test_compare_file_groups_survives_concurrent_commit( first = False test_session.commit() - with mock.patch.object(worker_mod, '_stream_filesystem_entries', commit_after_first): + with mock.patch.object(refresh_mod, '_stream_filesystem_entries', commit_after_first): result = await compare_file_groups(test_directory) assert len(result.new) == 2 @@ -358,8 +360,8 @@ async def test_compare_file_groups_cleans_up_work_table_on_aborted_tx( dropped (no orphan tables left behind).""" make_files_structure(['docs/file1.txt']) - from wrolpi.files import worker as worker_mod - original = worker_mod._stream_filesystem_entries + from wrolpi.files import refresh as refresh_mod + original = refresh_mod._stream_filesystem_entries async def commit_then_abort_then_raise(roots): async for entry in original(roots): @@ -372,7 +374,7 @@ async def test_compare_file_groups_cleans_up_work_table_on_aborted_tx( pass raise RuntimeError('simulated crash mid-scan') - with mock.patch.object(worker_mod, '_stream_filesystem_entries', commit_then_abort_then_raise): + with mock.patch.object(refresh_mod, '_stream_filesystem_entries', commit_then_abort_then_raise): with pytest.raises(RuntimeError): await compare_file_groups(test_directory) diff --git a/wrolpi/files/worker.py b/wrolpi/files/worker.py index a0076328..2ce4f4ec 100644 --- a/wrolpi/files/worker.py +++ b/wrolpi/files/worker.py @@ -1,15 +1,14 @@ -""" -File comparison worker for comparing database FileGroups to filesystem state. +"""File worker queue: refresh, move, and reorganize jobs. -This module provides memory-efficient comparison of potentially millions of files -on resource-constrained devices like Raspberry Pi. +Filesystem compare/discover lives in `wrolpi.files.refresh`. This module owns the +job queue, status, and the FileWorker methods that apply compare results (upsert, +delete, model) plus move/reorganize. """ import asyncio import json import os import pathlib import queue -import shlex import shutil import stat as stat_module import time @@ -30,30 +29,23 @@ from wrolpi.common import get_media_directory, logger, walk, chunks, unique_by_p from wrolpi.db import get_db_session, get_db_curs from wrolpi.errors import NoPrimaryFile from wrolpi.events import Events -from wrolpi.vars import PYTEST, IS_MACOS +from wrolpi.vars import PYTEST from wrolpi.files.lib import ( split_path_stem_and_suffix, _upsert_files, get_unique_files_by_stem, glob_shared_stem, group_files_by_stem, get_primary_file, choose_primary_file, delete_directory, apply_indexers, _move_file_group_files, _bulk_update_file_groups_db, MOVE_CHUNK_SIZE, _bulk_update_file_groups_reorganize, get_normalized_ignored_directories, remove_files_in_ignored_directories, - ensure_file_group_stems, +) +from wrolpi.files.refresh import ( + FileGroupDiff, FileComparisonResult, classify_file_group_diffs, compare_file_groups, + count_files, count_files_with_progress, find_directories, GNU_FIND_PRINTF, + _await_db, _diff_to_paths, _filenames_from_files_json, _stream_filesystem_entries, ) logger = logger.getChild(__name__) - -async def _await_db(func, *args): - """Run a function that uses get_db_curs. - - Tests share one SQLite connection via get_db_curs. sqlite3 is not safe for concurrent - use of that connection, so asyncio.to_thread segfaults in CI (xdist + session commit - on the event loop while the worker thread is still in get_stem_algorithm_version / - _upsert_files). Production opens a dedicated raw connection and can use a thread. - """ - if PYTEST: - return func(*args) - return await asyncio.to_thread(func, *args) - +# Re-export refresh compare API so existing `from wrolpi.files.worker import compare_file_groups` +# call sites keep working. Internals used by FileWorker are imported above. # Update status every N items to avoid excessive overhead PROGRESS_UPDATE_INTERVAL = 100 @@ -61,14 +53,6 @@ PROGRESS_UPDATE_INTERVAL = 100 # Log if jobs sit queued while the worker reports idle for this long. QUEUE_STALL_SECONDS = 30 -# Maximum mtime difference (seconds) before a file is considered "modified". -# 0.5s accommodates SD card / FAT32 / exFAT timestamp granularity on Pi 4. -MTIME_TOLERANCE_SECONDS = 0.5 - -# GNU find -printf format. `\0` here is the two-character find escape, NOT a Python -# NUL: subprocess arguments cannot contain embedded null bytes. -GNU_FIND_PRINTF = r'%h\0%f\0%T@\0' - __all__ = [ 'FileGroupDiff', 'FileComparisonResult', @@ -85,658 +69,6 @@ class FileWorkerJobFailed(RuntimeError): """A tracked file-worker job finished unsuccessfully.""" -@dataclass -class FileGroupDiff: - """Represents changes needed for a FileGroup.""" - directory: Path | str - stem: str - db_files: Set[str] = field(default_factory=set) # Filenames in DB - fs_files: Set[str] = field(default_factory=set) # Filenames on filesystem - file_group_id: int | None = None - - @property - def added_files(self) -> Set[str]: - """Files on disk but not in DB.""" - return self.fs_files - self.db_files - - @property - def removed_files(self) -> Set[str]: - """Files in DB but not on disk.""" - return self.db_files - self.fs_files - - @property - def is_new(self) -> bool: - """FileGroup doesn't exist in DB.""" - return self.file_group_id is None and bool(self.fs_files) - - @property - def is_deleted(self) -> bool: - """All files deleted from disk.""" - return not self.fs_files and bool(self.db_files) - - @property - def needs_update(self) -> bool: - """FileGroup exists but files changed (not completely deleted).""" - return (self.file_group_id is not None - and bool(self.added_files or self.removed_files) - and bool(self.fs_files)) # Not deleted - still has files on disk - - @property - def is_unchanged(self) -> bool: - """FileGroup matches filesystem exactly.""" - return self.db_files == self.fs_files and bool(self.db_files) - - -@dataclass -class FileComparisonResult: - """Result of comparing DB to filesystem at FileGroup level.""" - unchanged: list # FileGroupDiff where is_unchanged - new: list # FileGroupDiff where is_new - deleted: list # FileGroupDiff where is_deleted - modified: list # FileGroupDiff where needs_update - - -def _deduplicate_db_groups( - db_groups: Dict[Tuple[str, str], List[Tuple[int, Set[str], float]]], -) -> Tuple[Dict[Tuple[str, str], Tuple[int, Set[str], float]], List[FileGroupDiff]]: - """Pick one FileGroup per (directory, stem) key, mark the rest for deletion. - - Prefers the FG that has files (non-empty), then highest ID as a stable tiebreaker. - """ - deduplicated: Dict[Tuple[str, str], Tuple[int, Set[str], float]] = {} - duplicate_diffs: List[FileGroupDiff] = [] - for key, entries in db_groups.items(): - # Sort: prefer non-empty files first, then highest ID - entries.sort(key=lambda e: (bool(e[1]), e[0]), reverse=True) - deduplicated[key] = entries[0] - for fg_id, fg_files, _ in entries[1:]: - directory, stem = key - duplicate_diffs.append(FileGroupDiff( - directory=Path(directory), - stem=stem, - db_files=fg_files or {'__empty__'}, - fs_files=set(), - file_group_id=fg_id, - )) - return deduplicated, duplicate_diffs - - -def _filenames_from_files_json(raw) -> Set[str]: - """Filenames from a FileGroup.files JSON value (relative or absolute paths).""" - if not raw: - return set() - if isinstance(raw, str): - raw = json.loads(raw) - if not isinstance(raw, list): - return set() - names = set() - for entry in raw: - path = entry.get('path') if isinstance(entry, dict) else entry - if path: - names.add(Path(path).name) - return names - - -def classify_file_group_diffs( - fs_groups: Dict[Tuple[str, str], Set[str]], - db_groups: Dict[Tuple[str, str], List[Tuple[int, Set[str], float]]], - fs_mtimes: Dict[Tuple[str, str], float] | None = None, -) -> FileComparisonResult: - """Classify filesystem vs DB groups into new/deleted/modified/unchanged. - - `db_groups` may contain duplicates per (directory, stem); extras are marked deleted. - If `fs_mtimes` is provided (max mtime per key), unchanged-by-name groups with a newer - filesystem mtime are classified modified without a second stat(). If omitted, those - groups are stat()'d (used by the small direct-file path). - """ - deduplicated, duplicate_diffs = _deduplicate_db_groups(db_groups) - - unchanged = [] - new = [] - deleted = list(duplicate_diffs) - modified = [] - - all_keys = set(fs_groups.keys()) | set(deduplicated.keys()) - - for key in all_keys: - directory, stem = key - fs_files = fs_groups.get(key, set()) - db_id, db_files, db_mtime = deduplicated.get(key, (None, set(), 0)) - - diff = FileGroupDiff( - directory=Path(directory), - stem=stem, - db_files=db_files, - fs_files=fs_files, - file_group_id=db_id, - ) - - if diff.is_new: - new.append(diff) - elif diff.is_deleted: - deleted.append(diff) - elif diff.needs_update: - modified.append(diff) - elif diff.is_unchanged: - if fs_mtimes is not None: - fs_mtime = fs_mtimes.get(key, 0) - if fs_mtime > float(db_mtime) + MTIME_TOLERANCE_SECONDS: - modified.append(diff) - else: - unchanged.append(diff) - else: - try: - dir_path = Path(directory) - fs_mtime = max( - (dir_path / filename).stat().st_mtime - for filename in fs_files - ) - if fs_mtime > float(db_mtime) + MTIME_TOLERANCE_SECONDS: - modified.append(diff) - else: - unchanged.append(diff) - except (OSError, ValueError): - unchanged.append(diff) - elif db_id is not None and not fs_files and not db_files: - deleted.append(diff) - - return FileComparisonResult( - unchanged=unchanged, - new=new, - deleted=deleted, - modified=modified, - ) - - -def _normalize_roots(roots: Path | list[Path] | None) -> list[Path]: - """Always at least the media directory. An empty list is treated like None.""" - if roots is None: - return [get_media_directory()] - if isinstance(roots, (str, Path)): - return [Path(roots)] - root_list = [Path(r) for r in roots] - return root_list or [get_media_directory()] - - -def _directory_in_roots_sql(roots: list[Path], column: str = 'fg.directory') -> Tuple[str, dict]: - """SQL fragment: `column` is any of the roots or a subdirectory of one.""" - clauses = [] - params = {} - for i, root in enumerate(roots): - root_str = str(root) - clauses.append(f'({column} = :root_{i} OR {column} LIKE :root_pat_{i})') - params[f'root_{i}'] = root_str - params[f'root_pat_{i}'] = f'{root_str}/%' - return '(' + ' OR '.join(clauses) + ')', params - - -def _diff_to_paths(diff: FileGroupDiff) -> list[pathlib.Path]: - """Convert a FileGroupDiff to full file paths.""" - directory = pathlib.Path(diff.directory) - return [directory / filename for filename in diff.fs_files] - - -def _get_normalized_ignored_directories() -> list[str]: - """Get ignored directories as absolute paths. - - Delegates to files.lib so special directories (zims, videos, …) are never excluded. - """ - return get_normalized_ignored_directories() - - -async def count_files(directories: list[Path]) -> int: - """Lightning-fast file count using find | wc -l. - - Uses the same find arguments as _stream_filesystem_paths for consistency. - Excludes directories listed in get_wrolpi_config().ignored_directories. - Pipes to wc -l to avoid loading all paths into Python memory. - """ - if not directories: - return 0 - - # Build find arguments with ignored directory exclusions - find_args = [shlex.quote(str(d)) for d in directories] - find_args.extend(['-type', 'f', '-not', '-path', "'*/.*'"]) - - # Add exclusions for each ignored directory - for ignored in _get_normalized_ignored_directories(): - find_args.extend(['-not', '-path', shlex.quote(f'{ignored}/*')]) - - cmd = f"find {' '.join(find_args)} | wc -l" - proc = await asyncio.create_subprocess_shell( - cmd, - stdout=asyncio.subprocess.PIPE, - ) - try: - stdout, _ = await proc.communicate() - return int(stdout.strip()) - finally: - if proc.returncode is None: - proc.kill() - await proc.wait() - - -async def count_files_with_progress( - directories: list[Path], - callback: Callable[[int], None] = None, - batch_size: int = 1000, -) -> int: - """Count files with progress updates. - - Streams find output and calls callback with running count every batch_size files. - Excludes hidden files and directories listed in ignored_directories config. - """ - if not directories: - return 0 - - find_args = [str(d) for d in directories] - find_args.extend(['-type', 'f', '-not', '-path', '*/.*']) - for ignored in _get_normalized_ignored_directories(): - find_args.extend(['-not', '-path', f'{ignored}/*']) - - proc = await asyncio.create_subprocess_exec( - 'find', *find_args, - stdout=asyncio.subprocess.PIPE, - ) - try: - count = 0 - async for line in proc.stdout: - count += 1 - if callback and count % batch_size == 0: - callback(count) - await asyncio.sleep(0) # Yield for cancellation - if callback: - callback(count) # Final count - await proc.wait() - return count - finally: - if proc.returncode is None: - proc.kill() - await proc.wait() - - -async def find_directories(directories: list[Path]) -> set[Path]: - """Find all subdirectories using find command. - - Excludes hidden directories and ignored directories from config. - """ - if not directories: - return set() - - find_args = [str(d) for d in directories] - find_args.extend(['-type', 'd', '-not', '-path', '*/.*']) - for ignored in _get_normalized_ignored_directories(): - # Exclude the ignored directory itself as well as its contents. - find_args.extend(['-not', '-path', ignored, '-not', '-path', f'{ignored}/*']) - - proc = await asyncio.create_subprocess_exec( - 'find', *find_args, - stdout=asyncio.subprocess.PIPE, - ) - try: - found = set() - count = 0 - async for line in proc.stdout: - found.add(Path(line.decode().strip())) - count += 1 - if count % 100 == 0: - await asyncio.sleep(0) # Yield for cancellation - await proc.wait() - return found - finally: - if proc.returncode is None: - proc.kill() - await proc.wait() - - -def _find_args_for_roots(roots: list[Path]) -> list[str]: - """Common `find` arguments: files under roots, no hidden paths, no ignored dirs, no DB file.""" - find_args = [str(r) for r in roots] - find_args.extend(['-type', 'f', '-not', '-path', '*/.*']) - for ignored in _get_normalized_ignored_directories(): - find_args.extend(['-not', '-path', f'{ignored}/*']) - from wrolpi.db import get_db_file - find_args.extend(['-not', '-path', f'{get_db_file()}*']) - return find_args - - -async def _stream_filesystem_paths(root: Path | list[Path]) -> AsyncGenerator[str, None]: - """Stream file paths using find. Kept for tests that patch this generator.""" - async for directory, filename, stem, mtime in _stream_filesystem_entries(root): - yield str(Path(directory) / filename) - - -def _split_nul_field_triples(buf: bytes) -> Tuple[list, bytes]: - """Pull complete NUL-separated (directory, filename, mtime) triples out of `buf`. - - Returns (triples, leftover_bytes). A filename may contain tabs or newlines; only NUL - is the delimiter (GNU find `-printf '%h\\0%f\\0%T@\\0'`). - """ - triples = [] - while True: - parts = buf.split(b'\0', 3) - if len(parts) < 4: - return triples, buf - directory_b, filename_b, mtime_b, buf = parts - triples.append((directory_b, filename_b, mtime_b)) - - -def _entry_from_printf_fields(directory_b: bytes, filename_b: bytes, mtime_b: bytes): - """Decode one find -printf record. Returns None and logs if the record is unusable.""" - try: - directory = directory_b.decode() - filename = filename_b.decode() - mtime = float(mtime_b.decode()) - except (UnicodeDecodeError, ValueError) as e: - logger.warning(f'Skipping unreadable find record {directory_b!r}/{filename_b!r}: {e}') - return None - if not directory or not filename: - return None - stem, _ = split_path_stem_and_suffix(Path(directory) / filename) - return directory, filename, stem, mtime - - -async def _stream_gnu_find_entries(find_args: list[str]) -> AsyncGenerator[Tuple[str, str, str, float], None]: - """Stream (directory, filename, stem, mtime) using GNU find -printf with NUL delimiters.""" - proc = await asyncio.create_subprocess_exec( - 'find', *find_args, '-printf', GNU_FIND_PRINTF, - stdout=asyncio.subprocess.PIPE, - ) - buf = b'' - try: - while True: - chunk = await proc.stdout.read(65536) - if not chunk: - break - buf += chunk - triples, buf = _split_nul_field_triples(buf) - for fields in triples: - entry = _entry_from_printf_fields(*fields) - if entry: - yield entry - if buf.strip(b'\0'): - logger.warning(f'Incomplete find -printf record discarded: {buf[:200]!r}') - await proc.wait() - finally: - if proc.returncode is None: - proc.kill() - await proc.wait() - - -async def _stream_filesystem_entries( - roots: Path | list[Path], -) -> AsyncGenerator[Tuple[str, str, str, float], None]: - """Yield (directory, filename, stem, mtime) for files under roots. - - GNU find `-printf` records mtime in the same walk (Debian/RPi). macOS find has no - `-printf`, so each path is stat()'d once while streaming. Either way the mtime is - stored in the TEMP table and unchanged groups are never stat()'d again. - """ - root_list = _normalize_roots(roots) - find_args = _find_args_for_roots(root_list) - - if not IS_MACOS: - async for entry in _stream_gnu_find_entries(find_args): - yield entry - return - - proc = await asyncio.create_subprocess_exec( - 'find', *find_args, - stdout=asyncio.subprocess.PIPE, - ) - try: - async for line in proc.stdout: - path = line.decode().rstrip('\n') - if not path: - continue - p = Path(path) - try: - mtime = p.stat().st_mtime - except OSError: - continue - stem, _ = split_path_stem_and_suffix(p) - yield str(p.parent), p.name, stem, mtime - await proc.wait() - finally: - if proc.returncode is None: - proc.kill() - await proc.wait() - - -def _datetime_text_to_epoch(value) -> float: - """Convert a raw DB datetime (TEXT `YYYY-MM-DD HH:MM:SS.ffffff`, UTC) to a Unix epoch. - - Preserves microsecond precision (SQL `unixepoch()` truncates to whole seconds, which - would break sub-second mtime comparisons).""" - if not value: - return 0 - dt = value if isinstance(value, datetime) else datetime.fromisoformat(value) - if dt.tzinfo is None: - # TZDateTime stores all datetimes as naive UTC text. - dt = dt.replace(tzinfo=timezone.utc) - return dt.timestamp() - - -def _insert_fs_batch(conn, table_name: str, batch: list): - """Bulk insert filesystem files using executemany for speed.""" - if not batch: - return - conn.execute( - text(f""" - INSERT INTO {table_name} (directory, filename, stem, mtime) - VALUES (:directory, :filename, :stem, :mtime) - ON CONFLICT DO NOTHING - """), - [{"directory": b[0], "filename": b[1], "stem": b[2], "mtime": b[3]} for b in batch], - ) - - -def _diff_from_sql_row(row, fs_files: Set[str], db_files: Set[str]) -> FileGroupDiff: - return FileGroupDiff( - directory=Path(row.directory), - stem=row.stem, - db_files=db_files, - fs_files=fs_files, - file_group_id=row.id if getattr(row, 'id', None) else None, - ) - - -async def compare_file_groups( - roots: Path | list[Path] | None = None, - batch_size: int = 10000, - progress_callback: Callable[[int], None] = None, - root: Path | None = None, -) -> FileComparisonResult: - """ - Compare FileGroups in DB to filesystem. Memory efficient for Raspberry Pi. - - `roots` may be a single path or a list. Only FileGroups under those directories - are considered; a two-directory refresh must not delete a stale row in a third. - - Uses a uniquely-named SQLite TEMP table on a dedicated connection so the set-diff - runs in SQL (the join must use this connection; TEMP tables are per-connection). - Unchanged groups are never stat()'d a second time: mtime is recorded during the - find walk. - - This function is async and cancellable - when cancelled, it will terminate - the subprocess scanning the filesystem. - """ - if root is not None and roots is None: - roots = root - root_list = _normalize_roots(roots) - await _await_db(ensure_file_group_stems) - - # Per-call table name so concurrent scans don't collide. The work table is a TEMP table on - # its own dedicated connection: it never shares a transaction (or locks) with the session, - # it is invisible to other connections, and it is dropped automatically when the connection - # closes — orphaned work tables are impossible. - table_name = f'fs_files_{uuid.uuid4().hex[:12]}' - groups_table = f'{table_name}_groups' - roots_sql, root_params = _directory_in_roots_sql(root_list) - - with get_db_session() as session: - work_conn = session.get_bind().connect() - try: - work_conn.execute(text(f""" - CREATE TEMP TABLE {table_name} - ( - directory TEXT NOT NULL, - filename TEXT NOT NULL, - stem TEXT NOT NULL, - mtime REAL NOT NULL DEFAULT 0, - PRIMARY KEY (directory, filename) - ) - """)) - - batch = [] - file_count = 0 - path_gen = _stream_filesystem_entries(root_list) - try: - async for directory, filename, stem, mtime in path_gen: - batch.append((directory, filename, stem, mtime)) - - if len(batch) >= batch_size: - _insert_fs_batch(work_conn, table_name, batch) - file_count += len(batch) - batch = [] - if progress_callback: - progress_callback(file_count) - await asyncio.sleep(0) # Cancellation yield point - finally: - await path_gen.aclose() # Ensure subprocess is killed on cancellation - - if batch: - _insert_fs_batch(work_conn, table_name, batch) - file_count += len(batch) - - logger.info(f'Scanned {file_count} files from filesystem') - - work_conn.execute( - text(f"CREATE INDEX IF NOT EXISTS {table_name}_dir_stem_idx ON {table_name}(directory, stem)")) - work_conn.execute(text(f""" - CREATE TEMP TABLE {groups_table} AS - SELECT directory, stem, - json_group_array(filename) AS files, - COUNT(*) AS file_count, - MAX(mtime) AS mtime - FROM {table_name} - GROUP BY directory, stem - """)) - work_conn.execute(text(f"CREATE INDEX IF NOT EXISTS {groups_table}_idx ON {groups_table}(directory, stem)")) - work_conn.execute(text(f"ANALYZE {table_name}")) - - # Canonical DB row per (directory, stem). Duplicates (rn > 1) are deleted. - # The join runs on work_conn so it can see the TEMP tables. - db_canonical = f'{table_name}_db' - work_conn.execute(text(f""" - CREATE TEMP TABLE {db_canonical} AS - SELECT id, directory, stem, files, modification_datetime, file_count, rn - FROM ( - SELECT fg.id, - fg.directory, - COALESCE(fg.stem, '') AS stem, - fg.files, - fg.modification_datetime, - CASE - WHEN fg.files IS NULL OR json_type(fg.files) != 'array' - THEN 0 - ELSE json_array_length(fg.files) - END AS file_count, - ROW_NUMBER() OVER ( - PARTITION BY fg.directory, COALESCE(fg.stem, '') - ORDER BY ( - CASE - WHEN fg.files IS NULL OR json_type(fg.files) != 'array' - THEN 0 - ELSE json_array_length(fg.files) - END > 0 - ) DESC, fg.id DESC - ) AS rn - FROM file_group fg - WHERE fg.directory IS NOT NULL - AND {roots_sql} - ) - """), root_params) - - new = [] - deleted = [] - modified = [] - unchanged = [] - - # Duplicates: extra FileGroups for the same (directory, stem). - for row in work_conn.execute(text(f""" - SELECT id, directory, stem, files FROM {db_canonical} WHERE rn > 1 - """)): - deleted.append(_diff_from_sql_row(row, set(), _filenames_from_files_json(row.files))) - - # Deleted: canonical DB group with no filesystem group. - for row in work_conn.execute(text(f""" - SELECT c.id, c.directory, c.stem, c.files - FROM {db_canonical} c - LEFT JOIN {groups_table} fs ON fs.directory = c.directory AND fs.stem = c.stem - WHERE c.rn = 1 AND fs.stem IS NULL - """)): - deleted.append(_diff_from_sql_row(row, set(), _filenames_from_files_json(row.files))) - - # New: filesystem group with no DB group. - for row in work_conn.execute(text(f""" - SELECT fs.directory, fs.stem, fs.files, NULL AS id - FROM {groups_table} fs - LEFT JOIN {db_canonical} c ON c.directory = fs.directory AND c.stem = fs.stem AND c.rn = 1 - WHERE c.id IS NULL - """)): - fs_files = set(json.loads(row.files) if isinstance(row.files, str) else (row.files or [])) - new.append(_diff_from_sql_row(row, fs_files, set())) - - # Both sides present: compare names and mtime. Only pull file lists for these rows; - # unchanged file contents stay in SQL. - for row in work_conn.execute(text(f""" - SELECT c.id, c.directory, c.stem, c.files AS db_files, c.modification_datetime, - fs.files AS fs_files, fs.mtime AS fs_mtime, fs.file_count AS fs_count, - c.file_count AS db_count - FROM {db_canonical} c - JOIN {groups_table} fs ON fs.directory = c.directory AND fs.stem = c.stem - WHERE c.rn = 1 - """)): - fs_files = set(json.loads(row.fs_files) if isinstance(row.fs_files, str) else (row.fs_files or [])) - db_files = _filenames_from_files_json(row.db_files) - diff = FileGroupDiff( - directory=Path(row.directory), - stem=row.stem, - db_files=db_files, - fs_files=fs_files, - file_group_id=row.id, - ) - db_mtime = _datetime_text_to_epoch(row.modification_datetime) - if diff.needs_update: - modified.append(diff) - elif diff.is_unchanged: - if float(row.fs_mtime or 0) > db_mtime + MTIME_TOLERANCE_SECONDS: - modified.append(diff) - else: - unchanged.append(diff) - elif not fs_files and not db_files: - deleted.append(diff) - - logger.info( - f'Comparison complete: {len(unchanged)} unchanged, {len(new)} new, ' - f'{len(deleted)} deleted, {len(modified)} modified' - ) - - return FileComparisonResult( - unchanged=unchanged, - new=new, - deleted=deleted, - modified=modified, - ) - finally: - # Closing the dedicated connection drops its TEMP work table. - try: - work_conn.close() - except Exception: - logger.exception(f'Failed to close work table connection for {table_name}') - - async def build_move_plan_bulk( sources: list[pathlib.Path], destination: pathlib.Path, @@ -1446,7 +778,7 @@ class FileWorker: # For global refreshes, check that there are non-ignored files is_global_refresh = len(dir_paths) == 1 and dir_paths[0] == media_directory and not file_paths if is_global_refresh: - ignored_directories = set(_get_normalized_ignored_directories()) + ignored_directories = set(get_normalized_ignored_directories()) has_non_ignored_files = False for item in media_directory.iterdir(): if item.name.startswith('.'): diff --git a/wrolpi/test/test_db_bootstrap.py b/wrolpi/test/test_db_bootstrap.py index 045a050d..1098b22b 100644 --- a/wrolpi/test/test_db_bootstrap.py +++ b/wrolpi/test/test_db_bootstrap.py @@ -36,6 +36,9 @@ def test_ensure_db_creates_and_migrates(test_directory): fts_tables = {i[0] for i in conn.execute( "SELECT name FROM sqlite_master WHERE type='table' AND name IN ('file_group_fts', 'doc_section_fts')")} assert fts_tables == {'file_group_fts', 'doc_section_fts'} + file_group_cols = {r[1] for r in conn.execute('PRAGMA table_info(file_group)')} + assert 'idempotency' not in file_group_cols + assert 'stem' in file_group_cols # Running again is a no-op. assert db_bootstrap.ensure_db() is True @@ -117,6 +120,44 @@ def test_unique_stem_migration_collapses_duplicates(test_directory): assert 'UNIQUE' in sql.upper() +def test_drop_file_group_idempotency_preserves_fts(test_directory): + """DROP COLUMN idempotency must not rebuild file_group or break file_group_fts.""" + from alembic.config import Config + from alembic import command + from wrolpi.db import get_db_uri + from wrolpi.vars import PROJECT_DIR + + (test_directory / 'config').mkdir(parents=True, exist_ok=True) + config = Config(str(PROJECT_DIR / 'alembic.ini')) + config.set_main_option('sqlalchemy.url', get_db_uri()) + command.upgrade(config, '2026_09_18_1200') + + db_file = get_db_file() + with sqlite3.connect(db_file) as conn: + cols = {r[1] for r in conn.execute('PRAGMA table_info(file_group)')} + assert 'idempotency' in cols + conn.execute( + 'INSERT INTO file_group (directory, primary_path, files, stem, indexed, a_text) ' + 'VALUES (?,?,?,?,0,?)', + (str(test_directory), str(test_directory / 'searchable.txt'), '[]', 'searchable', 'alpha uniquephrase'), + ) + conn.commit() + hits = conn.execute( + "SELECT rowid FROM file_group_fts WHERE file_group_fts MATCH 'uniquephrase'" + ).fetchall() + assert len(hits) == 1 + + command.upgrade(config, 'head') + + with sqlite3.connect(db_file) as conn: + cols = {r[1] for r in conn.execute('PRAGMA table_info(file_group)')} + assert 'idempotency' not in cols + hits = conn.execute( + "SELECT rowid FROM file_group_fts WHERE file_group_fts MATCH 'uniquephrase'" + ).fetchall() + assert len(hits) == 1 + + def test_bootstrap_lock(test_directory): """Only one process can hold the bootstrap lock.""" with db_bootstrap.bootstrap_lock() as acquired: