Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

fix(inventory): stop the view seed from re-reading the backup every boot

Pouzord0679ad72be2
diff --git a/backend/app/services/inventory_sync.py b/backend/app/services/inventory_sync.py
index 02715f8..62e3b83 100644
--- a/backend/app/services/inventory_sync.py
+++ b/backend/app/services/inventory_sync.py
@@ -774,7 +774,16 @@ async def seed_node_views(
nodes = list(
(
await db.execute(
- select(Node).where(Node.device_id.isnot(None), Node.display_view.is_(None))
+ select(Node).where(
+ # A `device_id` naming a row that no longer exists cannot be
+ # seeded from anything, and leaving it in would match this
+ # query on every later boot — re-reading the backup file and
+ # logging a recovery that seeds nothing. Deleting a device
+ # leaves exactly that: SQLite runs with foreign keys off, so
+ # the `ON DELETE SET NULL` never fires on an existing table.
+ Node.device_id.in_(select(InventoryDevice.id)),
+ Node.display_view.is_(None),
+ )
)
)
.scalars()
@@ -789,7 +798,7 @@ async def seed_node_views(
seeded = 0
for node in nodes:
device = devices.get(node.device_id or "")
- if device is None:
+ if device is None: # pragma: no cover - the query already excluded these
continue
was = was_drawn.get(node.id)
# `strict`: an empty list in the backup is this canvas' real answer —
diff --git a/backend/tests/test_inventory_sync.py b/backend/tests/test_inventory_sync.py
index 177c53e..e247c4e 100644
--- a/backend/tests/test_inventory_sync.py
+++ b/backend/tests/test_inventory_sync.py
@@ -1269,6 +1269,27 @@ class TestPerNodeView:
# Idempotent: a second boot finds nothing without a view.
assert await seed_node_views(db_session) == 0
+ @pytest.mark.asyncio
+ async def test_a_node_whose_device_was_deleted_does_not_keep_the_seed_alive(self, db_session):
+ """Deleting a device leaves `nodes.device_id` dangling — foreign keys are off.
+
+ Such a node can be seeded from nothing, so it must not keep matching the
+ query: it would re-open the pre-upgrade backup on every single boot and
+ log a recovery that recovers nothing.
+ """
+ design = await _design(db_session)
+ db_session.add(_node(design, device_id="d-gone"))
+ await db_session.commit()
+
+ reads = []
+
+ def drawn():
+ reads.append(1)
+ return {}
+
+ assert await seed_node_views(db_session, drawn=drawn) == 0
+ assert reads == [] # the backup was never opened
+
@pytest.mark.asyncio
async def test_seeding_leaves_furniture_alone(self, db_session):
design = await _design(db_session)