@transaction_atomic_with_retry()<EOL><INDENT>def upsert_entities(self, entities, sync=False):<DEDENT>
<EOL>if entities:<EOL><INDENT>select_for_update_query = (<EOL>'<STR_LIT>'<EOL>).format(<EOL>table_name=Entity._meta.db_table<EOL>)<EOL>select_for_update_query_params = []<EOL>if not sync:<EOL><INDENT>select_for_update_query = (<EOL>'<STR_LIT>'<EOL>).format(<EOL>table_name=Entity._meta.db_table<EOL>)<EOL>select_for_update_query_params = [tuple(<EOL>(entity.entity_type_id, entity.entity_id)<EOL>for entity in entities<EOL>)]<EOL><DEDENT>with connection.cursor() as cursor:<EOL><INDENT>cursor.execute(select_for_update_query, select_for_update_query_params)<EOL><DEDENT><DEDENT>if sync:<EOL><INDENT>upserted_entities = manager_utils.sync(<EOL>queryset=Entity.all_objects.all(),<EOL>model_objs=entities,<EOL>unique_fields=['<STR_LIT>', '<STR_LIT>'],<EOL>update_fields=['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'],<EOL>return_upserts=True<EOL>)<EOL><DEDENT>else:<EOL><INDENT>upserted_entities = manager_utils.bulk_upsert(<EOL>queryset=Entity.all_objects.extra(<EOL>where=['<STR_LIT>'],<EOL>params=[tuple(<EOL>(entity.entity_type_id, entity.entity_id)<EOL>for entity in entities<EOL>)]<EOL>),<EOL>model_objs=entities,<EOL>unique_fields=['<STR_LIT>', '<STR_LIT>'],<EOL>update_fields=['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'],<EOL>return_upserts=True<EOL>)<EOL><DEDENT>return upserted_entities<EOL>
Upsert a list of entities to the database
:param entities: The entities to sync
:param sync: Do a sync instead of an upsert