Esempio n. 1
0
    def handle(self, *args, **options):
        """Main logic for the actual command."""
        is_simulation = options['simulate']
        only_print_queries = options['only_print_queries']
        model_name = options['model_label']

        model = apps.get_model(model_name)
        qs = self._get_query(model)

        if only_print_queries:
            self._print_queries(model, qs)
            return

        try:
            with ExitStack() as stack:
                if not is_simulation:
                    stack.enter_context(update_es_after_deletions())

                stack.enter_context(atomic())
                total_deleted, deletions_by_model = qs.delete()

                logger.info(f'{total_deleted} records deleted. Breakdown by model:')
                for deletion_model, model_deletion_count in deletions_by_model.items():
                    logger.info(f'{deletion_model}: {model_deletion_count}')

                if is_simulation:
                    logger.info('Rolling back deletions...')
                    raise SimulationRollback()
        except SimulationRollback:
            logger.info('Deletions rolled back')
Esempio n. 2
0
def test_update_es_after_deletions(setup_es):
    """
    Test that the context manager update_es_after_deletions collects and deletes
    all the django objects deleted.
    """
    obj = SimpleModel.objects.create()
    sync_object(SimpleModelSearchApp, str(obj.pk))
    setup_es.indices.refresh()
    read_alias = ESSimpleModel.get_read_alias()

    assert SimpleModel.objects.count() == 1
    assert setup_es.count(read_alias, doc_type=SimpleModelSearchApp.name)['count'] == 1

    with update_es_after_deletions():
        obj.delete()

    setup_es.indices.refresh()
    assert setup_es.count(read_alias, doc_type=SimpleModelSearchApp.name)['count'] == 0
Esempio n. 3
0
    def _process_row(self, row, simulate=False, **options):
        """Process one single row."""
        investment_project_id = row['id']
        investment_project = InvestmentProject.objects.get(pk=investment_project_id)

        try:
            with ExitStack() as stack:
                if not simulate:
                    stack.enter_context(update_es_after_deletions())

                stack.enter_context(atomic())
                total_deleted, deletions_by_model = investment_project.delete()
                logger.info((
                    f'{total_deleted} records deleted for investment project: '
                    f'{investment_project_id}. Breakdown by model:'
                ))
                for deletion_model, model_deletion_count in deletions_by_model.items():
                    logger.info(f'{deletion_model}: {model_deletion_count}')

                if simulate:
                    logger.info('Rolling back deletions...')
                    raise SimulationRollback()
        except SimulationRollback:
            logger.info('Deletions rolled back')