Spaces:
Sleeping

File size: 2,099 Bytes
f7690ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""
Script para añadir la columna `qr_path` a la tabla `diploma` si no existe.

Uso:
  python scripts\add_qr_column.py

Esto asume que la URI de la BD está en `config.Config.SQLALCHEMY_DATABASE_URI`.
Si usas otra BD que no sea SQLite, revisa el script antes de ejecutarlo.
"""
import sqlite3
import os
import sys

from config import Config


def get_sqlite_path(uri: str) -> str:
    # support sqlite:///relative/path.db or sqlite:////absolute/path.db
    if not uri.startswith("sqlite:"):
        raise RuntimeError("Este script solo soporta sqlite URIs (ej: sqlite:///data.db)")
    # strip prefix
    if uri.startswith("sqlite:///"):
        return uri.replace("sqlite:///", "")
    if uri.startswith("sqlite:////"):
        return uri.replace("sqlite:////", "/")
    # fallback
    return uri.split("sqlite://")[-1]


def main():
    uri = getattr(Config, "SQLALCHEMY_DATABASE_URI", None)
    if not uri:
        print("No se encontró SQLALCHEMY_DATABASE_URI en config.Config", file=sys.stderr)
        sys.exit(1)

    try:
        db_path = get_sqlite_path(uri)
    except RuntimeError as e:
        print("ERROR:", e, file=sys.stderr)
        sys.exit(1)

    if not os.path.exists(db_path):
        print(f"Archivo de BD no encontrado: {db_path}", file=sys.stderr)
        sys.exit(1)

    conn = sqlite3.connect(db_path)
    try:
        cur = conn.cursor()
        cur.execute("PRAGMA table_info('diploma')")
        cols = [r[1] for r in cur.fetchall()]
        if 'qr_path' in cols:
            print("La columna 'qr_path' ya existe. No se realizaron cambios.")
            return

        # Add column (nullable) — safe for SQLite
        print("Añadiendo columna 'qr_path' a la tabla 'diploma'...")
        cur.execute("ALTER TABLE diploma ADD COLUMN qr_path VARCHAR(255);")
        conn.commit()
        print("Columna 'qr_path' añadida correctamente.")
    except sqlite3.OperationalError as e:
        print("Error operativo de sqlite:", e, file=sys.stderr)
        sys.exit(2)
    finally:
        conn.close()


if __name__ == '__main__':
    main()