2017-09-15 22:00:12 +08:00
|
|
|
from django.contrib.postgres.signals import (
|
|
|
|
get_citext_oids, get_hstore_oids, register_type_handlers,
|
|
|
|
)
|
2019-07-25 19:44:18 +08:00
|
|
|
from django.db.migrations import AddIndex, RemoveIndex
|
2014-03-15 01:34:49 +08:00
|
|
|
from django.db.migrations.operations.base import Operation
|
2019-07-25 19:44:18 +08:00
|
|
|
from django.db.utils import NotSupportedError
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CreateExtension(Operation):
|
|
|
|
reversible = True
|
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self, name):
|
2014-03-15 01:34:49 +08:00
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2015-04-05 00:10:26 +08:00
|
|
|
if schema_editor.connection.vendor != 'postgresql':
|
|
|
|
return
|
2016-04-26 07:30:48 +08:00
|
|
|
schema_editor.execute("CREATE EXTENSION IF NOT EXISTS %s" % schema_editor.quote_name(self.name))
|
2017-09-15 22:00:12 +08:00
|
|
|
# Clear cached, stale oids.
|
|
|
|
get_hstore_oids.cache_clear()
|
|
|
|
get_citext_oids.cache_clear()
|
2017-05-03 13:25:30 +08:00
|
|
|
# Registering new type handlers cannot be done before the extension is
|
|
|
|
# installed, otherwise a subsequent data migration would use the same
|
|
|
|
# connection.
|
|
|
|
register_type_handlers(schema_editor.connection)
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
2016-04-26 07:30:48 +08:00
|
|
|
schema_editor.execute("DROP EXTENSION %s" % schema_editor.quote_name(self.name))
|
2017-09-15 22:00:12 +08:00
|
|
|
# Clear cached, stale oids.
|
|
|
|
get_hstore_oids.cache_clear()
|
|
|
|
get_citext_oids.cache_clear()
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Creates extension %s" % self.name
|
|
|
|
|
|
|
|
|
2019-10-18 17:08:50 +08:00
|
|
|
class BloomExtension(CreateExtension):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.name = 'bloom'
|
|
|
|
|
|
|
|
|
2016-09-22 00:08:43 +08:00
|
|
|
class BtreeGinExtension(CreateExtension):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.name = 'btree_gin'
|
|
|
|
|
|
|
|
|
2017-07-01 21:30:34 +08:00
|
|
|
class BtreeGistExtension(CreateExtension):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.name = 'btree_gist'
|
|
|
|
|
|
|
|
|
2016-06-02 05:43:59 +08:00
|
|
|
class CITextExtension(CreateExtension):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.name = 'citext'
|
|
|
|
|
|
|
|
|
2017-03-30 05:52:42 +08:00
|
|
|
class CryptoExtension(CreateExtension):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.name = 'pgcrypto'
|
|
|
|
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
class HStoreExtension(CreateExtension):
|
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self):
|
2014-03-15 01:34:49 +08:00
|
|
|
self.name = 'hstore'
|
|
|
|
|
2014-09-06 04:53:11 +08:00
|
|
|
|
2015-06-06 00:37:48 +08:00
|
|
|
class TrigramExtension(CreateExtension):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.name = 'pg_trgm'
|
2016-08-08 11:15:08 +08:00
|
|
|
|
|
|
|
|
2016-09-22 00:08:43 +08:00
|
|
|
class UnaccentExtension(CreateExtension):
|
2016-08-08 11:15:08 +08:00
|
|
|
|
|
|
|
def __init__(self):
|
2016-09-22 00:08:43 +08:00
|
|
|
self.name = 'unaccent'
|
2019-07-25 19:44:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
class NotInTransactionMixin:
|
|
|
|
def _ensure_not_in_transaction(self, schema_editor):
|
|
|
|
if schema_editor.connection.in_atomic_block:
|
|
|
|
raise NotSupportedError(
|
|
|
|
'The %s operation cannot be executed inside a transaction '
|
|
|
|
'(set atomic = False on the migration).'
|
|
|
|
% self.__class__.__name__
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class AddIndexConcurrently(NotInTransactionMixin, AddIndex):
|
|
|
|
"""Create an index using PostgreSQL's CREATE INDEX CONCURRENTLY syntax."""
|
|
|
|
atomic = False
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return 'Concurrently create index %s on field(s) %s of model %s' % (
|
|
|
|
self.index.name,
|
|
|
|
', '.join(self.index.fields),
|
|
|
|
self.model_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
self._ensure_not_in_transaction(schema_editor)
|
|
|
|
model = to_state.apps.get_model(app_label, self.model_name)
|
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
|
|
|
schema_editor.add_index(model, self.index, concurrently=True)
|
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
self._ensure_not_in_transaction(schema_editor)
|
|
|
|
model = from_state.apps.get_model(app_label, self.model_name)
|
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
|
|
|
schema_editor.remove_index(model, self.index, concurrently=True)
|
|
|
|
|
|
|
|
|
|
|
|
class RemoveIndexConcurrently(NotInTransactionMixin, RemoveIndex):
|
|
|
|
"""Remove an index using PostgreSQL's DROP INDEX CONCURRENTLY syntax."""
|
|
|
|
atomic = False
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return 'Concurrently remove index %s from %s' % (self.name, self.model_name)
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
self._ensure_not_in_transaction(schema_editor)
|
|
|
|
model = from_state.apps.get_model(app_label, self.model_name)
|
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
|
|
|
from_model_state = from_state.models[app_label, self.model_name_lower]
|
|
|
|
index = from_model_state.get_index_by_name(self.name)
|
|
|
|
schema_editor.remove_index(model, index, concurrently=True)
|
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
self._ensure_not_in_transaction(schema_editor)
|
|
|
|
model = to_state.apps.get_model(app_label, self.model_name)
|
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
|
|
|
to_model_state = to_state.models[app_label, self.model_name_lower]
|
|
|
|
index = to_model_state.get_index_by_name(self.name)
|
|
|
|
schema_editor.add_index(model, index, concurrently=True)
|