Files
admin-edu-space/backend/migrate_phase3_fields.py
T

70 lines
3.0 KiB
Python

"""
Migration script for Phase 3 MVP fields & tables:
- commissions.grades_closed (BOOLEAN DEFAULT 0)
- commissions.grades_closed_at (DATETIME)
- commissions.grades_closed_by (INTEGER)
- commissions.acta_number (VARCHAR(50))
- milestone_grades table
"""
import sqlite3
import os
def migrate():
db_paths = [
os.path.join(os.path.dirname(__file__), 'instance', 'app.db'),
os.path.join(os.path.dirname(__file__), 'app.db')
]
for db_path in db_paths:
if not os.path.exists(db_path):
continue
print(f"Migrating SQLite DB: {db_path}")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Check commissions columns
cursor.execute("PRAGMA table_info(commissions)")
columns = [row[1] for row in cursor.fetchall()]
if 'grades_closed' not in columns:
print("Adding column 'grades_closed' to commissions table...")
cursor.execute("ALTER TABLE commissions ADD COLUMN grades_closed BOOLEAN DEFAULT 0")
if 'grades_closed_at' not in columns:
print("Adding column 'grades_closed_at' to commissions table...")
cursor.execute("ALTER TABLE commissions ADD COLUMN grades_closed_at DATETIME")
if 'grades_closed_by' not in columns:
print("Adding column 'grades_closed_by' to commissions table...")
cursor.execute("ALTER TABLE commissions ADD COLUMN grades_closed_by INTEGER")
if 'acta_number' not in columns:
print("Adding column 'acta_number' to commissions table...")
cursor.execute("ALTER TABLE commissions ADD COLUMN acta_number VARCHAR(50)")
# Create milestone_grades table if not exists
cursor.execute("""
CREATE TABLE IF NOT EXISTS milestone_grades (
id INTEGER PRIMARY KEY AUTOINCREMENT,
milestone_id INTEGER NOT NULL,
enrollment_id INTEGER NOT NULL,
student_id INTEGER NOT NULL,
score FLOAT,
is_absent BOOLEAN DEFAULT 0,
feedback TEXT,
graded_by_user_id INTEGER,
graded_at DATETIME,
updated_at DATETIME,
FOREIGN KEY (milestone_id) REFERENCES academic_milestones(id) ON DELETE CASCADE,
FOREIGN KEY (enrollment_id) REFERENCES student_enrollments(id) ON DELETE CASCADE,
FOREIGN KEY (student_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (graded_by_user_id) REFERENCES users(id) ON DELETE SET NULL,
CONSTRAINT uq_milestone_student UNIQUE (milestone_id, student_id)
)
""")
cursor.execute("CREATE INDEX IF NOT EXISTS ix_milestone_grades_milestone_id ON milestone_grades (milestone_id)")
cursor.execute("CREATE INDEX IF NOT EXISTS ix_milestone_grades_enrollment_id ON milestone_grades (enrollment_id)")
cursor.execute("CREATE INDEX IF NOT EXISTS ix_milestone_grades_student_id ON milestone_grades (student_id)")
conn.commit()
conn.close()
print(f"Migration completed for {db_path}")
if __name__ == '__main__':
migrate()