mirror of
https://github.com/django/django.git
synced 2025-07-22 20:55:17 +00:00
Fixed #30023 -- Prevented SQLite schema alterations while foreign key checks are enabled.
Prior to this change foreign key constraint references could be left pointing at tables dropped during operations simulating unsupported table alterations because of an unexpected failure to disable foreign key constraint checks. SQLite3 does not allow disabling such checks while in a transaction so they must be disabled beforehand. Thanks ezaquarii for the report and Carlton and Tim for the review.
This commit is contained in:
parent
a394289b58
commit
315357ad25
8 changed files with 89 additions and 42 deletions
|
@ -2,7 +2,7 @@ import re
|
|||
import threading
|
||||
import unittest
|
||||
|
||||
from django.db import connection
|
||||
from django.db import connection, transaction
|
||||
from django.db.models import Avg, StdDev, Sum, Variance
|
||||
from django.db.models.fields import CharField
|
||||
from django.db.utils import NotSupportedError
|
||||
|
@ -16,22 +16,6 @@ from ..models import Author, Item, Object, Square
|
|||
class Tests(TestCase):
|
||||
longMessage = True
|
||||
|
||||
def test_autoincrement(self):
|
||||
"""
|
||||
auto_increment fields are created with the AUTOINCREMENT keyword
|
||||
in order to be monotonically increasing (#10164).
|
||||
"""
|
||||
with connection.schema_editor(collect_sql=True) as editor:
|
||||
editor.create_model(Square)
|
||||
statements = editor.collected_sql
|
||||
match = re.search('"id" ([^,]+),', statements[0])
|
||||
self.assertIsNotNone(match)
|
||||
self.assertEqual(
|
||||
'integer NOT NULL PRIMARY KEY AUTOINCREMENT',
|
||||
match.group(1),
|
||||
'Wrong SQL used to create an auto-increment column on SQLite'
|
||||
)
|
||||
|
||||
def test_aggregation(self):
|
||||
"""
|
||||
Raise NotImplementedError when aggregating on date/time fields (#19360).
|
||||
|
@ -82,6 +66,52 @@ class SchemaTests(TransactionTestCase):
|
|||
|
||||
available_apps = ['backends']
|
||||
|
||||
def test_autoincrement(self):
|
||||
"""
|
||||
auto_increment fields are created with the AUTOINCREMENT keyword
|
||||
in order to be monotonically increasing (#10164).
|
||||
"""
|
||||
with connection.schema_editor(collect_sql=True) as editor:
|
||||
editor.create_model(Square)
|
||||
statements = editor.collected_sql
|
||||
match = re.search('"id" ([^,]+),', statements[0])
|
||||
self.assertIsNotNone(match)
|
||||
self.assertEqual(
|
||||
'integer NOT NULL PRIMARY KEY AUTOINCREMENT',
|
||||
match.group(1),
|
||||
'Wrong SQL used to create an auto-increment column on SQLite'
|
||||
)
|
||||
|
||||
def test_disable_constraint_checking_failure_disallowed(self):
|
||||
"""
|
||||
SQLite schema editor is not usable within an outer transaction if
|
||||
foreign key constraint checks are not disabled beforehand.
|
||||
"""
|
||||
msg = (
|
||||
'SQLite schema editor cannot be used while foreign key '
|
||||
'constraint checks are enabled. Make sure to disable them '
|
||||
'before entering a transaction.atomic() context because '
|
||||
'SQLite3 does not support disabling them in the middle of '
|
||||
'a multi-statement transaction.'
|
||||
)
|
||||
with self.assertRaisesMessage(NotSupportedError, msg):
|
||||
with transaction.atomic(), connection.schema_editor(atomic=True):
|
||||
pass
|
||||
|
||||
def test_constraint_checks_disabled_atomic_allowed(self):
|
||||
"""
|
||||
SQLite3 schema editor is usable within an outer transaction as long as
|
||||
foreign key constraints checks are disabled beforehand.
|
||||
"""
|
||||
def constraint_checks_enabled():
|
||||
with connection.cursor() as cursor:
|
||||
return bool(cursor.execute('PRAGMA foreign_keys').fetchone()[0])
|
||||
with connection.constraint_checks_disabled(), transaction.atomic():
|
||||
with connection.schema_editor(atomic=True):
|
||||
self.assertFalse(constraint_checks_enabled())
|
||||
self.assertFalse(constraint_checks_enabled())
|
||||
self.assertTrue(constraint_checks_enabled())
|
||||
|
||||
def test_field_rename_inside_atomic_block(self):
|
||||
"""
|
||||
NotImplementedError is raised when a model field rename is attempted
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue