Bugfix in QuerySetMixin inheritance

This commit is contained in:
M1ha 2019-09-11 15:34:37 +05:00
parent 5f218d0da0
commit 0f1364b2b8

View File

@ -32,16 +32,18 @@ except ImportError:
fake = True fake = True
class ClickHouseSyncUpdateReturningQuerySetMixin(UpdateReturningMixin): class ClickHouseSyncRegisterMixin:
def _register_ops(self, operation, result):
pk_name = self.model._meta.pk.name
pk_list = [getattr(item, pk_name) for item in result]
self.model.register_clickhouse_operations(operation, *pk_list, using=self.db)
class ClickHouseSyncUpdateReturningQuerySetMixin(ClickHouseSyncRegisterMixin, UpdateReturningMixin):
""" """
This mixin adopts methods of django-pg-returning library This mixin adopts methods of django-pg-returning library
""" """
def _register_ops(self, operation, result):
pk_name = self.model._meta.pk.name
pk_list = result.values_list(pk_name, flat=True)
self.model.register_clickhouse_operations(operation, *pk_list, using=self.db)
def update_returning(self, **updates): def update_returning(self, **updates):
result = super().update_returning(**updates) result = super().update_returning(**updates)
self._register_ops('update', result) self._register_ops('update', result)
@ -53,7 +55,7 @@ class ClickHouseSyncUpdateReturningQuerySetMixin(UpdateReturningMixin):
return result return result
class ClickHouseSyncBulkUpdateQuerySetMixin(BulkUpdateManagerMixin): class ClickHouseSyncBulkUpdateQuerySetMixin(ClickHouseSyncRegisterMixin, BulkUpdateManagerMixin):
""" """
This mixin adopts methods of django-pg-bulk-update library This mixin adopts methods of django-pg-bulk-update library
""" """
@ -69,11 +71,6 @@ class ClickHouseSyncBulkUpdateQuerySetMixin(BulkUpdateManagerMixin):
return returning return returning
def _register_ops(self, result):
pk_name = self.model._meta.pk.name
pk_list = [getattr(item, pk_name) for item in result]
self.model.register_clickhouse_operations('update', *pk_list, using=self.db)
def bulk_update(self, *args, **kwargs): def bulk_update(self, *args, **kwargs):
original_returning = kwargs.pop('returning', None) original_returning = kwargs.pop('returning', None)
kwargs['returning'] = self._update_returning_param(original_returning) kwargs['returning'] = self._update_returning_param(original_returning)
@ -89,19 +86,18 @@ class ClickHouseSyncBulkUpdateQuerySetMixin(BulkUpdateManagerMixin):
return result.count() if original_returning is None else result return result.count() if original_returning is None else result
class ClickHouseSyncQuerySetMixin: class ClickHouseSyncQuerySetMixin(ClickHouseSyncRegisterMixin):
def update(self, **kwargs): def update(self, **kwargs):
# BUG I use update_returning method here. But it is not suitable for databases other then PostgreSQL # BUG I use update_returning method here. But it is not suitable for databases other then PostgreSQL
# and requires django-pg-update-returning installed # and requires django-pg-update-returning installed
pk_name = self.model._meta.pk.name pk_name = self.model._meta.pk.name
res = self.only(pk_name).update_returning(**kwargs).values_list(pk_name, flat=True) res = self.only(pk_name).update_returning(**kwargs)
self.model.register_clickhouse_operations('update', *res, using=self.db) self._register_ops('update', res)
return len(res) return len(res)
def bulk_create(self, objs, batch_size=None): def bulk_create(self, objs, batch_size=None):
objs = super().bulk_create(objs, batch_size=batch_size) objs = super().bulk_create(objs, batch_size=batch_size)
self.model.register_clickhouse_operations('insert', *[obj.pk for obj in objs], using=self.db) self._register_ops('insert', objs)
return objs return objs