def undo_form_edits(form_tuples, logger):
    cases_to_rebuild = defaultdict(set)
    ledgers_to_rebuild = defaultdict(set)
    operation_date = datetime.utcnow()
    for live_form, deprecated_form in form_tuples:
        # undo corehq.form_processor.parsers.form.apply_deprecation
        case_cache = CaseDbCacheSQL(live_form.domain,
                                    load_src="undo_form_edits")
        live_case_updates = get_case_updates(live_form)
        deprecated_case_updates = get_case_updates(deprecated_form)
        case_cache.populate(
            set(cu.id for cu in live_case_updates)
            | set(cu.id for cu in deprecated_case_updates))

        deprecated_form.form_id = new_id_in_same_dbalias(
            deprecated_form.form_id)
        deprecated_form.state = XFormInstanceSQL.NORMAL
        deprecated_form.orig_id = None
        deprecated_form.edited_on = None

        live_form.deprecated_form_id = None
        live_form.received_on = live_form.edited_on
        live_form.edited_on = None

        affected_cases, affected_ledgers = update_case_transactions_for_form(
            case_cache, live_case_updates, deprecated_case_updates, live_form,
            deprecated_form)

        for form in (live_form, deprecated_form):
            form.track_create(
                XFormOperationSQL(user_id='system',
                                  operation=XFormOperationSQL.UUID_DATA_FIX,
                                  date=operation_date))
            FormAccessorSQL.update_form(form)

        logger.log('Form edit undone: {}, {}({})'.format(
            live_form.form_id, deprecated_form.form_id,
            deprecated_form.original_form_id))
        cases_to_rebuild[live_form.domain].update(affected_cases)
        ledgers_to_rebuild[live_form.domain].update(affected_ledgers)
        logger.log('Cases to rebuild: {}'.format(','.join(affected_cases)))
        logger.log('Ledgers to rebuild: {}'.format(','.join(
            [l.as_id() for l in affected_ledgers])))

    return cases_to_rebuild, ledgers_to_rebuild
def undo_form_edits(form_tuples, logger):
    cases_to_rebuild = defaultdict(set)
    ledgers_to_rebuild = defaultdict(set)
    operation_date = datetime.utcnow()
    for live_form, deprecated_form in form_tuples:
        # undo corehq.form_processor.parsers.form.apply_deprecation
        case_cache = CaseDbCacheSQL(live_form.domain)
        live_case_updates = get_case_updates(live_form)
        deprecated_case_updates = get_case_updates(deprecated_form)
        case_cache.populate(
            set(cu.id for cu in live_case_updates) | set(cu.id for cu in deprecated_case_updates)
        )

        deprecated_form.form_id = new_id_in_same_dbalias(deprecated_form.form_id)
        deprecated_form.state = XFormInstanceSQL.NORMAL
        deprecated_form.orig_id = None
        deprecated_form.edited_on = None

        live_form.deprecated_form_id = None
        live_form.received_on = live_form.edited_on
        live_form.edited_on = None

        affected_cases, affected_ledgers = update_case_transactions_for_form(
            case_cache, live_case_updates, deprecated_case_updates, live_form, deprecated_form
        )

        for form in (live_form, deprecated_form):
            form.track_create(XFormOperationSQL(
                user_id='system',
                operation=XFormOperationSQL.UUID_DATA_FIX,
                date=operation_date)
            )
            FormAccessorSQL.update_form(form)

        logger.log('Form edit undone: {}, {}({})'.format(
            live_form.form_id, deprecated_form.form_id, deprecated_form.original_form_id
        ))
        cases_to_rebuild[live_form.domain].update(affected_cases)
        ledgers_to_rebuild[live_form.domain].update(affected_ledgers)
        logger.log('Cases to rebuild: {}'.format(','.join(affected_cases)))
        logger.log('Ledgers to rebuild: {}'.format(','.join([l.as_id() for l in affected_ledgers])))

    return cases_to_rebuild, ledgers_to_rebuild
Exemple #3
0
 def test_sql_wrap_support(self):
     CaseDbCacheSQL(domain='some-domain', wrap=True)
     with self.assertRaises(ValueError):
         # invalid
         CaseDbCacheSQL(domain='some-domain', wrap=False)