|
10 | 10 | from pathlib import Path |
11 | 11 |
|
12 | 12 | class VisageVaultDB: |
13 | | - # Modificar __init__ para aceptar 'db_path' |
14 | 13 | def __init__(self, db_path=None, is_worker=False): |
| 14 | + # --- 1. INICIALIZACIÓN SEGURA (Variables por defecto) --- |
| 15 | + # Definimos esto PRIMERO para que existan aunque todo lo demás falle. |
| 16 | + self.conn = None |
| 17 | + self.meta_conn = None |
| 18 | + self.was_reset = False # <--- Esto arregla el AttributeError |
| 19 | + self.is_worker = is_worker |
15 | 20 |
|
16 | | - # 1. Gestión de la ruta de la BD |
| 21 | + # --- 2. CONFIGURACIÓN DE RUTA --- |
17 | 22 | if db_path: |
18 | | - # Si nos pasan una ruta (desde AUR/Linux), la usamos |
19 | 23 | self.db_path = db_path |
20 | 24 | else: |
21 | | - # Si no (Windows/Dev), usamos la local junto al script |
| 25 | + # Modo Dev/Windows |
22 | 26 | base_dir = os.path.dirname(os.path.abspath(__file__)) |
23 | 27 | self.db_path = os.path.join(base_dir, "visagevault.db") |
24 | 28 |
|
25 | | - # Asegurar que el directorio existe (por seguridad) |
26 | | - os.makedirs(os.path.dirname(self.db_path), exist_ok=True) |
27 | | - |
28 | | - self.is_worker = is_worker |
29 | | - self.conn = None |
30 | | - |
31 | | - # 2. Intentar conectar a la BD Principal |
| 29 | + # --- 3. CREACIÓN DE DIRECTORIOS --- |
32 | 30 | try: |
33 | | - self._connect_main_db() |
34 | | - |
35 | | - if not is_worker: |
36 | | - # CHEQUEO DE SALUD |
37 | | - if not self._check_integrity(): |
38 | | - raise sqlite3.DatabaseError("La base de datos está corrupta o es incompatible.") |
39 | | - |
40 | | - # Si llegamos aquí, la BD está sana. |
41 | | - # Hacemos backup de los datos actuales a la MetaDB por seguridad |
42 | | - self._sync_main_to_meta() |
43 | | - |
44 | | - self._create_tables() |
45 | | - self._check_migrations() |
46 | | - |
47 | | - except (sqlite3.DatabaseError, sqlite3.OperationalError) as e: |
48 | | - if is_worker: |
49 | | - # Un worker no debe intentar arreglar la BD, solo fallar |
50 | | - print(f"Worker falló al conectar DB: {e}") |
51 | | - raise e |
| 31 | + # Aseguramos que la carpeta donde va la DB existe (vital para ~/.local/share/...) |
| 32 | + db_dir = os.path.dirname(self.db_path) |
| 33 | + if db_dir and not os.path.exists(db_dir): |
| 34 | + os.makedirs(db_dir, exist_ok=True) |
| 35 | + except Exception as e: |
| 36 | + print(f"Error crítico creando directorio de DB: {e}") |
52 | 37 |
|
53 | | - print(f"❌ ERROR CRÍTICO EN BD: {e}") |
54 | | - print("🔄 Iniciando protocolo de AUTOREPARACIÓN...") |
55 | | - self._perform_hard_reset() |
| 38 | + # --- 4. CONEXIÓN --- |
| 39 | + # Solo conectamos si todo lo anterior fue bien |
| 40 | + if not self.is_worker: |
| 41 | + try: |
| 42 | + self._connect_main_db() |
| 43 | + self._init_db() |
| 44 | + except Exception as e: |
| 45 | + print(f"Error inicializando DB: {e}") |
56 | 46 |
|
57 | 47 | def _connect_main_db(self): |
58 | 48 | """Conexión estándar con optimizaciones.""" |
|
0 commit comments