This commit is contained in:
2026-02-10 10:29:20 +01:00
parent 9838164b88
commit f9082f1e5b
7 changed files with 156 additions and 7 deletions

View File

@@ -29,5 +29,5 @@ BACKUP_PATH = os.getenv("BACKUP_PATH")
# Behaviour
# =========================
DRY_RUN = os.getenv("DRY_RUN", "1") == "1"
DRY_RUN = os.getenv("DRY_RUN", "true").lower() in ("1", "true", "yes")
BATCH_SIZE = int(os.getenv("BATCH_SIZE", 1000))

View File

@@ -79,10 +79,15 @@ def batch_insert_files(cur, files_list: list, run_id: int) -> dict:
f["size"], f["mtime"], f["content_hash"], run_id, run_id)
for f in chunk]
)
# pymysql executemany: lastrowid = first id in batch
first_id = cur.lastrowid
for j, f in enumerate(chunk):
path_to_id[f["relative_path"]] = first_id + j
# Fetch real IDs — lastrowid+j is unreliable with executemany
paths = [f["relative_path"] for f in chunk]
placeholders = ",".join(["%s"] * len(paths))
cur.execute(
f"SELECT id, relative_path FROM files WHERE relative_path IN ({placeholders})",
paths,
)
for row in cur.fetchall():
path_to_id[row[1]] = row[0]
return path_to_id

View File

@@ -20,11 +20,14 @@ def scan_files(root_path: str) -> dict:
continue
rel_path = os.path.relpath(full_path, root_path).replace("\\", "/")
rel_dir = os.path.relpath(root, root_path).replace("\\", "/")
# Truncate microseconds — MySQL DATETIME rounds to whole seconds,
# which causes false "modified" detections on every run.
mtime = datetime.fromtimestamp(stat.st_mtime).replace(microsecond=0)
result[rel_path] = {
"full_path": full_path,
"file_name": name,
"directory": rel_dir,
"size": stat.st_size,
"mtime": datetime.fromtimestamp(stat.st_mtime),
"mtime": mtime,
}
return result