Fixed #30961 -- Fixed spaces in columns list SQL generated for indexes.

This commit is contained in:
Hannes Ljungberg 2019-11-07 21:26:08 +01:00 committed by Mariusz Felisiak
parent d5af43c8d1
commit 6d590bcf1f
2 changed files with 29 additions and 3 deletions

View File

@ -83,10 +83,14 @@ class Columns(TableColumns):
def __str__(self): def __str__(self):
def col_str(column, idx): def col_str(column, idx):
col = self.quote_name(column)
try: try:
return self.quote_name(column) + self.col_suffixes[idx] suffix = self.col_suffixes[idx]
if suffix:
col = '{} {}'.format(col, suffix)
except IndexError: except IndexError:
return self.quote_name(column) pass
return col
return ', '.join(col_str(column, idx) for idx, column in enumerate(self.columns)) return ', '.join(col_str(column, idx) for idx, column in enumerate(self.columns))
@ -114,7 +118,9 @@ class IndexColumns(Columns):
# length as self.columns. # length as self.columns.
col = '{} {}'.format(self.quote_name(column), self.opclasses[idx]) col = '{} {}'.format(self.quote_name(column), self.opclasses[idx])
try: try:
col = '{} {}'.format(col, self.col_suffixes[idx]) suffix = self.col_suffixes[idx]
if suffix:
col = '{} {}'.format(col, suffix)
except IndexError: except IndexError:
pass pass
return col return col

View File

@ -83,6 +83,14 @@ class SchemaIndexesTests(TestCase):
str(index.create_sql(Article, editor)), str(index.create_sql(Article, editor)),
) )
def test_descending_columns_list_sql(self):
index = Index(fields=['-headline'], name='whitespace_idx')
editor = connection.schema_editor()
self.assertIn(
'(%s DESC)' % editor.quote_name('headline'),
str(index.create_sql(Article, editor)),
)
@skipIf(connection.vendor == 'postgresql', 'opclasses are PostgreSQL only') @skipIf(connection.vendor == 'postgresql', 'opclasses are PostgreSQL only')
class SchemaIndexesNotPostgreSQLTests(TransactionTestCase): class SchemaIndexesNotPostgreSQLTests(TransactionTestCase):
@ -231,6 +239,18 @@ class SchemaIndexesPostgreSQLTests(TransactionTestCase):
cursor.execute(self.get_opclass_query % indexname) cursor.execute(self.get_opclass_query % indexname)
self.assertCountEqual(cursor.fetchall(), [('text_pattern_ops', indexname)]) self.assertCountEqual(cursor.fetchall(), [('text_pattern_ops', indexname)])
def test_ops_class_columns_lists_sql(self):
index = Index(
fields=['headline'],
name='whitespace_idx',
opclasses=['text_pattern_ops'],
)
with connection.schema_editor() as editor:
self.assertIn(
'(%s text_pattern_ops)' % editor.quote_name('headline'),
str(index.create_sql(Article, editor)),
)
def test_ops_class_descending_columns_list_sql(self): def test_ops_class_descending_columns_list_sql(self):
index = Index( index = Index(
fields=['-headline'], fields=['-headline'],