File size: 1,583 Bytes
6aa6081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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")