Purge old aliases & add them back

pull/177/head
Stijn De Clercq 2023-09-24 16:23:45 +02:00
parent ae28f3a286
commit 1fc7fc94d8
1 changed files with 28 additions and 1 deletions

View File

@ -1,5 +1,6 @@
from sqlalchemy import delete, select from sqlalchemy import delete, select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from database.engine import DBSession from database.engine import DBSession
from database.schemas import UforaCourse, UforaCourseAlias from database.schemas import UforaCourse, UforaCourseAlias
@ -7,6 +8,21 @@ from database.schemas import UforaCourse, UforaCourseAlias
__all__ = ["main"] __all__ = ["main"]
async def purge_aliases(session: AsyncSession):
"""Delete old aliases for courses that will get a new id"""
codes = ["C004074", "C004073", "C004075", "C002309"]
for course_code in codes:
select_stmt = (
select(UforaCourse).where(UforaCourse.code == course_code).options(selectinload(UforaCourse.aliases))
)
course: UforaCourse = (await session.execute(select_stmt)).scalar_one()
for alias in list(course.aliases):
await session.delete(alias)
await session.commit()
async def main(): async def main():
"""Add the Ufora courses for the 2023-2024 academic year""" """Add the Ufora courses for the 2023-2024 academic year"""
session: AsyncSession session: AsyncSession
@ -16,6 +32,9 @@ async def main():
await session.execute(delete_stmt) await session.execute(delete_stmt)
await session.commit() await session.commit()
# Delete aliases of courses with new IDs
await purge_aliases(session)
# Fix IDs of compulsory courses and enable announcements # Fix IDs of compulsory courses and enable announcements
select_stmt = select(UforaCourse).where(UforaCourse.code == "C004074") select_stmt = select(UforaCourse).where(UforaCourse.code == "C004074")
bds: UforaCourse = (await session.execute(select_stmt)).scalar_one() bds: UforaCourse = (await session.execute(select_stmt)).scalar_one()
@ -27,7 +46,7 @@ async def main():
cg: UforaCourse = (await session.execute(select_stmt)).scalar_one() cg: UforaCourse = (await session.execute(select_stmt)).scalar_one()
cg.course_id = 828293 cg.course_id = 828293
cg.log_announcements = True cg.log_announcements = True
session.add(bds) session.add(cg)
select_stmt = select(UforaCourse).where(UforaCourse.code == "C004075") select_stmt = select(UforaCourse).where(UforaCourse.code == "C004075")
stage: UforaCourse = (await session.execute(select_stmt)).scalar_one() stage: UforaCourse = (await session.execute(select_stmt)).scalar_one()
@ -43,6 +62,14 @@ async def main():
await session.commit() await session.commit()
# Add new aliases for these courses
cg_alias = UforaCourseAlias(course_id=cg.course_id, alias="Computer Graphics")
stage_alias = UforaCourseAlias(course_id=stage.course_id, alias="Stage")
thesis_alias = UforaCourseAlias(course_id=thesis.course_id, alias="Thesis")
session.add_all([cg_alias, stage_alias, thesis_alias])
await session.commit()
# New elective courses # New elective courses
bed_eco = UforaCourse( bed_eco = UforaCourse(
code="H001535", name="Bedrijfseconomie", year=6, compulsory=False, role_id=1155496199000952922 code="H001535", name="Bedrijfseconomie", year=6, compulsory=False, role_id=1155496199000952922