Skip to content

Demo the deadlock #11

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 2 additions & 0 deletions django_bulk_load/bulk_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ def bulk_update_models(
else:
queries = [update_query]

helper = f"select id from {table_name} order by id asc for update"
queries = [helper] + queries
return bulk_load_models_with_queries(
models=models,
loading_table_name=loading_table_name,
Expand Down
44 changes: 34 additions & 10 deletions tests/test_bulk_update_models.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
from datetime import datetime, timezone

from django.test import TestCase
from django.db import close_old_connections
from django.test import TestCase, TransactionTestCase
from django_bulk_load import bulk_update_models, generate_greater_than_condition
import queue
from .test_project.models import (
TestComplexModel,
TestForeignKeyModel,
)


class E2ETestBulkUpdateModels(TestCase):
import random
import threading
import sys

def do_update(bucket):
objects = [x for x in TestComplexModel.objects.all()]
for current in objects:
current.integer_field = random.randint(10,100000)
try:
bulk_update_models(objects)
except Exception:
bucket.put(sys.exc_info())
close_old_connections()

class E2ETestBulkUpdateModels(TransactionTestCase):
def test_integer_field_change(self):
model1 = TestComplexModel(integer_field=1)
model1.save()
model1.integer_field = 2
bulk_update_models([model1])
saved_model = TestComplexModel.objects.get()
self.assertEqual(saved_model.integer_field, 2)
models = [TestComplexModel(integer_field=1) for x in range(10000)]
error_bucket = queue.Queue()

for model in models:
model.save()
threads = [
threading.Thread(target=do_update, args=[error_bucket])
for x in range(40)
]
for t in threads:
t.start()
for t in threads:
t.join()
while not error_bucket.empty():
raise error_bucket.get()[1]


def test_string_field_change(self):
model1 = TestComplexModel(string_field="hello")
Expand Down
Loading