Skip to content

Commit 22ef761

Browse files
committed
Check for ID before recording a record
1 parent ab2ee68 commit 22ef761

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

jupyter_server_fileid/manager.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,18 @@ def _from_normalized_path(self, path: Optional[str]) -> Optional[str]:
311311
return relpath
312312

313313
def _create(self, path: str) -> str:
314-
path = self._normalize_path(path)
315-
id = self._uuid()
316-
self.con.execute("INSERT INTO Files (id, path) VALUES (?, ?)", (id, path))
317-
return id
318-
319-
def index(self, path: str) -> str:
320314
path = self._normalize_path(path)
321315
row = self.con.execute("SELECT id FROM Files WHERE path = ?", (path,)).fetchone()
322316
existing_id = row and row[0]
323317

324318
if existing_id:
325319
return existing_id
320+
321+
id = self._uuid()
322+
self.con.execute("INSERT INTO Files (id, path) VALUES (?, ?)", (id, path))
323+
return id
326324

325+
def index(self, path: str) -> str:
327326
# create new record
328327
id = self._create(path)
329328
self.con.commit()
@@ -613,8 +612,17 @@ def _sync_file(self, path, stat_info):
613612
"SELECT id, path, crtime FROM Files WHERE ino = ?", (stat_info.ino,)
614613
).fetchone()
615614

615+
616616
# if ino is not in database, return None
617617
if src is None:
618+
# If this path previously existed, but was removed OOB,
619+
# then there will be an outdated record that we should
620+
# delete here to avoid old file ID's from being returned
621+
# anywhere.
622+
row = self.con.execute("SELECT id FROM Files WHERE path = ?", (path,)).fetchone()
623+
if row:
624+
old_id = row[0]
625+
self.con.execute("DELETE FROM Files where id = ?", (old_id,))
618626
return None
619627
id, old_path, crtime = src
620628

@@ -626,6 +634,7 @@ def _sync_file(self, path, stat_info):
626634
# otherwise update existing record with new path, moving any indexed
627635
# children if necessary. then return its id
628636
self._update(id, path=path)
637+
629638
if stat_info.is_dir and old_path != path:
630639
self._move_recursive(old_path, path)
631640
self._update_cursor = True
@@ -672,6 +681,13 @@ def _create(self, path, stat_info):
672681
dangerous and may throw a runtime error if the file is not guaranteed to
673682
have a unique `ino`.
674683
"""
684+
row = self.con.execute("SELECT id FROM Files WHERE path = ?", (path,)).fetchone()
685+
existing_id = row and row[0]
686+
687+
# If the file ID already exists, update it with stat_info.
688+
if existing_id:
689+
return existing_id
690+
675691
id = self._uuid()
676692
self.con.execute(
677693
"INSERT INTO Files (id, path, ino, crtime, mtime, is_dir) VALUES (?, ?, ?, ?, ?, ?)",

0 commit comments

Comments
 (0)