#!/usr/bin/env python3 """ Fix LiveSync corruption by recalculating file sizes The corruption happens when size field doesn't match actual chunk content size """ import couchdb import time from urllib.parse import quote USER = "admin" PASSWORD = "DonCucarach0!?" IP_ADDRESS = "100.100.112.48" PORT = "5984" DB_NAME = "obsidiandb" def fix_sizes(): safe_password = quote(PASSWORD, safe="") safe_username = quote(USER, safe="") url = f"http://{safe_username}:{safe_password}@{IP_ADDRESS}:{PORT}/" server = couchdb.Server(url) db = server[DB_NAME] print("Scanning for size mismatches...") fixed = [] correct = [] for row in db.view('_all_docs', include_docs=True): doc = row.doc # Only process file metadata docs if 'children' not in doc or not isinstance(doc.get('children'), list): continue path = doc.get('path', doc['_id']) stored_size = doc.get('size', 0) # Calculate actual size from chunks actual_size = 0 for chunk_id in doc['children']: if chunk_id in db: chunk = db[chunk_id] data = chunk.get('data', chunk.get('content', '')) actual_size += len(str(data)) if stored_size != actual_size: print(f"āŒ {path}") print(f" Stored: {stored_size}, Actual: {actual_size}") # Fix it doc['size'] = actual_size doc['mtime'] = int(time.time() * 1000) # Trigger sync try: db.save(doc) fixed.append(path) print(f" āœ“ Fixed") except Exception as e: print(f" āœ— Failed: {e}") else: correct.append(path) print(f"\n{'='*60}") print(f"āœ… Correct: {len(correct)} files") print(f"šŸ”§ Fixed: {len(fixed)} files") if fixed: print(f"\nFixed files:") for f in fixed: print(f" - {f}") print(f"\nāœ… All sizes corrected! Obsidian should now sync properly.") if __name__ == "__main__": fix_sizes()