Skip to content

Commit ea5b2de

Browse files
committed
Revisión de errores de la nube
1 parent 1eb7e2b commit ea5b2de

File tree

2 files changed

+159
-86
lines changed

2 files changed

+159
-86
lines changed

db_manager.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,37 @@ def _create_tables(self):
9292
def _check_migrations(self):
9393
"""
9494
Verifica si la base de datos existente necesita actualizaciones de estructura
95-
(por ejemplo, si el usuario viene de una versión anterior sin 'is_hidden').
95+
(por ejemplo, si el usuario viene de una versión anterior).
9696
"""
9797
try:
9898
with self.conn:
99-
# --- Migración FOTOS ---
99+
# --- Migración FOTOS (Añadir is_hidden) ---
100100
cursor = self.conn.execute("PRAGMA table_info(photos)")
101101
columns_photos = [col['name'] for col in cursor.fetchall()]
102102

103103
if 'is_hidden' not in columns_photos:
104104
print("INFO: Migrando base de datos... Añadiendo columna 'is_hidden' a photos.")
105105
self.conn.execute("ALTER TABLE photos ADD COLUMN is_hidden INTEGER DEFAULT 0")
106106

107-
# --- Migración VÍDEOS ---
107+
# --- Migración VÍDEOS (Añadir is_hidden) ---
108108
cursor = self.conn.execute("PRAGMA table_info(videos)")
109109
columns_videos = [col['name'] for col in cursor.fetchall()]
110110

111111
if 'is_hidden' not in columns_videos:
112112
print("INFO: Migrando base de datos... Añadiendo columna 'is_hidden' a videos.")
113113
self.conn.execute("ALTER TABLE videos ADD COLUMN is_hidden INTEGER DEFAULT 0")
114+
115+
# --- Migración DRIVE (Añadir root_folder_id) ---
116+
# Primero verificamos si la tabla drive_photos existe (puede que sea una instalación nueva)
117+
cursor = self.conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='drive_photos'")
118+
if cursor.fetchone():
119+
cursor = self.conn.execute("PRAGMA table_info(drive_photos)")
120+
columns_drive = [col['name'] for col in cursor.fetchall()]
121+
122+
if 'root_folder_id' not in columns_drive:
123+
print("INFO: Migrando BD Drive... Añadiendo columna 'root_folder_id' a drive_photos.")
124+
self.conn.execute("ALTER TABLE drive_photos ADD COLUMN root_folder_id TEXT")
125+
114126
except Exception as e:
115127
print(f"ERROR en migraciones de BD: {e}")
116128

@@ -358,38 +370,43 @@ def get_faces_for_person(self, person_id):
358370
""", (person_id,))
359371
return cursor.fetchall()
360372

361-
def get_all_drive_photos(self):
362-
"""Recupera todas las fotos de Drive guardadas para carga instantánea."""
363-
cursor = self.conn.execute("SELECT * FROM drive_photos")
364-
# Convertimos a lista de dicts para que sea compatible con el resto del código
373+
def get_all_drive_photos(self, root_folder_id=None):
374+
"""Recupera fotos de Drive, opcionalmente filtradas por carpeta raíz."""
375+
if root_folder_id:
376+
# Si nos pasan un ID, filtramos solo las fotos de esa carpeta
377+
cursor = self.conn.execute("SELECT * FROM drive_photos WHERE root_folder_id = ?", (root_folder_id,))
378+
else:
379+
# Si no (comportamiento antiguo), devolvemos todo
380+
cursor = self.conn.execute("SELECT * FROM drive_photos")
381+
365382
return [dict(row) for row in cursor.fetchall()]
366383

367-
def bulk_upsert_drive_photos(self, photos_list):
368-
"""Guarda o actualiza fotos de Drive masivamente."""
384+
def bulk_upsert_drive_photos(self, photos_list, root_folder_id=None):
385+
"""Guarda fotos de Drive vinculadas a una carpeta raíz."""
369386
if not photos_list: return
370387

371-
# Preparamos los datos para SQLite
372388
data_to_insert = []
373389
for p in photos_list:
374-
# Extraer parent_id si existe (Drive lo da como lista)
390+
# Extraer padre (Drive lo da como lista, cogemos el primero o string vacío)
375391
parent = p.get('parents', [''])[0] if p.get('parents') else ''
376392

377393
data_to_insert.append((
378394
p.get('id'),
379395
p.get('name'),
380-
p.get('createdTime'), # Ojo: Drive usa 'createdTime'
396+
p.get('createdTime'),
381397
p.get('mimeType'),
382398
p.get('thumbnailLink'),
383399
p.get('webContentLink'),
384-
parent
400+
parent,
401+
root_folder_id # <--- Guardamos el ID de la carpeta origen
385402
))
386403

387404
with self.conn:
388405
self.conn.executemany("""
389-
INSERT INTO drive_photos (id, name, created_time, mime_type, thumbnail_link, web_content_link, parent_id)
390-
VALUES (?, ?, ?, ?, ?, ?, ?)
406+
INSERT INTO drive_photos (id, name, created_time, mime_type, thumbnail_link, web_content_link, parent_id, root_folder_id)
407+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
391408
ON CONFLICT(id) DO UPDATE SET
392409
name=excluded.name,
393410
thumbnail_link=excluded.thumbnail_link,
394-
web_content_link=excluded.web_content_link
411+
root_folder_id=excluded.root_folder_id
395412
""", data_to_insert)

0 commit comments

Comments
 (0)