Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-introduce bulk_create on supported databases #972

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions reversion/revisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.apps import apps
from django.core import serializers
from django.core.exceptions import ObjectDoesNotExist
from django.db import models, transaction, router
from django.db import models, transaction, router, connections
from django.db.models.query import QuerySet
from django.db.models.signals import post_save, m2m_changed
from django.utils.encoding import force_str
Expand Down Expand Up @@ -211,6 +211,7 @@ def add_to_revision(obj, model_db=None):

def _save_revision(versions, user=None, comment="", meta=(), date_created=None, using=None):
from reversion.models import Revision
from reversion.models import Version
# Only save versions that exist in the database.
# Use _base_manager so we don't have problems when _default_manager is overriden
model_db_pks = defaultdict(lambda: defaultdict(set))
Expand Down Expand Up @@ -248,9 +249,17 @@ def _save_revision(versions, user=None, comment="", meta=(), date_created=None,
# Save the revision.
revision.save(using=using)
# Save version models.

can_use_bulk_create = connections[using].features.can_return_rows_from_bulk_insert

for version in versions:
version.revision = revision
version.save(using=using)
if not can_use_bulk_create:
version.save(using=using)

if can_use_bulk_create:
Version.objects.using(using).bulk_create(versions)

# Save the meta information.
for meta_model, meta_fields in meta:
meta_model._base_manager.db_manager(using=using).create(
Expand Down
Loading