Debian Codegraph @

tests/postgres_tests/test_operations.py

raw
1 import unittest
2
3 from migrations.test_base import OperationTestBase, OptimizerTestBase
4
5 from django.db import IntegrityError, NotSupportedError, connection, transaction
6 from django.db.migrations.operations import RemoveIndex, RenameIndex
7 from django.db.migrations.state import ProjectState
8 from django.db.migrations.writer import OperationWriter
9 from django.db.models import CheckConstraint, Index, Q, UniqueConstraint
10 from django.db.utils import ProgrammingError
11 from django.test import modify_settings, override_settings
12 from django.test.utils import CaptureQueriesContext
13
14 from . import PostgreSQLTestCase
15
16 try:
17 from django.contrib.postgres.indexes import BrinIndex, BTreeIndex
18 from django.contrib.postgres.operations import (
19 AddConstraintNotValid,
20 AddIndexConcurrently,
21 BloomExtension,
22 CreateCollation,
23 CreateExtension,
24 RemoveCollation,
25 RemoveIndexConcurrently,
26 ValidateConstraint,
27 )
28 except ImportError:
29 pass
30
31
32 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")
33 @modify_settings(INSTALLED_APPS={"append": "migrations"})
34 class AddIndexConcurrentlyTests(OptimizerTestBase, OperationTestBase):
35 app_label = "test_add_concurrently"
36
37 def test_requires_atomic_false(self):
38 project_state = self.set_up_test_model(self.app_label)
39 new_state = project_state.clone()
40 operation = AddIndexConcurrently(
41 "Pony",
42 Index(fields=["pink"], name="pony_pink_idx"),
43 )
44 msg = (
45 "The AddIndexConcurrently operation cannot be executed inside "
46 "a transaction (set atomic = False on the migration)."
47 )
48 with self.assertRaisesMessage(NotSupportedError, msg):
49 with connection.schema_editor(atomic=True) as editor:
50 operation.database_forwards(
51 self.app_label, editor, project_state, new_state
52 )
53
54 def test_add(self):
55 project_state = self.set_up_test_model(self.app_label, index=False)
56 table_name = "%s_pony" % self.app_label
57 index = Index(fields=["pink"], name="pony_pink_idx")
58 new_state = project_state.clone()
59 operation = AddIndexConcurrently("Pony", index)
60 self.assertEqual(
61 operation.describe(),
62 "Concurrently create index pony_pink_idx on field(s) pink of model Pony",
63 )
64 self.assertEqual(
65 operation.formatted_description(),
66 "+ Concurrently create index pony_pink_idx on field(s) pink of model Pony",
67 )
68 operation.state_forwards(self.app_label, new_state)
69 self.assertEqual(
70 len(new_state.models[self.app_label, "pony"].options["indexes"]), 1
71 )
72 self.assertIndexNotExists(table_name, ["pink"])
73 # Add index.
74 with connection.schema_editor(atomic=False) as editor:
75 operation.database_forwards(
76 self.app_label, editor, project_state, new_state
77 )
78 self.assertIndexExists(table_name, ["pink"])
79 # Reversal.
80 with connection.schema_editor(atomic=False) as editor:
81 operation.database_backwards(
82 self.app_label, editor, new_state, project_state
83 )
84 self.assertIndexNotExists(table_name, ["pink"])
85 # Deconstruction.
86 name, args, kwargs = operation.deconstruct()
87 self.assertEqual(name, "AddIndexConcurrently")
88 self.assertEqual(args, [])
89 self.assertEqual(kwargs, {"model_name": "Pony", "index": index})
90
91 def test_add_other_index_type(self):
92 project_state = self.set_up_test_model(self.app_label, index=False)
93 table_name = "%s_pony" % self.app_label
94 new_state = project_state.clone()
95 operation = AddIndexConcurrently(
96 "Pony",
97 BrinIndex(fields=["pink"], name="pony_pink_brin_idx"),
98 )
99 self.assertIndexNotExists(table_name, ["pink"])
100 # Add index.
101 with connection.schema_editor(atomic=False) as editor:
102 operation.database_forwards(
103 self.app_label, editor, project_state, new_state
104 )
105 self.assertIndexExists(table_name, ["pink"], index_type="brin")
106 # Reversal.
107 with connection.schema_editor(atomic=False) as editor:
108 operation.database_backwards(
109 self.app_label, editor, new_state, project_state
110 )
111 self.assertIndexNotExists(table_name, ["pink"])
112
113 def test_add_with_options(self):
114 project_state = self.set_up_test_model(self.app_label, index=False)
115 table_name = "%s_pony" % self.app_label
116 new_state = project_state.clone()
117 index = BTreeIndex(fields=["pink"], name="pony_pink_btree_idx", fillfactor=70)
118 operation = AddIndexConcurrently("Pony", index)
119 self.assertIndexNotExists(table_name, ["pink"])
120 # Add index.
121 with connection.schema_editor(atomic=False) as editor:
122 operation.database_forwards(
123 self.app_label, editor, project_state, new_state
124 )
125 self.assertIndexExists(table_name, ["pink"], index_type="btree")
126 # Reversal.
127 with connection.schema_editor(atomic=False) as editor:
128 operation.database_backwards(
129 self.app_label, editor, new_state, project_state
130 )
131 self.assertIndexNotExists(table_name, ["pink"])
132
133 def test_reduce_add_remove_concurrently(self):
134 self.assertOptimizesTo(
135 [
136 AddIndexConcurrently(
137 "Pony",
138 Index(fields=["pink"], name="pony_pink_idx"),
139 ),
140 RemoveIndex("Pony", "pony_pink_idx"),
141 ],
142 [],
143 )
144
145 def test_reduce_add_remove(self):
146 self.assertOptimizesTo(
147 [
148 AddIndexConcurrently(
149 "Pony",
150 Index(fields=["pink"], name="pony_pink_idx"),
151 ),
152 RemoveIndexConcurrently("Pony", "pony_pink_idx"),
153 ],
154 [],
155 )
156
157 def test_reduce_add_rename(self):
158 self.assertOptimizesTo(
159 [
160 AddIndexConcurrently(
161 "Pony",
162 Index(fields=["pink"], name="pony_pink_idx"),
163 ),
164 RenameIndex(
165 "Pony",
166 old_name="pony_pink_idx",
167 new_name="pony_pink_index",
168 ),
169 ],
170 [
171 AddIndexConcurrently(
172 "Pony",
173 Index(fields=["pink"], name="pony_pink_index"),
174 ),
175 ],
176 )
177
178
179 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")
180 @modify_settings(INSTALLED_APPS={"append": "migrations"})
181 class RemoveIndexConcurrentlyTests(OperationTestBase):
182 app_label = "test_rm_concurrently"
183
184 def test_requires_atomic_false(self):
185 project_state = self.set_up_test_model(self.app_label, index=True)
186 new_state = project_state.clone()
187 operation = RemoveIndexConcurrently("Pony", "pony_pink_idx")
188 msg = (
189 "The RemoveIndexConcurrently operation cannot be executed inside "
190 "a transaction (set atomic = False on the migration)."
191 )
192 with self.assertRaisesMessage(NotSupportedError, msg):
193 with connection.schema_editor(atomic=True) as editor:
194 operation.database_forwards(
195 self.app_label, editor, project_state, new_state
196 )
197
198 def test_remove(self):
199 project_state = self.set_up_test_model(self.app_label, index=True)
200 table_name = "%s_pony" % self.app_label
201 self.assertTableExists(table_name)
202 new_state = project_state.clone()
203 operation = RemoveIndexConcurrently("Pony", "pony_pink_idx")
204 self.assertEqual(
205 operation.describe(),
206 "Concurrently remove index pony_pink_idx from Pony",
207 )
208 self.assertEqual(
209 operation.formatted_description(),
210 "- Concurrently remove index pony_pink_idx from Pony",
211 )
212 operation.state_forwards(self.app_label, new_state)
213 self.assertEqual(
214 len(new_state.models[self.app_label, "pony"].options["indexes"]), 0
215 )
216 self.assertIndexExists(table_name, ["pink"])
217 # Remove index.
218 with connection.schema_editor(atomic=False) as editor:
219 operation.database_forwards(
220 self.app_label, editor, project_state, new_state
221 )
222 self.assertIndexNotExists(table_name, ["pink"])
223 # Reversal.
224 with connection.schema_editor(atomic=False) as editor:
225 operation.database_backwards(
226 self.app_label, editor, new_state, project_state
227 )
228 self.assertIndexExists(table_name, ["pink"])
229 # Deconstruction.
230 name, args, kwargs = operation.deconstruct()
231 self.assertEqual(name, "RemoveIndexConcurrently")
232 self.assertEqual(args, [])
233 self.assertEqual(kwargs, {"model_name": "Pony", "name": "pony_pink_idx"})
234
235
236 class NoMigrationRouter:
237 def allow_migrate(self, db, app_label, **hints):
238 return False
239
240
241 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")
242 class CreateExtensionTests(PostgreSQLTestCase):
243 app_label = "test_allow_create_extention"
244
245 @override_settings(DATABASE_ROUTERS=[NoMigrationRouter()])
246 def test_no_allow_migrate(self):
247 operation = CreateExtension("tablefunc")
248 self.assertEqual(
249 operation.formatted_description(), "+ Creates extension tablefunc"
250 )
251 project_state = ProjectState()
252 new_state = project_state.clone()
253 # Don't create an extension.
254 with CaptureQueriesContext(connection) as captured_queries:
255 with connection.schema_editor(atomic=False) as editor:
256 operation.database_forwards(
257 self.app_label, editor, project_state, new_state
258 )
259 self.assertEqual(len(captured_queries), 0)
260 # Reversal.
261 with CaptureQueriesContext(connection) as captured_queries:
262 with connection.schema_editor(atomic=False) as editor:
263 operation.database_backwards(
264 self.app_label, editor, new_state, project_state
265 )
266 self.assertEqual(len(captured_queries), 0)
267
268 def test_allow_migrate(self):
269 operation = CreateExtension("tablefunc")
270 self.assertEqual(
271 operation.migration_name_fragment, "create_extension_tablefunc"
272 )
273 project_state = ProjectState()
274 new_state = project_state.clone()
275 # Create an extension.
276 with CaptureQueriesContext(connection) as captured_queries:
277 with connection.schema_editor(atomic=False) as editor:
278 operation.database_forwards(
279 self.app_label, editor, project_state, new_state
280 )
281 self.assertEqual(len(captured_queries), 4)
282 self.assertIn("CREATE EXTENSION IF NOT EXISTS", captured_queries[1]["sql"])
283 # Reversal.
284 with CaptureQueriesContext(connection) as captured_queries:
285 with connection.schema_editor(atomic=False) as editor:
286 operation.database_backwards(
287 self.app_label, editor, new_state, project_state
288 )
289 self.assertEqual(len(captured_queries), 2)
290 self.assertIn("DROP EXTENSION IF EXISTS", captured_queries[1]["sql"])
291
292 def test_create_existing_extension(self):
293 operation = BloomExtension()
294 self.assertEqual(operation.migration_name_fragment, "create_extension_bloom")
295 project_state = ProjectState()
296 new_state = project_state.clone()
297 # Don't create an existing extension.
298 with CaptureQueriesContext(connection) as captured_queries:
299 with connection.schema_editor(atomic=False) as editor:
300 operation.database_forwards(
301 self.app_label, editor, project_state, new_state
302 )
303 self.assertEqual(len(captured_queries), 3)
304 self.assertIn("SELECT", captured_queries[0]["sql"])
305
306 def test_drop_nonexistent_extension(self):
307 operation = CreateExtension("tablefunc")
308 project_state = ProjectState()
309 new_state = project_state.clone()
310 # Don't drop a nonexistent extension.
311 with CaptureQueriesContext(connection) as captured_queries:
312 with connection.schema_editor(atomic=False) as editor:
313 operation.database_backwards(
314 self.app_label, editor, project_state, new_state
315 )
316 self.assertEqual(len(captured_queries), 1)
317 self.assertIn("SELECT", captured_queries[0]["sql"])
318
319
320 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")
321 class CreateCollationTests(OptimizerTestBase, PostgreSQLTestCase):
322 app_label = "test_allow_create_collation"
323
324 @override_settings(DATABASE_ROUTERS=[NoMigrationRouter()])
325 def test_no_allow_migrate(self):
326 operation = CreateCollation("C_test", locale="C")
327 project_state = ProjectState()
328 new_state = project_state.clone()
329 # Don't create a collation.
330 with CaptureQueriesContext(connection) as captured_queries:
331 with connection.schema_editor(atomic=False) as editor:
332 operation.database_forwards(
333 self.app_label, editor, project_state, new_state
334 )
335 self.assertEqual(len(captured_queries), 0)
336 # Reversal.
337 with CaptureQueriesContext(connection) as captured_queries:
338 with connection.schema_editor(atomic=False) as editor:
339 operation.database_backwards(
340 self.app_label, editor, new_state, project_state
341 )
342 self.assertEqual(len(captured_queries), 0)
343
344 def test_create(self):
345 operation = CreateCollation("C_test", locale="C")
346 self.assertEqual(operation.migration_name_fragment, "create_collation_c_test")
347 self.assertEqual(operation.describe(), "Create collation C_test")
348 self.assertEqual(operation.formatted_description(), "+ Create collation C_test")
349 project_state = ProjectState()
350 new_state = project_state.clone()
351 # Create a collation.
352 with CaptureQueriesContext(connection) as captured_queries:
353 with connection.schema_editor(atomic=False) as editor:
354 operation.database_forwards(
355 self.app_label, editor, project_state, new_state
356 )
357 self.assertEqual(len(captured_queries), 1)
358 self.assertIn("CREATE COLLATION", captured_queries[0]["sql"])
359 # Creating the same collation raises an exception.
360 with self.assertRaisesMessage(ProgrammingError, "already exists"):
361 with connection.schema_editor(atomic=True) as editor:
362 operation.database_forwards(
363 self.app_label, editor, project_state, new_state
364 )
365 # Reversal.
366 with CaptureQueriesContext(connection) as captured_queries:
367 with connection.schema_editor(atomic=False) as editor:
368 operation.database_backwards(
369 self.app_label, editor, new_state, project_state
370 )
371 self.assertEqual(len(captured_queries), 1)
372 self.assertIn("DROP COLLATION", captured_queries[0]["sql"])
373 # Deconstruction.
374 name, args, kwargs = operation.deconstruct()
375 self.assertEqual(name, "CreateCollation")
376 self.assertEqual(args, [])
377 self.assertEqual(kwargs, {"name": "C_test", "locale": "C"})
378
379 def test_create_non_deterministic_collation(self):
380 operation = CreateCollation(
381 "case_insensitive_test",
382 "und-u-ks-level2",
383 provider="icu",
384 deterministic=False,
385 )
386 project_state = ProjectState()
387 new_state = project_state.clone()
388 # Create a collation.
389 with CaptureQueriesContext(connection) as captured_queries:
390 with connection.schema_editor(atomic=False) as editor:
391 operation.database_forwards(
392 self.app_label, editor, project_state, new_state
393 )
394 self.assertEqual(len(captured_queries), 1)
395 self.assertIn("CREATE COLLATION", captured_queries[0]["sql"])
396 # Reversal.
397 with CaptureQueriesContext(connection) as captured_queries:
398 with connection.schema_editor(atomic=False) as editor:
399 operation.database_backwards(
400 self.app_label, editor, new_state, project_state
401 )
402 self.assertEqual(len(captured_queries), 1)
403 self.assertIn("DROP COLLATION", captured_queries[0]["sql"])
404 # Deconstruction.
405 name, args, kwargs = operation.deconstruct()
406 self.assertEqual(name, "CreateCollation")
407 self.assertEqual(args, [])
408 self.assertEqual(
409 kwargs,
410 {
411 "name": "case_insensitive_test",
412 "locale": "und-u-ks-level2",
413 "provider": "icu",
414 "deterministic": False,
415 },
416 )
417
418 def test_create_collation_alternate_provider(self):
419 operation = CreateCollation(
420 "german_phonebook_test",
421 provider="icu",
422 locale="de-u-co-phonebk",
423 )
424 project_state = ProjectState()
425 new_state = project_state.clone()
426 # Create an collation.
427 with CaptureQueriesContext(connection) as captured_queries:
428 with connection.schema_editor(atomic=False) as editor:
429 operation.database_forwards(
430 self.app_label, editor, project_state, new_state
431 )
432 self.assertEqual(len(captured_queries), 1)
433 self.assertIn("CREATE COLLATION", captured_queries[0]["sql"])
434 # Reversal.
435 with CaptureQueriesContext(connection) as captured_queries:
436 with connection.schema_editor(atomic=False) as editor:
437 operation.database_backwards(
438 self.app_label, editor, new_state, project_state
439 )
440 self.assertEqual(len(captured_queries), 1)
441 self.assertIn("DROP COLLATION", captured_queries[0]["sql"])
442
443 def test_writer(self):
444 operation = CreateCollation(
445 "sample_collation",
446 "und-u-ks-level2",
447 provider="icu",
448 deterministic=False,
449 )
450 buff, imports = OperationWriter(operation, indentation=0).serialize()
451 self.assertEqual(imports, {"import django.contrib.postgres.operations"})
452 self.assertEqual(
453 buff,
454 "django.contrib.postgres.operations.CreateCollation(\n"
455 " name='sample_collation',\n"
456 " locale='und-u-ks-level2',\n"
457 " provider='icu',\n"
458 " deterministic=False,\n"
459 "),",
460 )
461
462 def test_reduce_create_remove(self):
463 self.assertOptimizesTo(
464 [
465 CreateCollation(
466 "sample_collation",
467 "und-u-ks-level2",
468 provider="icu",
469 deterministic=False,
470 ),
471 RemoveCollation(
472 "sample_collation",
473 # Different locale
474 "de-u-ks-level1",
475 ),
476 ],
477 [],
478 )
479
480
481 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")
482 class RemoveCollationTests(PostgreSQLTestCase):
483 app_label = "test_allow_remove_collation"
484
485 @override_settings(DATABASE_ROUTERS=[NoMigrationRouter()])
486 def test_no_allow_migrate(self):
487 operation = RemoveCollation("C_test", locale="C")
488 project_state = ProjectState()
489 new_state = project_state.clone()
490 # Don't create a collation.
491 with CaptureQueriesContext(connection) as captured_queries:
492 with connection.schema_editor(atomic=False) as editor:
493 operation.database_forwards(
494 self.app_label, editor, project_state, new_state
495 )
496 self.assertEqual(len(captured_queries), 0)
497 # Reversal.
498 with CaptureQueriesContext(connection) as captured_queries:
499 with connection.schema_editor(atomic=False) as editor:
500 operation.database_backwards(
501 self.app_label, editor, new_state, project_state
502 )
503 self.assertEqual(len(captured_queries), 0)
504
505 def test_remove(self):
506 operation = CreateCollation("C_test", locale="C")
507 project_state = ProjectState()
508 new_state = project_state.clone()
509 with connection.schema_editor(atomic=False) as editor:
510 operation.database_forwards(
511 self.app_label, editor, project_state, new_state
512 )
513
514 operation = RemoveCollation("C_test", locale="C")
515 self.assertEqual(operation.migration_name_fragment, "remove_collation_c_test")
516 self.assertEqual(operation.describe(), "Remove collation C_test")
517 self.assertEqual(operation.formatted_description(), "- Remove collation C_test")
518 project_state = ProjectState()
519 new_state = project_state.clone()
520 # Remove a collation.
521 with CaptureQueriesContext(connection) as captured_queries:
522 with connection.schema_editor(atomic=False) as editor:
523 operation.database_forwards(
524 self.app_label, editor, project_state, new_state
525 )
526 self.assertEqual(len(captured_queries), 1)
527 self.assertIn("DROP COLLATION", captured_queries[0]["sql"])
528 # Removing a nonexistent collation raises an exception.
529 with self.assertRaisesMessage(ProgrammingError, "does not exist"):
530 with connection.schema_editor(atomic=True) as editor:
531 operation.database_forwards(
532 self.app_label, editor, project_state, new_state
533 )
534 # Reversal.
535 with CaptureQueriesContext(connection) as captured_queries:
536 with connection.schema_editor(atomic=False) as editor:
537 operation.database_backwards(
538 self.app_label, editor, new_state, project_state
539 )
540 self.assertEqual(len(captured_queries), 1)
541 self.assertIn("CREATE COLLATION", captured_queries[0]["sql"])
542 # Deconstruction.
543 name, args, kwargs = operation.deconstruct()
544 self.assertEqual(name, "RemoveCollation")
545 self.assertEqual(args, [])
546 self.assertEqual(kwargs, {"name": "C_test", "locale": "C"})
547
548
549 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")
550 @modify_settings(INSTALLED_APPS={"append": "migrations"})
551 class AddConstraintNotValidTests(OperationTestBase):
552 app_label = "test_add_constraint_not_valid"
553
554 def test_non_check_constraint_not_supported(self):
555 constraint = UniqueConstraint(fields=["pink"], name="pony_pink_uniq")
556 msg = "AddConstraintNotValid.constraint must be a check constraint."
557 with self.assertRaisesMessage(TypeError, msg):
558 AddConstraintNotValid(model_name="pony", constraint=constraint)
559
560 def test_add(self):
561 table_name = f"{self.app_label}_pony"
562 constraint_name = "pony_pink_gte_check"
563 constraint = CheckConstraint(condition=Q(pink__gte=4), name=constraint_name)
564 operation = AddConstraintNotValid("Pony", constraint=constraint)
565 project_state, new_state = self.make_test_state(self.app_label, operation)
566 self.assertEqual(
567 operation.describe(),
568 f"Create not valid constraint {constraint_name} on model Pony",
569 )
570 self.assertEqual(
571 operation.formatted_description(),
572 f"+ Create not valid constraint {constraint_name} on model Pony",
573 )
574 self.assertEqual(
575 operation.migration_name_fragment,
576 f"pony_{constraint_name}_not_valid",
577 )
578 self.assertEqual(
579 len(new_state.models[self.app_label, "pony"].options["constraints"]),
580 1,
581 )
582 self.assertConstraintNotExists(table_name, constraint_name)
583 Pony = new_state.apps.get_model(self.app_label, "Pony")
584 self.assertEqual(len(Pony._meta.constraints), 1)
585 Pony.objects.create(pink=2, weight=1.0)
586 # Add constraint.
587 with connection.schema_editor(atomic=True) as editor:
588 operation.database_forwards(
589 self.app_label, editor, project_state, new_state
590 )
591 msg = f'check constraint "{constraint_name}"'
592 with self.assertRaisesMessage(IntegrityError, msg), transaction.atomic():
593 Pony.objects.create(pink=3, weight=1.0)
594 self.assertConstraintExists(table_name, constraint_name)
595 # Reversal.
596 with connection.schema_editor(atomic=True) as editor:
597 operation.database_backwards(
598 self.app_label, editor, project_state, new_state
599 )
600 self.assertConstraintNotExists(table_name, constraint_name)
601 Pony.objects.create(pink=3, weight=1.0)
602 # Deconstruction.
603 name, args, kwargs = operation.deconstruct()
604 self.assertEqual(name, "AddConstraintNotValid")
605 self.assertEqual(args, [])
606 self.assertEqual(kwargs, {"model_name": "Pony", "constraint": constraint})
607
608
609 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")
610 @modify_settings(INSTALLED_APPS={"append": "migrations"})
611 class ValidateConstraintTests(OperationTestBase):
612 app_label = "test_validate_constraint"
613
614 def test_validate(self):
615 constraint_name = "pony_pink_gte_check"
616 constraint = CheckConstraint(condition=Q(pink__gte=4), name=constraint_name)
617 operation = AddConstraintNotValid("Pony", constraint=constraint)
618 project_state, new_state = self.make_test_state(self.app_label, operation)
619 Pony = new_state.apps.get_model(self.app_label, "Pony")
620 obj = Pony.objects.create(pink=2, weight=1.0)
621 # Add constraint.
622 with connection.schema_editor(atomic=True) as editor:
623 operation.database_forwards(
624 self.app_label, editor, project_state, new_state
625 )
626 project_state = new_state
627 new_state = new_state.clone()
628 operation = ValidateConstraint("Pony", name=constraint_name)
629 operation.state_forwards(self.app_label, new_state)
630 self.assertEqual(
631 operation.describe(),
632 f"Validate constraint {constraint_name} on model Pony",
633 )
634 self.assertEqual(
635 operation.formatted_description(),
636 f"~ Validate constraint {constraint_name} on model Pony",
637 )
638 self.assertEqual(
639 operation.migration_name_fragment,
640 f"pony_validate_{constraint_name}",
641 )
642 # Validate constraint.
643 with connection.schema_editor(atomic=True) as editor:
644 msg = f'check constraint "{constraint_name}"'
645 with self.assertRaisesMessage(IntegrityError, msg):
646 operation.database_forwards(
647 self.app_label, editor, project_state, new_state
648 )
649 obj.pink = 5
650 obj.save()
651 with connection.schema_editor(atomic=True) as editor:
652 operation.database_forwards(
653 self.app_label, editor, project_state, new_state
654 )
655 # Reversal is a noop.
656 with connection.schema_editor() as editor:
657 with self.assertNumQueries(0):
658 operation.database_backwards(
659 self.app_label, editor, new_state, project_state
660 )
661 # Deconstruction.
662 name, args, kwargs = operation.deconstruct()
663 self.assertEqual(name, "ValidateConstraint")
664 self.assertEqual(args, [])
665 self.assertEqual(kwargs, {"model_name": "Pony", "name": constraint_name})