j
This commit is contained in:
@@ -512,36 +512,46 @@ def import_pending_sidecars(db_root: Path, db: Any) -> None:
|
||||
if not base_path.exists():
|
||||
continue
|
||||
|
||||
# Ensure file entry exists
|
||||
file_id: Optional[int] = None
|
||||
# Ensure file entry exists (folder store schema is keyed by hash).
|
||||
file_hash_value: Optional[str] = None
|
||||
if sha256_file and base_path.exists():
|
||||
try:
|
||||
file_hash_value = sha256_file(base_path)
|
||||
except Exception:
|
||||
file_hash_value = None
|
||||
|
||||
if not file_hash_value:
|
||||
continue
|
||||
|
||||
try:
|
||||
db_file_path = (
|
||||
db._to_db_file_path(base_path) # type: ignore[attr-defined]
|
||||
if hasattr(db, "_to_db_file_path")
|
||||
else str(base_path)
|
||||
)
|
||||
except Exception:
|
||||
db_file_path = str(base_path)
|
||||
|
||||
try:
|
||||
file_modified = float(base_path.stat().st_mtime)
|
||||
except Exception:
|
||||
file_modified = None
|
||||
|
||||
try:
|
||||
cursor = db.connection.cursor() if db.connection else None
|
||||
if cursor:
|
||||
cursor.execute(
|
||||
"SELECT id FROM files WHERE file_path = ?",
|
||||
(str(base_path),
|
||||
)
|
||||
"SELECT hash FROM file WHERE file_path = ?",
|
||||
(str(db_file_path),),
|
||||
)
|
||||
result = cursor.fetchone()
|
||||
file_id = result[0] if result else None
|
||||
except Exception:
|
||||
file_id = None
|
||||
|
||||
if not file_id:
|
||||
try:
|
||||
cursor = db.connection.cursor() if db.connection else None
|
||||
if cursor:
|
||||
if not result:
|
||||
cursor.execute(
|
||||
'INSERT INTO files (file_path, indexed_at, updated_at) VALUES (?, datetime("now"), datetime("now"))',
|
||||
(str(base_path),
|
||||
),
|
||||
"INSERT INTO file (hash, file_path, file_modified) VALUES (?, ?, ?)",
|
||||
(file_hash_value, str(db_file_path), file_modified),
|
||||
)
|
||||
db.connection.commit()
|
||||
file_id = cursor.lastrowid
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if not file_id:
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if sidecar_path.suffix == ".tag":
|
||||
@@ -557,15 +567,9 @@ def import_pending_sidecars(db_root: Path, db: Any) -> None:
|
||||
try:
|
||||
cursor = db.connection.cursor() if db.connection else None
|
||||
if cursor:
|
||||
file_hash_value: Optional[str] = None
|
||||
if hasattr(db, "get_file_hash"):
|
||||
try:
|
||||
file_hash_value = db.get_file_hash(file_id)
|
||||
except Exception:
|
||||
file_hash_value = None
|
||||
for tag in tags:
|
||||
cursor.execute(
|
||||
"INSERT OR IGNORE INTO tags (hash, tag) VALUES (?, ?)",
|
||||
"INSERT OR IGNORE INTO tag (hash, tag) VALUES (?, ?)",
|
||||
(file_hash_value,
|
||||
tag),
|
||||
)
|
||||
@@ -608,13 +612,15 @@ def import_pending_sidecars(db_root: Path, db: Any) -> None:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not hash_value:
|
||||
hash_value = file_hash_value
|
||||
|
||||
try:
|
||||
cursor = db.connection.cursor() if db.connection else None
|
||||
if cursor:
|
||||
cursor.execute(
|
||||
'INSERT OR REPLACE INTO metadata (file_id, hash, url, relationships, time_imported, time_modified) VALUES (?, ?, ?, ?, datetime("now"), datetime("now"))',
|
||||
'INSERT OR REPLACE INTO metadata (hash, url, relationships, time_imported, time_modified) VALUES (?, ?, ?, datetime("now"), datetime("now"))',
|
||||
(
|
||||
file_id,
|
||||
hash_value,
|
||||
json.dumps(url),
|
||||
json.dumps(relationships),
|
||||
@@ -634,9 +640,8 @@ def import_pending_sidecars(db_root: Path, db: Any) -> None:
|
||||
cursor = db.connection.cursor() if db.connection else None
|
||||
if cursor:
|
||||
cursor.execute(
|
||||
'INSERT INTO notes (file_id, note, created_at, updated_at) VALUES (?, ?, datetime("now"), datetime("now")) ON CONFLICT(file_id) DO UPDATE SET note = excluded.note, updated_at = datetime("now")',
|
||||
(file_id,
|
||||
content),
|
||||
'INSERT INTO note (hash, name, note, created_at, updated_at) VALUES (?, ?, ?, datetime("now"), datetime("now")) ON CONFLICT(hash, name) DO UPDATE SET note = excluded.note, updated_at = datetime("now")',
|
||||
(file_hash_value, "default", content),
|
||||
)
|
||||
db.connection.commit()
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user