<commit_before>from collections import defaultdict

from django.db import connections, models
from django.utils.functional import SimpleLazyObject


def bulk_update(objs, update_fields=None, exclude_fields=None,
                using='default', batch_size=None):
    assert batch_size is None or batch_size > 0

    batch_size = batch_size or len(objs)
    connection = connections[using]
    ref_obj = objs[0]
    meta = ref_obj._meta
    exclude_fields = exclude_fields or []
    update_fields = update_fields or meta.get_all_field_names()
    fields = list(filter(
        lambda f: (not isinstance(f, models.AutoField)) and
                  (f.attname in update_fields),
        meta.fields))
    fields = list(filter(lambda f: not f.attname in exclude_fields, fields))
	
    def _batched_update(objs, fields, batch_size, connection):
        def _get_db_type(field):
            if isinstance(field, (models.PositiveSmallIntegerField,
                                  models.PositiveIntegerField)):
                return field.db_type(connection).split(' ', 1)[0]
            return field.db_type(connection)

        pks = []
        case_clauses = defaultdict(dict)
        for obj in objs[:batch_size]:
            pks.append(obj.pk)
            for field in fields:
                column = field.column
                _default = SimpleLazyObject(
                    lambda: '{column} = CAST(CASE {pkcolumn} {{when}}'.format(
                        column=column, pkcolumn=meta.pk.column))

                case_clauses.setdefault(
                    column, {
                        'sql': _default,
                        'params': [],
                        'type': _get_db_type(field)
                    }
                )

                case_clauses[column]['sql'] = case_clauses[column]['sql']\
                    .format(when="WHEN %s THEN %s {when}")

                case_clauses[column]['params'].extend(
                    [obj.pk, field.get_db_prep_value(
                        getattr(obj, field.attname), connection)])<commit_msg>Updating datetimefield went wrong in sqlite<commit_after>from collections import defaultdict

from django.db import connections, models
from django.utils.functional import SimpleLazyObject


def bulk_update(objs, update_fields=None, exclude_fields=None,
                using='default', batch_size=None):
    assert batch_size is None or batch_size > 0

    batch_size = batch_size or len(objs)
    connection = connections[using]
    ref_obj = objs[0]
    meta = ref_obj._meta
    exclude_fields = exclude_fields or []
    update_fields = update_fields or meta.get_all_field_names()
    fields = list(filter(
        lambda f: (not isinstance(f, models.AutoField)) and
                  (f.attname in update_fields),
        meta.fields))
    fields = list(filter(lambda f: not f.attname in exclude_fields, fields))
	
    def _batched_update(objs, fields, batch_size, connection):
        def _get_db_type(field):
            if isinstance(field, (models.PositiveSmallIntegerField,
                                  models.PositiveIntegerField)):
                return field.db_type(connection).split(' ', 1)[0]
            elif isinstance(field, (models.DateTimeField, models.DateField)) and connection.vendor == 'sqlite':
                return 'text' # otherwise it turns 2015-03-10 to 2015
            return field.db_type(connection)

        pks = []
        case_clauses = defaultdict(dict)
        for obj in objs[:batch_size]:
            pks.append(obj.pk)
            for field in fields:
                column = field.column
                _default = SimpleLazyObject(
                    lambda: '{column} = CAST(CASE {pkcolumn} {{when}}'.format(
                        column=column, pkcolumn=meta.pk.column))

                case_clauses.setdefault(
                    column, {
                        'sql': _default,
                        'params': [],
                        'type': _get_db_type(field)
                    }
                )

                case_clauses[column]['sql'] = case_clauses[column]['sql']\
                    .format(when="WHEN %s THEN %s {when}")

                case_clauses[column]['params'].extend(
                    [obj.pk, field.get_db_prep_value(
                        getattr(obj, field.attname), connection)])
{'fraction_non_alphanumeric': 0.09761139182361048, 'fraction_numerical': 0.004593477262287551, 'mean_word_length': 1.926747311827957, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7518212', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1141, 'n_words': 263}