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

Add ignore_conflicts to bulk_create_with_history #733

Merged
merged 4 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Authors
- `Paulo Peres <https://github.com/PauloPeres>`_
- `Alex Todorov <https://github.com/atodorov>`_
- David Smith (`smithdc1 <https://github.com/smithdc1>`_)
- Shi Han Ng (`shihanng <https://github.com/shihanng>`_)

Background
==========
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Unreleased
- Removed support for Django versions prior to 2.2 (gh-652)
- Migrate from TravisCI to Github Actions (gh-739)
- Add Python 3.9 support (gh-745)
- Support ``ignore_conflicts`` in ``bulk_create_with_history`` (gh-733)

2.12.0 (2020-10-14)
-------------------
Expand Down
7 changes: 7 additions & 0 deletions simple_history/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def get_absolute_url(self):
return reverse("poll-detail", kwargs={"pk": self.pk})


class PollWithUniqueQuestion(models.Model):
question = models.CharField(max_length=200, unique=True)
pub_date = models.DateTimeField("date published")

history = HistoricalRecords()


class PollWithExcludeFields(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField("date published")
Expand Down
33 changes: 32 additions & 1 deletion simple_history/tests/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime

from django.contrib.auth import get_user_model
from django.db import IntegrityError
from django.db import IntegrityError, transaction
from django.test import TestCase, TransactionTestCase
from django.utils import timezone
from unittest.mock import Mock, patch
Expand All @@ -14,6 +14,7 @@
Poll,
PollWithAlternativeManager,
PollWithExcludeFields,
PollWithUniqueQuestion,
Street,
)
from simple_history.utils import (
Expand Down Expand Up @@ -58,6 +59,17 @@ def setUp(self):
id=5, question="Question 5", pub_date=timezone.now()
),
]
self.data_with_duplicates = [
PollWithUniqueQuestion(
pk=1, question="Question 1", pub_date=timezone.now()
),
PollWithUniqueQuestion(
pk=2, question="Question 2", pub_date=timezone.now()
),
PollWithUniqueQuestion(
pk=3, question="Question 1", pub_date=timezone.now()
),
]

def test_bulk_create_history(self):
bulk_create_with_history(self.data, Poll)
Expand Down Expand Up @@ -148,6 +160,25 @@ def test_bulk_create_history_with_relation_name(self):
self.assertEqual(Street.objects.count(), 4)
self.assertEqual(Street.log.count(), 4)

def test_bulk_create_history_with_duplicates(self):
with transaction.atomic(), self.assertRaises(IntegrityError):
bulk_create_with_history(
self.data_with_duplicates,
PollWithUniqueQuestion,
ignore_conflicts=False,
)

self.assertEqual(PollWithUniqueQuestion.objects.count(), 0)
self.assertEqual(PollWithUniqueQuestion.history.count(), 0)

def test_bulk_create_history_with_duplicates_ignore_conflicts(self):
bulk_create_with_history(
self.data_with_duplicates, PollWithUniqueQuestion, ignore_conflicts=True
)

self.assertEqual(PollWithUniqueQuestion.objects.count(), 2)
self.assertEqual(PollWithUniqueQuestion.history.count(), 2)


class BulkCreateWithHistoryTransactionTestCase(TransactionTestCase):
def setUp(self):
Expand Down
7 changes: 5 additions & 2 deletions simple_history/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def bulk_create_with_history(
objs,
model,
batch_size=None,
ignore_conflicts=False,
default_user=None,
default_change_reason=None,
default_date=None,
Expand Down Expand Up @@ -82,8 +83,10 @@ def bulk_create_with_history(

second_transaction_required = True
with transaction.atomic(savepoint=False):
objs_with_id = model_manager.bulk_create(objs, batch_size=batch_size)
if objs_with_id and objs_with_id[0].pk:
objs_with_id = model_manager.bulk_create(
objs, batch_size=batch_size, ignore_conflicts=ignore_conflicts
)
if objs_with_id and objs_with_id[0].pk and not ignore_conflicts:
second_transaction_required = False
history_manager.bulk_history_create(
objs_with_id,
Expand Down