Unified construction of WITH SQL in contrib.postgres.indexes.
@@ -14,6 +14,19 @@ class PostgresIndex(Index):
# 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'
@@ -30,13 +43,11 @@ class BrinIndex(PostgresIndex):
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):
@@ -55,16 +66,13 @@ class GinIndex(PostgresIndex):
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):
@@ -83,13 +91,10 @@ class GistIndex(PostgresIndex):
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
