Skip to content

Commit

Permalink
Re-introduce bulk_create on supported databases
Browse files Browse the repository at this point in the history
This fixes etianen#924.
  • Loading branch information
stianjensen committed Jul 18, 2024
1 parent 6796b93 commit 23b2396
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion reversion/revisions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from contextvars import ContextVar
from django.db import connections
from collections import namedtuple, defaultdict
from contextlib import contextmanager
from functools import wraps
Expand Down Expand Up @@ -211,6 +212,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 +250,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 = connection[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()

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

0 comments on commit 23b2396

Please sign in to comment.