You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

operations.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from django.contrib.postgres.signals import (
  2. get_citext_oids, get_hstore_oids, register_type_handlers,
  3. )
  4. from django.db.migrations.operations.base import Operation
  5. class CreateExtension(Operation):
  6. reversible = True
  7. def __init__(self, name):
  8. self.name = name
  9. def state_forwards(self, app_label, state):
  10. pass
  11. def database_forwards(self, app_label, schema_editor, from_state, to_state):
  12. if schema_editor.connection.vendor != 'postgresql':
  13. return
  14. schema_editor.execute("CREATE EXTENSION IF NOT EXISTS %s" % schema_editor.quote_name(self.name))
  15. # Clear cached, stale oids.
  16. get_hstore_oids.cache_clear()
  17. get_citext_oids.cache_clear()
  18. # Registering new type handlers cannot be done before the extension is
  19. # installed, otherwise a subsequent data migration would use the same
  20. # connection.
  21. register_type_handlers(schema_editor.connection)
  22. def database_backwards(self, app_label, schema_editor, from_state, to_state):
  23. schema_editor.execute("DROP EXTENSION %s" % schema_editor.quote_name(self.name))
  24. # Clear cached, stale oids.
  25. get_hstore_oids.cache_clear()
  26. get_citext_oids.cache_clear()
  27. def describe(self):
  28. return "Creates extension %s" % self.name
  29. class BtreeGinExtension(CreateExtension):
  30. def __init__(self):
  31. self.name = 'btree_gin'
  32. class BtreeGistExtension(CreateExtension):
  33. def __init__(self):
  34. self.name = 'btree_gist'
  35. class CITextExtension(CreateExtension):
  36. def __init__(self):
  37. self.name = 'citext'
  38. class CryptoExtension(CreateExtension):
  39. def __init__(self):
  40. self.name = 'pgcrypto'
  41. class HStoreExtension(CreateExtension):
  42. def __init__(self):
  43. self.name = 'hstore'
  44. class TrigramExtension(CreateExtension):
  45. def __init__(self):
  46. self.name = 'pg_trgm'
  47. class UnaccentExtension(CreateExtension):
  48. def __init__(self):
  49. self.name = 'unaccent'