Download scripts/add_qr_column.py from ed29/xertify: direct link, hf CLI and curl.
- Browser
- Download file 2.1 kB
-
https://huggingface.co/spaces/ed29/xertify/resolve/main/scripts/add_qr_column.py
- Command line
-
hf download hf://spaces/ed29/xertify/scripts/add_qr_column.py
-
curl -L -o add_qr_column.py https://huggingface.co/spaces/ed29/xertify/resolve/main/scripts/add_qr_column.py
2.1 kB
| #!/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() | |