2015-01-28 22:55:52 +08:00
|
|
|
import threading
|
2011-04-21 04:42:07 +08:00
|
|
|
import time
|
2017-01-20 01:16:04 +08:00
|
|
|
from unittest import mock
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from multiple_database.routers import TestRouter
|
|
|
|
|
2017-06-30 04:00:15 +08:00
|
|
|
from django.core.exceptions import FieldError
|
2015-11-16 12:14:55 +08:00
|
|
|
from django.db import (
|
2022-02-04 03:24:19 +08:00
|
|
|
DatabaseError,
|
|
|
|
NotSupportedError,
|
|
|
|
connection,
|
|
|
|
connections,
|
|
|
|
router,
|
2017-04-07 20:08:07 +08:00
|
|
|
transaction,
|
2015-11-16 12:14:55 +08:00
|
|
|
)
|
2014-11-30 01:09:40 +08:00
|
|
|
from django.test import (
|
2022-02-04 03:24:19 +08:00
|
|
|
TransactionTestCase,
|
|
|
|
override_settings,
|
|
|
|
skipIfDBFeature,
|
2014-11-30 01:09:40 +08:00
|
|
|
skipUnlessDBFeature,
|
|
|
|
)
|
2016-06-24 00:06:55 +08:00
|
|
|
from django.test.utils import CaptureQueriesContext
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2020-08-08 13:17:36 +08:00
|
|
|
from .models import (
|
2022-02-04 03:24:19 +08:00
|
|
|
City,
|
|
|
|
CityCountryProxy,
|
|
|
|
Country,
|
|
|
|
EUCity,
|
|
|
|
EUCountry,
|
|
|
|
Person,
|
|
|
|
PersonProfile,
|
2020-08-08 13:17:36 +08:00
|
|
|
)
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2011-04-21 04:42:07 +08:00
|
|
|
class SelectForUpdateTests(TransactionTestCase):
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
available_apps = ["select_for_update"]
|
2013-06-04 14:09:29 +08:00
|
|
|
|
2011-04-21 04:42:07 +08:00
|
|
|
def setUp(self):
|
2014-03-21 21:21:43 +08:00
|
|
|
# This is executed in autocommit mode so that code in
|
|
|
|
# run_select_for_update can see this data.
|
2022-02-04 03:24:19 +08:00
|
|
|
self.country1 = Country.objects.create(name="Belgium")
|
|
|
|
self.country2 = Country.objects.create(name="France")
|
|
|
|
self.city1 = City.objects.create(name="Liberchies", country=self.country1)
|
|
|
|
self.city2 = City.objects.create(name="Samois-sur-Seine", country=self.country2)
|
|
|
|
self.person = Person.objects.create(
|
|
|
|
name="Reinhardt", born=self.city1, died=self.city2
|
|
|
|
)
|
2017-10-17 11:28:00 +08:00
|
|
|
self.person_profile = PersonProfile.objects.create(person=self.person)
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2014-03-21 21:21:43 +08:00
|
|
|
# We need another database connection in transaction to test that one
|
|
|
|
# connection issuing a SELECT ... FOR UPDATE will block.
|
2015-09-08 04:10:31 +08:00
|
|
|
self.new_connection = connection.copy()
|
2011-04-21 04:42:07 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
try:
|
|
|
|
self.end_blocking_transaction()
|
|
|
|
except (DatabaseError, AttributeError):
|
|
|
|
pass
|
2014-03-21 21:21:43 +08:00
|
|
|
self.new_connection.close()
|
2011-04-21 04:42:07 +08:00
|
|
|
|
|
|
|
def start_blocking_transaction(self):
|
2014-03-21 21:21:43 +08:00
|
|
|
self.new_connection.set_autocommit(False)
|
2011-04-21 04:42:07 +08:00
|
|
|
# Start a blocking transaction. At some point,
|
|
|
|
# end_blocking_transaction() should be called.
|
|
|
|
self.cursor = self.new_connection.cursor()
|
2022-02-04 03:24:19 +08:00
|
|
|
sql = "SELECT * FROM %(db_table)s %(for_update)s;" % {
|
|
|
|
"db_table": Person._meta.db_table,
|
|
|
|
"for_update": self.new_connection.ops.for_update_sql(),
|
2013-10-18 17:02:43 +08:00
|
|
|
}
|
2011-04-21 04:42:07 +08:00
|
|
|
self.cursor.execute(sql, ())
|
2012-05-27 07:24:57 +08:00
|
|
|
self.cursor.fetchone()
|
2011-04-21 04:42:07 +08:00
|
|
|
|
|
|
|
def end_blocking_transaction(self):
|
|
|
|
# Roll back the blocking transaction.
|
2017-08-17 00:39:58 +08:00
|
|
|
self.cursor.close()
|
2013-02-20 09:11:54 +08:00
|
|
|
self.new_connection.rollback()
|
2014-03-21 21:21:43 +08:00
|
|
|
self.new_connection.set_autocommit(True)
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2016-06-23 23:52:14 +08:00
|
|
|
def has_for_update_sql(self, queries, **kwargs):
|
2011-04-21 04:42:07 +08:00
|
|
|
# Examine the SQL that was executed to determine whether it
|
|
|
|
# contains the 'SELECT..FOR UPDATE' stanza.
|
2016-06-23 23:52:14 +08:00
|
|
|
for_update_sql = connection.ops.for_update_sql(**kwargs)
|
2022-02-04 03:24:19 +08:00
|
|
|
return any(for_update_sql in query["sql"] for query in queries)
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2011-04-21 04:42:07 +08:00
|
|
|
def test_for_update_sql_generated(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The backend's FOR UPDATE variant appears in
|
2011-04-21 04:42:07 +08:00
|
|
|
generated SQL when select_for_update is invoked.
|
|
|
|
"""
|
2016-06-24 00:06:55 +08:00
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
2014-03-31 01:03:35 +08:00
|
|
|
list(Person.objects.all().select_for_update())
|
2016-06-24 00:06:55 +08:00
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries))
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_nowait")
|
2011-04-21 04:42:07 +08:00
|
|
|
def test_for_update_sql_generated_nowait(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The backend's FOR UPDATE NOWAIT variant appears in
|
2011-04-21 04:42:07 +08:00
|
|
|
generated SQL when select_for_update is invoked.
|
|
|
|
"""
|
2016-06-24 00:06:55 +08:00
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
2014-03-31 01:03:35 +08:00
|
|
|
list(Person.objects.all().select_for_update(nowait=True))
|
2016-06-24 00:06:55 +08:00
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries, nowait=True))
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_skip_locked")
|
2016-06-23 23:52:14 +08:00
|
|
|
def test_for_update_sql_generated_skip_locked(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The backend's FOR UPDATE SKIP LOCKED variant appears in
|
2016-06-23 23:52:14 +08:00
|
|
|
generated SQL when select_for_update is invoked.
|
|
|
|
"""
|
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
|
|
|
list(Person.objects.all().select_for_update(skip_locked=True))
|
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries, skip_locked=True))
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_no_key_update")
|
2020-05-11 00:25:06 +08:00
|
|
|
def test_update_sql_generated_no_key(self):
|
|
|
|
"""
|
|
|
|
The backend's FOR NO KEY UPDATE variant appears in generated SQL when
|
|
|
|
select_for_update() is invoked.
|
|
|
|
"""
|
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
|
|
|
list(Person.objects.all().select_for_update(no_key=True))
|
|
|
|
self.assertIs(self.has_for_update_sql(ctx.captured_queries, no_key=True), True)
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2017-06-30 04:00:15 +08:00
|
|
|
def test_for_update_sql_generated_of(self):
|
|
|
|
"""
|
|
|
|
The backend's FOR UPDATE OF variant appears in the generated SQL when
|
|
|
|
select_for_update() is invoked.
|
|
|
|
"""
|
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
2022-02-04 03:24:19 +08:00
|
|
|
list(
|
|
|
|
Person.objects.select_related(
|
|
|
|
"born__country",
|
|
|
|
)
|
|
|
|
.select_for_update(
|
|
|
|
of=("born__country",),
|
|
|
|
)
|
|
|
|
.select_for_update(of=("self", "born__country"))
|
|
|
|
)
|
|
|
|
features = connections["default"].features
|
2017-06-30 04:00:15 +08:00
|
|
|
if features.select_for_update_of_column:
|
2020-02-08 12:52:09 +08:00
|
|
|
expected = [
|
|
|
|
'select_for_update_person"."id',
|
|
|
|
'select_for_update_country"."entity_ptr_id',
|
|
|
|
]
|
2017-06-30 04:00:15 +08:00
|
|
|
else:
|
2022-02-04 03:24:19 +08:00
|
|
|
expected = ["select_for_update_person", "select_for_update_country"]
|
2018-11-21 16:06:50 +08:00
|
|
|
expected = [connection.ops.quote_name(value) for value in expected]
|
2017-06-30 04:00:15 +08:00
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries, of=expected))
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2019-12-02 14:57:19 +08:00
|
|
|
def test_for_update_sql_model_inheritance_generated_of(self):
|
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
2022-02-04 03:24:19 +08:00
|
|
|
list(EUCountry.objects.select_for_update(of=("self",)))
|
2019-12-02 14:57:19 +08:00
|
|
|
if connection.features.select_for_update_of_column:
|
|
|
|
expected = ['select_for_update_eucountry"."country_ptr_id']
|
|
|
|
else:
|
2022-02-04 03:24:19 +08:00
|
|
|
expected = ["select_for_update_eucountry"]
|
2019-12-02 14:57:19 +08:00
|
|
|
expected = [connection.ops.quote_name(value) for value in expected]
|
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries, of=expected))
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2019-12-02 14:57:19 +08:00
|
|
|
def test_for_update_sql_model_inheritance_ptr_generated_of(self):
|
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
2022-02-04 03:24:19 +08:00
|
|
|
list(
|
|
|
|
EUCountry.objects.select_for_update(
|
|
|
|
of=(
|
|
|
|
"self",
|
|
|
|
"country_ptr",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2019-12-02 14:57:19 +08:00
|
|
|
if connection.features.select_for_update_of_column:
|
|
|
|
expected = [
|
|
|
|
'select_for_update_eucountry"."country_ptr_id',
|
2020-02-08 12:52:09 +08:00
|
|
|
'select_for_update_country"."entity_ptr_id',
|
2019-12-02 14:57:19 +08:00
|
|
|
]
|
|
|
|
else:
|
2022-02-04 03:24:19 +08:00
|
|
|
expected = ["select_for_update_eucountry", "select_for_update_country"]
|
2019-12-02 14:57:19 +08:00
|
|
|
expected = [connection.ops.quote_name(value) for value in expected]
|
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries, of=expected))
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2020-02-08 12:52:09 +08:00
|
|
|
def test_for_update_sql_related_model_inheritance_generated_of(self):
|
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
2022-02-04 03:24:19 +08:00
|
|
|
list(
|
|
|
|
EUCity.objects.select_related("country").select_for_update(
|
|
|
|
of=("self", "country"),
|
|
|
|
)
|
|
|
|
)
|
2020-02-08 12:52:09 +08:00
|
|
|
if connection.features.select_for_update_of_column:
|
|
|
|
expected = [
|
|
|
|
'select_for_update_eucity"."id',
|
|
|
|
'select_for_update_eucountry"."country_ptr_id',
|
|
|
|
]
|
|
|
|
else:
|
2022-02-04 03:24:19 +08:00
|
|
|
expected = ["select_for_update_eucity", "select_for_update_eucountry"]
|
2020-02-08 12:52:09 +08:00
|
|
|
expected = [connection.ops.quote_name(value) for value in expected]
|
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries, of=expected))
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2019-12-02 14:57:19 +08:00
|
|
|
def test_for_update_sql_model_inheritance_nested_ptr_generated_of(self):
|
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
2022-02-04 03:24:19 +08:00
|
|
|
list(
|
|
|
|
EUCity.objects.select_related("country").select_for_update(
|
|
|
|
of=(
|
|
|
|
"self",
|
|
|
|
"country__country_ptr",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
2019-12-02 14:57:19 +08:00
|
|
|
if connection.features.select_for_update_of_column:
|
|
|
|
expected = [
|
|
|
|
'select_for_update_eucity"."id',
|
2020-02-08 12:52:09 +08:00
|
|
|
'select_for_update_country"."entity_ptr_id',
|
2019-12-02 14:57:19 +08:00
|
|
|
]
|
|
|
|
else:
|
2022-02-04 03:24:19 +08:00
|
|
|
expected = ["select_for_update_eucity", "select_for_update_country"]
|
2019-12-02 14:57:19 +08:00
|
|
|
expected = [connection.ops.quote_name(value) for value in expected]
|
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries, of=expected))
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2020-02-08 12:52:09 +08:00
|
|
|
def test_for_update_sql_multilevel_model_inheritance_ptr_generated_of(self):
|
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
2022-02-04 03:24:19 +08:00
|
|
|
list(
|
|
|
|
EUCountry.objects.select_for_update(
|
|
|
|
of=("country_ptr", "country_ptr__entity_ptr"),
|
|
|
|
)
|
|
|
|
)
|
2020-02-08 12:52:09 +08:00
|
|
|
if connection.features.select_for_update_of_column:
|
|
|
|
expected = [
|
|
|
|
'select_for_update_country"."entity_ptr_id',
|
|
|
|
'select_for_update_entity"."id',
|
|
|
|
]
|
|
|
|
else:
|
2022-02-04 03:24:19 +08:00
|
|
|
expected = ["select_for_update_country", "select_for_update_entity"]
|
2020-02-08 12:52:09 +08:00
|
|
|
expected = [connection.ops.quote_name(value) for value in expected]
|
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries, of=expected))
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2020-08-08 13:17:36 +08:00
|
|
|
def test_for_update_sql_model_proxy_generated_of(self):
|
|
|
|
with transaction.atomic(), CaptureQueriesContext(connection) as ctx:
|
2022-02-04 03:24:19 +08:00
|
|
|
list(
|
|
|
|
CityCountryProxy.objects.select_related("country",).select_for_update(
|
|
|
|
of=("country",),
|
|
|
|
)
|
|
|
|
)
|
2020-08-08 13:17:36 +08:00
|
|
|
if connection.features.select_for_update_of_column:
|
|
|
|
expected = ['select_for_update_country"."entity_ptr_id']
|
|
|
|
else:
|
2022-02-04 03:24:19 +08:00
|
|
|
expected = ["select_for_update_country"]
|
2020-08-08 13:17:36 +08:00
|
|
|
expected = [connection.ops.quote_name(value) for value in expected]
|
|
|
|
self.assertTrue(self.has_for_update_sql(ctx.captured_queries, of=expected))
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2017-12-23 17:35:08 +08:00
|
|
|
def test_for_update_of_followed_by_values(self):
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
values = list(Person.objects.select_for_update(of=("self",)).values("pk"))
|
|
|
|
self.assertEqual(values, [{"pk": self.person.pk}])
|
2017-12-23 17:35:08 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2017-12-23 17:35:08 +08:00
|
|
|
def test_for_update_of_followed_by_values_list(self):
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
values = list(
|
|
|
|
Person.objects.select_for_update(of=("self",)).values_list("pk")
|
|
|
|
)
|
2017-12-23 17:35:08 +08:00
|
|
|
self.assertEqual(values, [(self.person.pk,)])
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_of")
|
2017-12-23 17:35:08 +08:00
|
|
|
def test_for_update_of_self_when_self_is_not_selected(self):
|
|
|
|
"""
|
|
|
|
select_for_update(of=['self']) when the only columns selected are from
|
|
|
|
related tables.
|
|
|
|
"""
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
values = list(
|
|
|
|
Person.objects.select_related("born")
|
|
|
|
.select_for_update(of=("self",))
|
|
|
|
.values("born__name")
|
|
|
|
)
|
|
|
|
self.assertEqual(values, [{"born__name": self.city1.name}])
|
2017-12-23 17:35:08 +08:00
|
|
|
|
2021-11-03 20:28:04 +08:00
|
|
|
@skipUnlessDBFeature(
|
2022-02-04 03:24:19 +08:00
|
|
|
"has_select_for_update_of",
|
|
|
|
"supports_select_for_update_with_limit",
|
2021-11-03 20:28:04 +08:00
|
|
|
)
|
|
|
|
def test_for_update_of_with_exists(self):
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
qs = Person.objects.select_for_update(of=("self", "born"))
|
2021-11-03 20:28:04 +08:00
|
|
|
self.assertIs(qs.exists(), True)
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_nowait")
|
2011-04-21 04:42:07 +08:00
|
|
|
def test_nowait_raises_error_on_block(self):
|
|
|
|
"""
|
|
|
|
If nowait is specified, we expect an error to be raised rather
|
|
|
|
than blocking.
|
|
|
|
"""
|
|
|
|
self.start_blocking_transaction()
|
|
|
|
status = []
|
2013-02-20 09:11:54 +08:00
|
|
|
|
2011-04-21 04:42:07 +08:00
|
|
|
thread = threading.Thread(
|
|
|
|
target=self.run_select_for_update,
|
|
|
|
args=(status,),
|
2022-02-04 03:24:19 +08:00
|
|
|
kwargs={"nowait": True},
|
2011-04-21 04:42:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
thread.start()
|
|
|
|
time.sleep(1)
|
|
|
|
thread.join()
|
|
|
|
self.end_blocking_transaction()
|
2013-03-06 22:33:35 +08:00
|
|
|
self.assertIsInstance(status[-1], DatabaseError)
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update_skip_locked")
|
2016-06-23 23:52:14 +08:00
|
|
|
def test_skip_locked_skips_locked_rows(self):
|
|
|
|
"""
|
|
|
|
If skip_locked is specified, the locked row is skipped resulting in
|
|
|
|
Person.DoesNotExist.
|
|
|
|
"""
|
|
|
|
self.start_blocking_transaction()
|
|
|
|
status = []
|
|
|
|
thread = threading.Thread(
|
|
|
|
target=self.run_select_for_update,
|
|
|
|
args=(status,),
|
2022-02-04 03:24:19 +08:00
|
|
|
kwargs={"skip_locked": True},
|
2016-06-23 23:52:14 +08:00
|
|
|
)
|
|
|
|
thread.start()
|
|
|
|
time.sleep(1)
|
|
|
|
thread.join()
|
|
|
|
self.end_blocking_transaction()
|
|
|
|
self.assertIsInstance(status[-1], Person.DoesNotExist)
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipIfDBFeature("has_select_for_update_nowait")
|
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2011-04-21 04:42:07 +08:00
|
|
|
def test_unsupported_nowait_raises_error(self):
|
|
|
|
"""
|
2017-04-11 23:52:53 +08:00
|
|
|
NotSupportedError is raised if a SELECT...FOR UPDATE NOWAIT is run on
|
2016-08-09 00:28:37 +08:00
|
|
|
a database backend that supports FOR UPDATE but not NOWAIT.
|
2011-04-21 04:42:07 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotSupportedError, "NOWAIT is not supported on this database backend."
|
|
|
|
):
|
2016-08-09 00:28:37 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
Person.objects.select_for_update(nowait=True).get()
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipIfDBFeature("has_select_for_update_skip_locked")
|
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2016-06-23 23:52:14 +08:00
|
|
|
def test_unsupported_skip_locked_raises_error(self):
|
|
|
|
"""
|
2017-04-11 23:52:53 +08:00
|
|
|
NotSupportedError is raised if a SELECT...FOR UPDATE SKIP LOCKED is run
|
|
|
|
on a database backend that supports FOR UPDATE but not SKIP LOCKED.
|
2016-06-23 23:52:14 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotSupportedError, "SKIP LOCKED is not supported on this database backend."
|
|
|
|
):
|
2016-06-23 23:52:14 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
Person.objects.select_for_update(skip_locked=True).get()
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipIfDBFeature("has_select_for_update_of")
|
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2017-06-30 04:00:15 +08:00
|
|
|
def test_unsupported_of_raises_error(self):
|
|
|
|
"""
|
|
|
|
NotSupportedError is raised if a SELECT...FOR UPDATE OF... is run on
|
|
|
|
a database backend that supports FOR UPDATE but not OF.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
msg = "FOR UPDATE OF is not supported on this database backend."
|
2017-06-30 04:00:15 +08:00
|
|
|
with self.assertRaisesMessage(NotSupportedError, msg):
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
Person.objects.select_for_update(of=("self",)).get()
|
2017-06-30 04:00:15 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipIfDBFeature("has_select_for_no_key_update")
|
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2020-05-11 00:25:06 +08:00
|
|
|
def test_unsuported_no_key_raises_error(self):
|
|
|
|
"""
|
|
|
|
NotSupportedError is raised if a SELECT...FOR NO KEY UPDATE... is run
|
|
|
|
on a database backend that supports FOR UPDATE but not NO KEY.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
msg = "FOR NO KEY UPDATE is not supported on this database backend."
|
2020-05-11 00:25:06 +08:00
|
|
|
with self.assertRaisesMessage(NotSupportedError, msg):
|
|
|
|
with transaction.atomic():
|
|
|
|
Person.objects.select_for_update(no_key=True).get()
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update", "has_select_for_update_of")
|
2017-06-30 04:00:15 +08:00
|
|
|
def test_unrelated_of_argument_raises_error(self):
|
|
|
|
"""
|
|
|
|
FieldError is raised if a non-relation field is specified in of=(...).
|
|
|
|
"""
|
|
|
|
msg = (
|
2022-02-04 03:24:19 +08:00
|
|
|
"Invalid field name(s) given in select_for_update(of=(...)): %s. "
|
|
|
|
"Only relational fields followed in the query are allowed. "
|
|
|
|
"Choices are: self, born, born__country, "
|
|
|
|
"born__country__entity_ptr."
|
2017-06-30 04:00:15 +08:00
|
|
|
)
|
|
|
|
invalid_of = [
|
2022-02-04 03:24:19 +08:00
|
|
|
("nonexistent",),
|
|
|
|
("name",),
|
|
|
|
("born__nonexistent",),
|
|
|
|
("born__name",),
|
|
|
|
("born__nonexistent", "born__name"),
|
2017-06-30 04:00:15 +08:00
|
|
|
]
|
|
|
|
for of in invalid_of:
|
|
|
|
with self.subTest(of=of):
|
2022-02-04 03:24:19 +08:00
|
|
|
with self.assertRaisesMessage(FieldError, msg % ", ".join(of)):
|
2017-06-30 04:00:15 +08:00
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
Person.objects.select_related(
|
|
|
|
"born__country"
|
|
|
|
).select_for_update(of=of).get()
|
2017-06-30 04:00:15 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update", "has_select_for_update_of")
|
2017-06-30 04:00:15 +08:00
|
|
|
def test_related_but_unselected_of_argument_raises_error(self):
|
|
|
|
"""
|
|
|
|
FieldError is raised if a relation field that is not followed in the
|
|
|
|
query is specified in of=(...).
|
|
|
|
"""
|
|
|
|
msg = (
|
2022-02-04 03:24:19 +08:00
|
|
|
"Invalid field name(s) given in select_for_update(of=(...)): %s. "
|
|
|
|
"Only relational fields followed in the query are allowed. "
|
|
|
|
"Choices are: self, born, profile."
|
2017-06-30 04:00:15 +08:00
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
for name in ["born__country", "died", "died__country"]:
|
2017-06-30 04:00:15 +08:00
|
|
|
with self.subTest(name=name):
|
|
|
|
with self.assertRaisesMessage(FieldError, msg % name):
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
Person.objects.select_related("born", "profile",).exclude(
|
|
|
|
profile=None
|
|
|
|
).select_for_update(of=(name,)).get()
|
2017-10-17 11:28:00 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update", "has_select_for_update_of")
|
2019-12-02 14:57:19 +08:00
|
|
|
def test_model_inheritance_of_argument_raises_error_ptr_in_choices(self):
|
|
|
|
msg = (
|
2022-02-04 03:24:19 +08:00
|
|
|
"Invalid field name(s) given in select_for_update(of=(...)): "
|
|
|
|
"name. Only relational fields followed in the query are allowed. "
|
|
|
|
"Choices are: self, %s."
|
2019-12-02 14:57:19 +08:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
FieldError,
|
2022-02-04 03:24:19 +08:00
|
|
|
msg % "country, country__country_ptr, country__country_ptr__entity_ptr",
|
2019-12-02 14:57:19 +08:00
|
|
|
):
|
|
|
|
with transaction.atomic():
|
|
|
|
EUCity.objects.select_related(
|
2022-02-04 03:24:19 +08:00
|
|
|
"country",
|
|
|
|
).select_for_update(of=("name",)).get()
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
FieldError, msg % "country_ptr, country_ptr__entity_ptr"
|
|
|
|
):
|
2019-12-02 14:57:19 +08:00
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
EUCountry.objects.select_for_update(of=("name",)).get()
|
2019-12-02 14:57:19 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update", "has_select_for_update_of")
|
2020-08-08 13:17:36 +08:00
|
|
|
def test_model_proxy_of_argument_raises_error_proxy_field_in_choices(self):
|
|
|
|
msg = (
|
2022-02-04 03:24:19 +08:00
|
|
|
"Invalid field name(s) given in select_for_update(of=(...)): "
|
|
|
|
"name. Only relational fields followed in the query are allowed. "
|
|
|
|
"Choices are: self, country, country__entity_ptr."
|
2020-08-08 13:17:36 +08:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
with transaction.atomic():
|
|
|
|
CityCountryProxy.objects.select_related(
|
2022-02-04 03:24:19 +08:00
|
|
|
"country",
|
|
|
|
).select_for_update(of=("name",)).get()
|
2020-08-08 13:17:36 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update", "has_select_for_update_of")
|
2017-10-17 11:28:00 +08:00
|
|
|
def test_reverse_one_to_one_of_arguments(self):
|
|
|
|
"""
|
|
|
|
Reverse OneToOneFields may be included in of=(...) as long as NULLs
|
|
|
|
are excluded because LEFT JOIN isn't allowed in SELECT FOR UPDATE.
|
|
|
|
"""
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
person = (
|
|
|
|
Person.objects.select_related(
|
|
|
|
"profile",
|
|
|
|
)
|
|
|
|
.exclude(profile=None)
|
|
|
|
.select_for_update(of=("profile",))
|
|
|
|
.get()
|
|
|
|
)
|
2017-10-17 11:28:00 +08:00
|
|
|
self.assertEqual(person.profile, self.person_profile)
|
2017-06-30 04:00:15 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2015-11-16 12:14:55 +08:00
|
|
|
def test_for_update_after_from(self):
|
2022-02-04 03:24:19 +08:00
|
|
|
features_class = connections["default"].features.__class__
|
|
|
|
attribute_to_patch = "%s.%s.for_update_after_from" % (
|
|
|
|
features_class.__module__,
|
|
|
|
features_class.__name__,
|
|
|
|
)
|
2015-11-16 12:14:55 +08:00
|
|
|
with mock.patch(attribute_to_patch, return_value=True):
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
self.assertIn(
|
|
|
|
"FOR UPDATE WHERE",
|
|
|
|
str(Person.objects.filter(name="foo").select_for_update().query),
|
|
|
|
)
|
2015-11-16 12:14:55 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2014-03-31 01:03:35 +08:00
|
|
|
def test_for_update_requires_transaction(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A TransactionManagementError is raised
|
2014-03-31 01:03:35 +08:00
|
|
|
when a select_for_update query is executed outside of a transaction.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
msg = "select_for_update cannot be used outside of a transaction."
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(transaction.TransactionManagementError, msg):
|
2014-03-31 01:03:35 +08:00
|
|
|
list(Person.objects.all().select_for_update())
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2014-03-31 01:03:35 +08:00
|
|
|
def test_for_update_requires_transaction_only_in_execution(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
No TransactionManagementError is raised
|
2014-03-31 01:03:35 +08:00
|
|
|
when select_for_update is invoked outside of a transaction -
|
|
|
|
only when the query is executed.
|
|
|
|
"""
|
|
|
|
people = Person.objects.all().select_for_update()
|
2022-02-04 03:24:19 +08:00
|
|
|
msg = "select_for_update cannot be used outside of a transaction."
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(transaction.TransactionManagementError, msg):
|
2014-03-31 01:03:35 +08:00
|
|
|
list(people)
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("supports_select_for_update_with_limit")
|
2017-04-07 20:08:07 +08:00
|
|
|
def test_select_for_update_with_limit(self):
|
2022-02-04 03:24:19 +08:00
|
|
|
other = Person.objects.create(name="Grappeli", born=self.city1, died=self.city2)
|
2017-04-07 20:08:07 +08:00
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
qs = list(Person.objects.all().order_by("pk").select_for_update()[1:2])
|
2017-04-07 20:08:07 +08:00
|
|
|
self.assertEqual(qs[0], other)
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipIfDBFeature("supports_select_for_update_with_limit")
|
2017-04-07 20:08:07 +08:00
|
|
|
def test_unsupported_select_for_update_with_limit(self):
|
2022-02-04 03:24:19 +08:00
|
|
|
msg = "LIMIT/OFFSET is not supported with select_for_update on this database backend."
|
2017-04-07 20:08:07 +08:00
|
|
|
with self.assertRaisesMessage(NotSupportedError, msg):
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
list(Person.objects.all().order_by("pk").select_for_update()[1:2])
|
2017-04-07 20:08:07 +08:00
|
|
|
|
2016-06-23 23:52:14 +08:00
|
|
|
def run_select_for_update(self, status, **kwargs):
|
2011-04-21 04:42:07 +08:00
|
|
|
"""
|
|
|
|
Utility method that runs a SELECT FOR UPDATE against all
|
|
|
|
Person instances. After the select_for_update, it attempts
|
|
|
|
to update the name of the only record, save, and commit.
|
|
|
|
|
2011-12-11 16:43:01 +08:00
|
|
|
This function expects to run in a separate thread.
|
2011-04-21 04:42:07 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
status.append("started")
|
2011-04-21 04:42:07 +08:00
|
|
|
try:
|
|
|
|
# We need to enter transaction management again, as this is done on
|
|
|
|
# per-thread basis
|
2014-03-21 21:21:43 +08:00
|
|
|
with transaction.atomic():
|
2016-06-23 23:52:14 +08:00
|
|
|
person = Person.objects.select_for_update(**kwargs).get()
|
2022-02-04 03:24:19 +08:00
|
|
|
person.name = "Fred"
|
2016-06-23 23:52:14 +08:00
|
|
|
person.save()
|
|
|
|
except (DatabaseError, Person.DoesNotExist) as e:
|
2011-04-21 04:42:07 +08:00
|
|
|
status.append(e)
|
2011-12-11 16:43:01 +08:00
|
|
|
finally:
|
|
|
|
# This method is run in a separate thread. It uses its own
|
|
|
|
# database connection. Close it without waiting for the GC.
|
|
|
|
connection.close()
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
|
|
|
@skipUnlessDBFeature("supports_transactions")
|
2011-04-21 04:42:07 +08:00
|
|
|
def test_block(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A thread running a select_for_update that accesses rows being touched
|
|
|
|
by a similar operation on another connection blocks correctly.
|
2011-04-21 04:42:07 +08:00
|
|
|
"""
|
|
|
|
# First, let's start the transaction in our thread.
|
|
|
|
self.start_blocking_transaction()
|
|
|
|
|
|
|
|
# Now, try it again using the ORM's select_for_update
|
|
|
|
# facility. Do this in a separate thread.
|
|
|
|
status = []
|
2022-02-04 03:24:19 +08:00
|
|
|
thread = threading.Thread(target=self.run_select_for_update, args=(status,))
|
2011-04-21 04:42:07 +08:00
|
|
|
|
|
|
|
# The thread should immediately block, but we'll sleep
|
|
|
|
# for a bit to make sure.
|
|
|
|
thread.start()
|
|
|
|
sanity_count = 0
|
|
|
|
while len(status) != 1 and sanity_count < 10:
|
|
|
|
sanity_count += 1
|
|
|
|
time.sleep(1)
|
|
|
|
if sanity_count >= 10:
|
2022-02-04 03:24:19 +08:00
|
|
|
raise ValueError("Thread did not run and block")
|
2011-04-21 04:42:07 +08:00
|
|
|
|
|
|
|
# Check the person hasn't been updated. Since this isn't
|
|
|
|
# using FOR UPDATE, it won't block.
|
|
|
|
p = Person.objects.get(pk=self.person.pk)
|
2022-02-04 03:24:19 +08:00
|
|
|
self.assertEqual("Reinhardt", p.name)
|
2011-04-21 04:42:07 +08:00
|
|
|
|
|
|
|
# When we end our blocking transaction, our thread should
|
|
|
|
# be able to continue.
|
|
|
|
self.end_blocking_transaction()
|
|
|
|
thread.join(5.0)
|
|
|
|
|
|
|
|
# Check the thread has finished. Assuming it has, we should
|
|
|
|
# find that it has updated the person's name.
|
2019-04-29 14:47:34 +08:00
|
|
|
self.assertFalse(thread.is_alive())
|
2011-09-11 01:29:33 +08:00
|
|
|
|
|
|
|
# We must commit the transaction to ensure that MySQL gets a fresh read,
|
|
|
|
# since by default it runs in REPEATABLE READ mode
|
|
|
|
transaction.commit()
|
|
|
|
|
2011-04-21 04:42:07 +08:00
|
|
|
p = Person.objects.get(pk=self.person.pk)
|
2022-02-04 03:24:19 +08:00
|
|
|
self.assertEqual("Fred", p.name)
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2011-04-21 04:42:07 +08:00
|
|
|
def test_raw_lock_not_available(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Running a raw query which can't obtain a FOR UPDATE lock raises
|
|
|
|
the correct exception
|
2011-04-21 04:42:07 +08:00
|
|
|
"""
|
|
|
|
self.start_blocking_transaction()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2011-04-21 04:42:07 +08:00
|
|
|
def raw(status):
|
|
|
|
try:
|
|
|
|
list(
|
|
|
|
Person.objects.raw(
|
2022-02-04 03:24:19 +08:00
|
|
|
"SELECT * FROM %s %s"
|
|
|
|
% (
|
2011-04-21 04:42:07 +08:00
|
|
|
Person._meta.db_table,
|
2022-02-04 03:24:19 +08:00
|
|
|
connection.ops.for_update_sql(nowait=True),
|
2011-04-21 04:42:07 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2012-04-29 00:09:37 +08:00
|
|
|
except DatabaseError as e:
|
2011-04-21 04:42:07 +08:00
|
|
|
status.append(e)
|
2011-12-11 16:43:01 +08:00
|
|
|
finally:
|
|
|
|
# This method is run in a separate thread. It uses its own
|
|
|
|
# database connection. Close it without waiting for the GC.
|
2017-08-17 00:39:58 +08:00
|
|
|
# Connection cannot be closed on Oracle because cursor is still
|
|
|
|
# open.
|
2022-02-04 03:24:19 +08:00
|
|
|
if connection.vendor != "oracle":
|
2017-08-17 00:39:58 +08:00
|
|
|
connection.close()
|
2011-12-11 16:43:01 +08:00
|
|
|
|
2011-04-21 04:42:07 +08:00
|
|
|
status = []
|
2022-02-04 03:24:19 +08:00
|
|
|
thread = threading.Thread(target=raw, kwargs={"status": status})
|
2011-04-21 04:42:07 +08:00
|
|
|
thread.start()
|
|
|
|
time.sleep(1)
|
|
|
|
thread.join()
|
|
|
|
self.end_blocking_transaction()
|
2013-03-06 22:33:35 +08:00
|
|
|
self.assertIsInstance(status[-1], DatabaseError)
|
2011-04-21 04:42:07 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2014-11-30 01:09:40 +08:00
|
|
|
@override_settings(DATABASE_ROUTERS=[TestRouter()])
|
2013-02-26 17:51:56 +08:00
|
|
|
def test_select_for_update_on_multidb(self):
|
2014-11-30 01:09:40 +08:00
|
|
|
query = Person.objects.select_for_update()
|
|
|
|
self.assertEqual(router.db_for_write(Person), query.db)
|
2014-08-01 06:36:06 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@skipUnlessDBFeature("has_select_for_update")
|
2014-08-01 06:36:06 +08:00
|
|
|
def test_select_for_update_with_get(self):
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
person = Person.objects.select_for_update().get(name="Reinhardt")
|
|
|
|
self.assertEqual(person.name, "Reinhardt")
|
2016-06-23 23:52:14 +08:00
|
|
|
|
|
|
|
def test_nowait_and_skip_locked(self):
|
2022-02-04 03:24:19 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValueError, "The nowait option cannot be used with skip_locked."
|
|
|
|
):
|
2016-06-23 23:52:14 +08:00
|
|
|
Person.objects.select_for_update(nowait=True, skip_locked=True)
|
2016-09-29 13:39:50 +08:00
|
|
|
|
|
|
|
def test_ordered_select_for_update(self):
|
|
|
|
"""
|
|
|
|
Subqueries should respect ordering as an ORDER BY clause may be useful
|
|
|
|
to specify a row locking order to prevent deadlocks (#27193).
|
|
|
|
"""
|
|
|
|
with transaction.atomic():
|
2022-02-04 03:24:19 +08:00
|
|
|
qs = Person.objects.filter(
|
|
|
|
id__in=Person.objects.order_by("-id").select_for_update()
|
|
|
|
)
|
|
|
|
self.assertIn("ORDER BY", str(qs.query))
|