Coverage for rfpy/api/fix/orphan_questions.py: 100%
10 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-31 16:00 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-31 16:00 +0000
1from sqlalchemy.orm import Session
3from rfpy.model.questionnaire import QuestionDefinition, QuestionInstance
6def delete_orphan_questions(session: Session, dry_run=True):
7 '''
8 Find, and optionally delete if dry_run is False, question definitions that have no
9 associated question instances - orphans.
11 If dry_run is False question definitions and child question_elements are deleted.
12 '''
13 q = session.query(QuestionDefinition)\
14 .outerjoin(QuestionInstance)\
15 .filter(QuestionInstance.id==None) # noqa E711
16 orphans = []
17 for qd in q:
18 if not dry_run:
19 session.delete(qd)
20 orphans.append({'question_def_id': qd.id, 'refcount': qd.refcount})
22 return orphans