Spaces:
Sleeping

xertify / scripts /add_diploma_columns.py
Fedgo29
Adding changes by FedgoX
2ba84e9
Raw History Blame Contribute Delete
2.42 kB
#!/usr/bin/env python3
"""Add missing columns to the `diploma` table in the SQLite DB.
This script is idempotent and will only run ALTER TABLE for columns
that don't already exist. It expects the DB at the project root as
configured in `config.py` (default `data.db`).
Usage:
python scripts/add_diploma_columns.py
Backup your DB first if it contains important data.
"""
import sqlite3
import os
# Try multiple likely DB locations (project root and instance folder)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DB_CANDIDATES = [
os.path.join(BASE_DIR, "data.db"),
os.path.join(BASE_DIR, "instance", "data.db"),
]
def find_db_path():
for p in DB_CANDIDATES:
if os.path.exists(p):
return p
# fallback to first candidate (where seed/reset create it normally)
return DB_CANDIDATES[0]
DB_PATH = find_db_path()
# Columns to add to the `diploma` table if they don't already exist
columns_to_add = {
"student_name": "TEXT",
"student_id": "TEXT",
"center_name": "TEXT",
"certification_title": "TEXT",
"graduation_year": "INTEGER",
"registration_number": "TEXT",
"director": "TEXT",
"provincial_director": "TEXT",
"professor": "TEXT",
# store uploaded PDF filename/path (files are saved to `static/pdfs`)
"pdf": "TEXT",
}
def get_existing_columns(conn):
cur = conn.execute("PRAGMA table_info('diploma')")
return [row[1] for row in cur.fetchall()]
def main():
if not os.path.exists(DB_PATH):
print("Database not found. Tried these locations:")
for p in DB_CANDIDATES:
print(" -", p)
print("You can create the DB by running: python .\seed.py (or python .\reset_db.py to recreate)")
return
conn = sqlite3.connect(DB_PATH)
try:
existing = get_existing_columns(conn)
print("Existing columns:", existing)
for col, coltype in columns_to_add.items():
if col in existing:
print(f"Column '{col}' already exists — skipping")
continue
stmt = f"ALTER TABLE diploma ADD COLUMN {col} {coltype}"
print("Executing:", stmt)
conn.execute(stmt)
conn.commit()
print("Done — added missing columns.")
except sqlite3.OperationalError as e:
print("SQLite error:", e)
finally:
conn.close()
if __name__ == "__main__":
main()