Skip to content

Commit

Permalink
Unified construction of WITH SQL in contrib.postgres.indexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Jan 10, 2018
1 parent 5bf6282 commit db9cd1b
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions django/contrib/postgres/indexes.py
Expand Up @@ -14,6 +14,19 @@ def max_name_length(self):
# indexes.
return Index.max_name_length - len(Index.suffix) + len(self.suffix)

def create_sql(self, model, schema_editor, using=''):
statement = super().create_sql(model, schema_editor, using=' USING %s' % self.suffix)
with_params = self.get_with_params()
if with_params:
statement.parts['extra'] = 'WITH (%s) %s' % (
', '.join(with_params),
statement.parts['extra'],
)
return statement

def get_with_params(self):
return []


class BrinIndex(PostgresIndex):
suffix = 'brin'
Expand All @@ -30,13 +43,11 @@ def deconstruct(self):
kwargs['pages_per_range'] = self.pages_per_range
return path, args, kwargs

def create_sql(self, model, schema_editor, using=''):
statement = super().create_sql(model, schema_editor, using=' USING brin')
def get_with_params(self):
with_params = []
if self.pages_per_range is not None:
statement.parts['extra'] = ' WITH (pages_per_range={})'.format(
schema_editor.quote_value(self.pages_per_range)
) + statement.parts['extra']
return statement
with_params.append('pages_per_range = %d' % self.pages_per_range)
return with_params


class GinIndex(PostgresIndex):
Expand All @@ -55,16 +66,13 @@ def deconstruct(self):
kwargs['gin_pending_list_limit'] = self.gin_pending_list_limit
return path, args, kwargs

def create_sql(self, model, schema_editor, using=''):
statement = super().create_sql(model, schema_editor, using=' USING gin')
def get_with_params(self):
with_params = []
if self.gin_pending_list_limit is not None:
with_params.append('gin_pending_list_limit = %d' % self.gin_pending_list_limit)
if self.fastupdate is not None:
with_params.append('fastupdate = {}'.format('on' if self.fastupdate else 'off'))
if with_params:
statement.parts['extra'] = 'WITH ({}) {}'.format(', '.join(with_params), statement.parts['extra'])
return statement
with_params.append('fastupdate = %s' % ('on' if self.fastupdate else 'off'))
return with_params


class GistIndex(PostgresIndex):
Expand All @@ -83,13 +91,10 @@ def deconstruct(self):
kwargs['fillfactor'] = self.fillfactor
return path, args, kwargs

def create_sql(self, model, schema_editor):
statement = super().create_sql(model, schema_editor, using=' USING gist')
def get_with_params(self):
with_params = []
if self.buffering is not None:
with_params.append('buffering = {}'.format('on' if self.buffering else 'off'))
with_params.append('buffering = %s' % ('on' if self.buffering else 'off'))
if self.fillfactor is not None:
with_params.append('fillfactor = %s' % self.fillfactor)
if with_params:
statement.parts['extra'] = 'WITH ({}) {}'.format(', '.join(with_params), statement.parts['extra'])
return statement
return with_params

0 comments on commit db9cd1b

Please sign in to comment.