operations.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. =============================
  2. Database migration operations
  3. =============================
  4. All of these :doc:`operations </ref/migration-operations>` are available from
  5. the ``django.contrib.postgres.operations`` module.
  6. .. _create-postgresql-extensions:
  7. Creating extension using migrations
  8. ===================================
  9. You can create a PostgreSQL extension in your database using a migration file.
  10. This example creates an hstore extension, but the same principles apply for
  11. other extensions.
  12. Set up the hstore extension in PostgreSQL before the first ``CreateModel``
  13. or ``AddField`` operation that involves
  14. :class:`~django.contrib.postgres.fields.HStoreField` by adding a migration with
  15. the :class:`~django.contrib.postgres.operations.HStoreExtension` operation.
  16. For example::
  17. from django.contrib.postgres.operations import HStoreExtension
  18. class Migration(migrations.Migration):
  19. ...
  20. operations = [
  21. HStoreExtension(),
  22. ...
  23. ]
  24. The operation skips adding the extension if it already exists.
  25. For most extensions, this requires a database user with superuser privileges.
  26. If the Django database user doesn't have the appropriate privileges, you'll
  27. have to create the extension outside of Django migrations with a user that has
  28. them. In that case, connect to your Django database and run the query
  29. ``CREATE EXTENSION IF NOT EXISTS hstore;``.
  30. .. currentmodule:: django.contrib.postgres.operations
  31. ``CreateExtension``
  32. ===================
  33. .. class:: CreateExtension(name)
  34. An ``Operation`` subclass which installs a PostgreSQL extension. For common
  35. extensions, use one of the more specific subclasses below.
  36. .. attribute:: name
  37. This is a required argument. The name of the extension to be installed.
  38. ``BloomExtension``
  39. ==================
  40. .. class:: BloomExtension()
  41. Installs the ``bloom`` extension.
  42. ``BtreeGinExtension``
  43. =====================
  44. .. class:: BtreeGinExtension()
  45. Installs the ``btree_gin`` extension.
  46. ``BtreeGistExtension``
  47. ======================
  48. .. class:: BtreeGistExtension()
  49. Installs the ``btree_gist`` extension.
  50. ``CITextExtension``
  51. ===================
  52. .. class:: CITextExtension()
  53. Installs the ``citext`` extension.
  54. ``CryptoExtension``
  55. ===================
  56. .. class:: CryptoExtension()
  57. Installs the ``pgcrypto`` extension.
  58. ``HStoreExtension``
  59. ===================
  60. .. class:: HStoreExtension()
  61. Installs the ``hstore`` extension and also sets up the connection to
  62. interpret hstore data for possible use in subsequent migrations.
  63. ``TrigramExtension``
  64. ====================
  65. .. class:: TrigramExtension()
  66. Installs the ``pg_trgm`` extension.
  67. ``UnaccentExtension``
  68. =====================
  69. .. class:: UnaccentExtension()
  70. Installs the ``unaccent`` extension.
  71. .. _manage-postgresql-collations:
  72. Managing collations using migrations
  73. ====================================
  74. If you need to filter or order a column using a particular collation that your
  75. operating system provides but PostgreSQL does not, you can manage collations in
  76. your database using a migration file. These collations can then be used with
  77. the ``db_collation`` parameter on :class:`~django.db.models.CharField`,
  78. :class:`~django.db.models.TextField`, and their subclasses.
  79. For example, to create a collation for German phone book ordering::
  80. from django.contrib.postgres.operations import CreateCollation
  81. class Migration(migrations.Migration):
  82. ...
  83. operations = [
  84. CreateCollation(
  85. 'german_phonebook',
  86. provider='icu',
  87. locale='und-u-ks-level2',
  88. ),
  89. ...
  90. ]
  91. .. class:: CreateCollation(name, locale, *, provider='libc', deterministic=True)
  92. Creates a collation with the given ``name``, ``locale`` and ``provider``.
  93. Set the ``deterministic`` parameter to ``False`` to create a
  94. non-deterministic collation, such as for case-insensitive filtering.
  95. .. class:: RemoveCollation(name, locale, *, provider='libc', deterministic=True)
  96. Removes the collations named ``name``.
  97. When reversed this is creating a collation with the provided ``locale``,
  98. ``provider``, and ``deterministic`` arguments. Therefore, ``locale`` is
  99. required to make this operation reversible.
  100. .. admonition:: Restrictions
  101. Non-deterministic collations are supported only on PostgreSQL 12+.
  102. Concurrent index operations
  103. ===========================
  104. PostgreSQL supports the ``CONCURRENTLY`` option to ``CREATE INDEX`` and
  105. ``DROP INDEX`` statements to add and remove indexes without locking out writes.
  106. This option is useful for adding or removing an index in a live production
  107. database.
  108. .. class:: AddIndexConcurrently(model_name, index)
  109. Like :class:`~django.db.migrations.operations.AddIndex`, but creates an
  110. index with the ``CONCURRENTLY`` option. This has a few caveats to be aware
  111. of when using this option, see `the PostgreSQL documentation of building
  112. indexes concurrently <https://www.postgresql.org/docs/current/
  113. sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY>`_.
  114. .. class:: RemoveIndexConcurrently(model_name, name)
  115. Like :class:`~django.db.migrations.operations.RemoveIndex`, but removes the
  116. index with the ``CONCURRENTLY`` option. This has a few caveats to be aware
  117. of when using this option, see `the PostgreSQL documentation
  118. <https://www.postgresql.org/docs/current/sql-dropindex.html>`_.
  119. .. note::
  120. The ``CONCURRENTLY`` option is not supported inside a transaction (see
  121. :ref:`non-atomic migration <non-atomic-migrations>`).
  122. Adding constraints without enforcing validation
  123. ===============================================
  124. .. versionadded:: 4.0
  125. PostgreSQL supports the ``NOT VALID`` option with the ``ADD CONSTRAINT``
  126. statement to add check constraints without enforcing validation on existing
  127. rows. This option is useful if you want to skip the potentially lengthy scan of
  128. the table to verify that all existing rows satisfy the constraint.
  129. To validate check constraints created with the ``NOT VALID`` option at a later
  130. point of time, use the
  131. :class:`~django.contrib.postgres.operations.ValidateConstraint` operation.
  132. See `the PostgreSQL documentation <https://www.postgresql.org/docs/current/
  133. sql-altertable.html#SQL-ALTERTABLE-NOTES>`__ for more details.
  134. .. class:: AddConstraintNotValid(model_name, constraint)
  135. Like :class:`~django.db.migrations.operations.AddConstraint`, but avoids
  136. validating the constraint on existing rows.
  137. .. class:: ValidateConstraint(model_name, name)
  138. Scans through the table and validates the given check constraint on
  139. existing rows.
  140. .. note::
  141. ``AddConstraintNotValid`` and ``ValidateConstraint`` operations should be
  142. performed in two separate migrations. Performing both operations in the
  143. same atomic migration has the same effect as
  144. :class:`~django.db.migrations.operations.AddConstraint`, whereas performing
  145. them in a single non-atomic migration, may leave your database in an
  146. inconsistent state if the ``ValidateConstraint`` operation fails.