operations.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. .. versionchanged:: 3.2
  31. In older versions, the pre-existence of the extension isn't checked.
  32. .. currentmodule:: django.contrib.postgres.operations
  33. ``CreateExtension``
  34. ===================
  35. .. class:: CreateExtension(name)
  36. An ``Operation`` subclass which installs a PostgreSQL extension. For common
  37. extensions, use one of the more specific subclasses below.
  38. .. attribute:: name
  39. This is a required argument. The name of the extension to be installed.
  40. ``BloomExtension``
  41. ==================
  42. .. class:: BloomExtension()
  43. .. versionadded:: 3.1
  44. Installs the ``bloom`` extension.
  45. ``BtreeGinExtension``
  46. =====================
  47. .. class:: BtreeGinExtension()
  48. Installs the ``btree_gin`` extension.
  49. ``BtreeGistExtension``
  50. ======================
  51. .. class:: BtreeGistExtension()
  52. Installs the ``btree_gist`` extension.
  53. ``CITextExtension``
  54. ===================
  55. .. class:: CITextExtension()
  56. Installs the ``citext`` extension.
  57. ``CryptoExtension``
  58. ===================
  59. .. class:: CryptoExtension()
  60. Installs the ``pgcrypto`` extension.
  61. ``HStoreExtension``
  62. ===================
  63. .. class:: HStoreExtension()
  64. Installs the ``hstore`` extension and also sets up the connection to
  65. interpret hstore data for possible use in subsequent migrations.
  66. ``TrigramExtension``
  67. ====================
  68. .. class:: TrigramExtension()
  69. Installs the ``pg_trgm`` extension.
  70. ``UnaccentExtension``
  71. =====================
  72. .. class:: UnaccentExtension()
  73. Installs the ``unaccent`` extension.
  74. .. _manage-postgresql-collations:
  75. Managing collations using migrations
  76. ====================================
  77. .. versionadded:: 3.2
  78. If you need to filter or order a column using a particular collation that your
  79. operating system provides but PostgreSQL does not, you can manage collations in
  80. your database using a migration file. These collations can then be used with
  81. the ``db_collation`` parameter on :class:`~django.db.models.CharField`,
  82. :class:`~django.db.models.TextField`, and their subclasses.
  83. For example, to create a collation for German phone book ordering::
  84. from django.contrib.postgres.operations import CreateCollation
  85. class Migration(migrations.Migration):
  86. ...
  87. operations = [
  88. CreateCollation(
  89. 'german_phonebook',
  90. provider='icu',
  91. locale='und-u-ks-level2',
  92. ),
  93. ...
  94. ]
  95. .. class:: CreateCollation(name, locale, *, provider='libc', deterministic=True)
  96. Creates a collation with the given ``name``, ``locale`` and ``provider``.
  97. Set the ``deterministic`` parameter to ``False`` to create a
  98. non-deterministic collation, such as for case-insensitive filtering.
  99. .. class:: RemoveCollation(name, locale, *, provider='libc', deterministic=True)
  100. Removes the collations named ``name``.
  101. When reversed this is creating a collation with the provided ``locale``,
  102. ``provider``, and ``deterministic`` arguments. Therefore, ``locale`` is
  103. required to make this operation reversible.
  104. .. admonition:: Restrictions
  105. PostgreSQL 9.6 only supports the ``'libc'`` provider.
  106. Non-deterministic collations are supported only on PostgreSQL 12+.
  107. Concurrent index operations
  108. ===========================
  109. PostgreSQL supports the ``CONCURRENTLY`` option to ``CREATE INDEX`` and
  110. ``DROP INDEX`` statements to add and remove indexes without locking out writes.
  111. This option is useful for adding or removing an index in a live production
  112. database.
  113. .. class:: AddIndexConcurrently(model_name, index)
  114. Like :class:`~django.db.migrations.operations.AddIndex`, but creates an
  115. index with the ``CONCURRENTLY`` option. This has a few caveats to be aware
  116. of when using this option, see `the PostgreSQL documentation of building
  117. indexes concurrently <https://www.postgresql.org/docs/current/
  118. sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY>`_.
  119. .. class:: RemoveIndexConcurrently(model_name, name)
  120. Like :class:`~django.db.migrations.operations.RemoveIndex`, but removes the
  121. index with the ``CONCURRENTLY`` option. This has a few caveats to be aware
  122. of when using this option, see `the PostgreSQL documentation
  123. <https://www.postgresql.org/docs/current/sql-dropindex.html>`_.
  124. .. note::
  125. The ``CONCURRENTLY`` option is not supported inside a transaction (see
  126. :ref:`non-atomic migration <non-atomic-migrations>`).