Fix missing import, add transaction safety, clean orphaned snapshots

- Add load_json to tab_timeline_ng imports (NameError on disk fallback)
- Wrap save_history_tree in BEGIN/COMMIT transaction (was autocommitting
  each statement, risking partial writes on failure)
- Clean up orphaned history_snapshots in sync_to_db when nodes are
  removed from the tree

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-19 09:56:10 +01:00
parent a1a85ecc4d
commit e575a78893
3 changed files with 43 additions and 21 deletions
+2 -3
View File
@@ -8,7 +8,7 @@ from nicegui import ui
from state import AppState
from history_tree import HistoryTree
from utils import save_json, sync_to_db, KEY_BATCH_DATA, KEY_HISTORY_TREE
from utils import save_json, load_json, sync_to_db, KEY_BATCH_DATA, KEY_HISTORY_TREE
logger = logging.getLogger(__name__)
@@ -580,8 +580,7 @@ async def _restore_node(data, node, htree, file_path, state: AppState):
state.db.get_node_snapshot, df['id'], node['id'])
if not raw_snap:
# Last resort: read from JSON file on disk
from utils import load_json as _load_json
raw_file, _ = await asyncio.to_thread(_load_json, file_path)
raw_file, _ = await asyncio.to_thread(load_json, file_path)
tree_on_disk = raw_file.get(KEY_HISTORY_TREE, {})
raw_snap = tree_on_disk.get('nodes', {}).get(node['id'], {}).get('data', {})
node_data = json.loads(json.dumps(raw_snap)) if raw_snap else {}