2
0

operations.txt 6.8 KB

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