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