@@ -311,19 +311,18 @@ def _from_normalized_path(self, path: Optional[str]) -> Optional[str]:
311
311
return relpath
312
312
313
313
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 :
320
314
path = self ._normalize_path (path )
321
315
row = self .con .execute ("SELECT id FROM Files WHERE path = ?" , (path ,)).fetchone ()
322
316
existing_id = row and row [0 ]
323
317
324
318
if existing_id :
325
319
return existing_id
320
+
321
+ id = self ._uuid ()
322
+ self .con .execute ("INSERT INTO Files (id, path) VALUES (?, ?)" , (id , path ))
323
+ return id
326
324
325
+ def index (self , path : str ) -> str :
327
326
# create new record
328
327
id = self ._create (path )
329
328
self .con .commit ()
@@ -613,8 +612,17 @@ def _sync_file(self, path, stat_info):
613
612
"SELECT id, path, crtime FROM Files WHERE ino = ?" , (stat_info .ino ,)
614
613
).fetchone ()
615
614
615
+
616
616
# if ino is not in database, return None
617
617
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 ,))
618
626
return None
619
627
id , old_path , crtime = src
620
628
@@ -626,6 +634,7 @@ def _sync_file(self, path, stat_info):
626
634
# otherwise update existing record with new path, moving any indexed
627
635
# children if necessary. then return its id
628
636
self ._update (id , path = path )
637
+
629
638
if stat_info .is_dir and old_path != path :
630
639
self ._move_recursive (old_path , path )
631
640
self ._update_cursor = True
@@ -672,6 +681,13 @@ def _create(self, path, stat_info):
672
681
dangerous and may throw a runtime error if the file is not guaranteed to
673
682
have a unique `ino`.
674
683
"""
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
+
675
691
id = self ._uuid ()
676
692
self .con .execute (
677
693
"INSERT INTO Files (id, path, ino, crtime, mtime, is_dir) VALUES (?, ?, ?, ?, ?, ?)" ,
0 commit comments