2
0

operations.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. Creating the extension requires a database user with superuser privileges.
  25. If the Django database user doesn't have superuser privileges, you'll have
  26. to create the extension outside of Django migrations with a user that has
  27. the appropriate privileges. In that case, connect to your Django database and
  28. run the query ``CREATE EXTENSION IF NOT EXISTS hstore;``.
  29. .. currentmodule:: django.contrib.postgres.operations
  30. ``CreateExtension``
  31. ===================
  32. .. class:: CreateExtension(name)
  33. An ``Operation`` subclass which installs PostgreSQL extensions.
  34. .. attribute:: name
  35. This is a required argument. The name of the extension to be installed.
  36. ``BloomExtension``
  37. ==================
  38. .. class:: BloomExtension()
  39. .. versionadded:: 3.1
  40. Install the ``bloom`` extension.
  41. ``BtreeGinExtension``
  42. =====================
  43. .. class:: BtreeGinExtension()
  44. Install the ``btree_gin`` extension.
  45. ``BtreeGistExtension``
  46. ======================
  47. .. class:: BtreeGistExtension()
  48. Install the ``btree_gist`` extension.
  49. ``CITextExtension``
  50. ===================
  51. .. class:: CITextExtension()
  52. Installs the ``citext`` extension.
  53. ``CryptoExtension``
  54. ===================
  55. .. class:: CryptoExtension()
  56. Installs the ``pgcrypto`` extension.
  57. ``HStoreExtension``
  58. ===================
  59. .. class:: HStoreExtension()
  60. Installs the ``hstore`` extension and also sets up the connection to
  61. interpret hstore data for possible use in subsequent migrations.
  62. ``TrigramExtension``
  63. ====================
  64. .. class:: TrigramExtension()
  65. Installs the ``pg_trgm`` extension.
  66. ``UnaccentExtension``
  67. =====================
  68. .. class:: UnaccentExtension()
  69. Installs the ``unaccent`` extension.
  70. Index concurrent operations
  71. ===========================
  72. PostgreSQL supports the ``CONCURRENTLY`` option to ``CREATE INDEX`` and
  73. ``DROP INDEX`` statements to add and remove indexes without locking out writes.
  74. This option is useful for adding or removing an index in a live production
  75. database.
  76. .. class:: AddIndexConcurrently(model_name, index)
  77. Like :class:`~django.db.migrations.operations.AddIndex`, but creates an
  78. index with the ``CONCURRENTLY`` option. This has a few caveats to be aware
  79. of when using this option, see `the PostgreSQL documentation of building
  80. indexes concurrently <https://www.postgresql.org/docs/current/
  81. sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY>`_.
  82. .. class:: RemoveIndexConcurrently(model_name, name)
  83. Like :class:`~django.db.migrations.operations.RemoveIndex`, but removes the
  84. index with the ``CONCURRENTLY`` option. This has a few caveats to be aware
  85. of when using this option, see `the PostgreSQL documentation
  86. <https://www.postgresql.org/docs/current/sql-dropindex.html>`_.
  87. .. note::
  88. The ``CONCURRENTLY`` option is not supported inside a transaction (see
  89. :ref:`non-atomic migration <non-atomic-migrations>`).