From 8c3bd0b708b488a1f6e8bd8cc6b96569904605be Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Sat, 17 Oct 2020 00:47:13 +0530 Subject: Fixed #31653 -- Added AddConstraintNotValid()/ValidateConstraint() operations for PostgreSQL. --- docs/ref/contrib/postgres/operations.txt | 36 ++++++++++++++++++++++++++++++++ docs/releases/4.0.txt | 9 ++++++++ 2 files changed, 45 insertions(+) (limited to 'docs') diff --git a/docs/ref/contrib/postgres/operations.txt b/docs/ref/contrib/postgres/operations.txt index dc9faebcdb..4a0ef7a6b8 100644 --- a/docs/ref/contrib/postgres/operations.txt +++ b/docs/ref/contrib/postgres/operations.txt @@ -188,3 +188,39 @@ database. The ``CONCURRENTLY`` option is not supported inside a transaction (see :ref:`non-atomic migration `). + +Adding constraints without enforcing validation +=============================================== + +.. versionadded:: 4.0 + +PostgreSQL supports the ``NOT VALID`` option with the ``ADD CONSTRAINT`` +statement to add check constraints without enforcing validation on existing +rows. This option is useful if you want to skip the potentially lengthy scan of +the table to verify that all existing rows satisfy the constraint. + +To validate check constraints created with the ``NOT VALID`` option at a later +point of time, use the +:class:`~django.contrib.postgres.operations.ValidateConstraint` operation. + +See `the PostgreSQL documentation `__ for more details. + +.. class:: AddConstraintNotValid(model_name, constraint) + + Like :class:`~django.db.migrations.operations.AddConstraint`, but avoids + validating the constraint on existing rows. + +.. class:: ValidateConstraint(model_name, name) + + Scans through the table and validates the given check constraint on + existing rows. + +.. note:: + + ``AddConstraintNotValid`` and ``ValidateConstraint`` operations should be + performed in two separate migrations. Performing both operations in the + same atomic migration has the same effect as + :class:`~django.db.migrations.operations.AddConstraint`, whereas performing + them in a single non-atomic migration, may leave your database in an + inconsistent state if the ``ValidateConstraint`` operation fails. diff --git a/docs/releases/4.0.txt b/docs/releases/4.0.txt index 60c449bc88..2bdb76ef3d 100644 --- a/docs/releases/4.0.txt +++ b/docs/releases/4.0.txt @@ -122,6 +122,15 @@ Minor features * The PostgreSQL backend now supports connecting by a service name. See :ref:`postgresql-connection-settings` for more details. +* The new :class:`~django.contrib.postgres.operations.AddConstraintNotValid` + operation allows creating check constraints on PostgreSQL without verifying + that all existing rows satisfy the new constraint. + +* The new :class:`~django.contrib.postgres.operations.ValidateConstraint` + operation allows validating check constraints which were created using + :class:`~django.contrib.postgres.operations.AddConstraintNotValid` on + PostgreSQL. + :mod:`django.contrib.redirects` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- cgit v1.3