2017-01-16 03:09:38 +08:00
|
|
|
from django.db.models import Index
|
2016-08-08 19:50:25 +08:00
|
|
|
|
2016-10-13 20:39:44 +08:00
|
|
|
__all__ = ['BrinIndex', 'GinIndex']
|
|
|
|
|
|
|
|
|
|
|
|
class BrinIndex(Index):
|
|
|
|
suffix = 'brin'
|
2017-03-20 23:21:01 +08:00
|
|
|
# Allow an index name longer than 30 characters since the suffix is 4
|
|
|
|
# characters (usual limit is 3). Since this index can only be used on
|
|
|
|
# PostgreSQL, the 30 character limit for cross-database compatibility isn't
|
|
|
|
# applicable.
|
|
|
|
max_name_length = 31
|
2016-10-13 20:39:44 +08:00
|
|
|
|
|
|
|
def __init__(self, fields=[], name=None, pages_per_range=None):
|
2017-01-17 00:28:30 +08:00
|
|
|
if pages_per_range is not None and pages_per_range <= 0:
|
|
|
|
raise ValueError('pages_per_range must be None or a positive integer')
|
2016-10-13 20:39:44 +08:00
|
|
|
self.pages_per_range = pages_per_range
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(fields, name)
|
2016-10-13 20:39:44 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
if self.pages_per_range is not None:
|
|
|
|
return '<%(name)s: fields=%(fields)s, pages_per_range=%(pages_per_range)s>' % {
|
|
|
|
'name': self.__class__.__name__,
|
|
|
|
'fields': "'{}'".format(', '.join(self.fields)),
|
|
|
|
'pages_per_range': self.pages_per_range,
|
|
|
|
}
|
|
|
|
else:
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().__repr__()
|
2016-10-13 20:39:44 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
path, args, kwargs = super().deconstruct()
|
2016-10-13 20:39:44 +08:00
|
|
|
kwargs['pages_per_range'] = self.pages_per_range
|
|
|
|
return path, args, kwargs
|
|
|
|
|
|
|
|
def get_sql_create_template_values(self, model, schema_editor, using):
|
2017-01-21 21:13:44 +08:00
|
|
|
parameters = super().get_sql_create_template_values(model, schema_editor, using=' USING brin')
|
2016-10-13 20:39:44 +08:00
|
|
|
if self.pages_per_range is not None:
|
|
|
|
parameters['extra'] = ' WITH (pages_per_range={})'.format(
|
|
|
|
schema_editor.quote_value(self.pages_per_range)) + parameters['extra']
|
|
|
|
return parameters
|
2016-08-08 19:50:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GinIndex(Index):
|
|
|
|
suffix = 'gin'
|
|
|
|
|
|
|
|
def create_sql(self, model, schema_editor):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().create_sql(model, schema_editor, using=' USING gin')
|