2014-03-15 01:34:49 +08:00
|
|
|
from django.contrib.postgres.signals import register_hstore_handler
|
|
|
|
from django.db.migrations.operations.base import Operation
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
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))
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Creates extension %s" % self.name
|
|
|
|
|
|
|
|
|
2016-09-22 00:08:43 +08:00
|
|
|
class BtreeGinExtension(CreateExtension):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.name = 'btree_gin'
|
|
|
|
|
|
|
|
|
2016-06-02 05:43:59 +08:00
|
|
|
class CITextExtension(CreateExtension):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.name = 'citext'
|
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
super(HStoreExtension, self).database_forwards(app_label, schema_editor, from_state, to_state)
|
|
|
|
# Register hstore straight away as it cannot be done before the
|
|
|
|
# extension is installed, a subsequent data migration would use the
|
|
|
|
# same connection
|
|
|
|
register_hstore_handler(schema_editor.connection)
|
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'
|