from app.core.database import SessionLocal from app.models.section import Section from app.models.section_subject import SectionSubject from app.models.teacher_section import TeacherSection from app.models.teacher_subject import TeacherSubject from sqlalchemy import text db = SessionLocal() # Migrate sections -> section_subjects sections = db.query(Section).all() for sec in sections: existing = db.query(SectionSubject).filter( SectionSubject.section_id == sec.id, SectionSubject.subject_id == sec.subject_id ).first() if not existing and sec.subject_id: ss = SectionSubject( section_id=sec.id, subject_id=sec.subject_id, account_id=sec.account_id ) db.add(ss) print(f"SectionSubject: section={sec.id} subject={sec.subject_id}") # Migrate teacher_sections -> teacher_subjects ts_list = db.query(TeacherSection).all() for ts in ts_list: sec = db.query(Section).filter(Section.id == ts.section_id).first() if sec and sec.subject_id: existing = db.query(TeacherSubject).filter( TeacherSubject.teacher_id == ts.teacher_id, TeacherSubject.subject_id == sec.subject_id ).first() if not existing: tu = TeacherSubject( teacher_id=ts.teacher_id, subject_id=sec.subject_id, account_id=ts.account_id ) db.add(tu) print(f"TeacherSubject: teacher={ts.teacher_id} subject={sec.subject_id}") db.commit() db.close() print("Migration complete")