Fixed #30754 -- Prevented inclusion of aliases in partial index conditions.

SQLite doesn't repoint table aliases in partial index conditions on table
rename which breaks the documented table alteration procedure.

Thanks Pēteris Caune for the report.
This commit is contained in:
Simon Charette 2019-09-08 20:31:43 -04:00 committed by Mariusz Felisiak
parent a624803514
commit 34decdebf1
3 changed files with 11 additions and 13 deletions

View File

@ -41,13 +41,10 @@ class Index:
if self.condition is None: if self.condition is None:
return None return None
query = Query(model=model) query = Query(model=model)
query.add_q(self.condition) where = query.build_where(self.condition)
compiler = query.get_compiler(connection=schema_editor.connection) compiler = query.get_compiler(connection=schema_editor.connection)
# Only the WhereNode is of interest for the partial index. sql, params = where.as_sql(compiler, schema_editor.connection)
sql, params = query.where.as_sql(compiler=compiler, connection=schema_editor.connection) return sql % tuple(schema_editor.quote_value(p) for p in params)
# BaseDatabaseSchemaEditor does the same map on the params, but since
# it's handled outside of that class, the work is done here.
return sql % tuple(map(schema_editor.quote_value, params))
def create_sql(self, model, schema_editor, using='', **kwargs): def create_sql(self, model, schema_editor, using='', **kwargs):
fields = [model._meta.get_field(field_name) for field_name, _ in self.fields_orders] fields = [model._meta.get_field(field_name) for field_name, _ in self.fields_orders]

View File

@ -9,4 +9,5 @@ Django 2.2.6 fixes several bugs in 2.2.5.
Bugfixes Bugfixes
======== ========
* ... * Fixed migrations crash on SQLite when altering a model containing partial
indexes (:ticket:`30754`).

View File

@ -108,7 +108,7 @@ class PartialIndexConditionIgnoredTests(TransactionTestCase):
editor.add_index(Article, index) editor.add_index(Article, index)
self.assertNotIn( self.assertNotIn(
'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), 'published'), 'WHERE %s' % editor.quote_name('published'),
str(index.create_sql(Article, editor)) str(index.create_sql(Article, editor))
) )
@ -258,7 +258,7 @@ class PartialIndexTests(TransactionTestCase):
) )
) )
self.assertIn( self.assertIn(
'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")), 'WHERE %s' % editor.quote_name('pub_date'),
str(index.create_sql(Article, schema_editor=editor)) str(index.create_sql(Article, schema_editor=editor))
) )
editor.add_index(index=index, model=Article) editor.add_index(index=index, model=Article)
@ -275,7 +275,7 @@ class PartialIndexTests(TransactionTestCase):
condition=Q(pk__gt=1), condition=Q(pk__gt=1),
) )
self.assertIn( self.assertIn(
'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name('id')), 'WHERE %s' % editor.quote_name('id'),
str(index.create_sql(Article, schema_editor=editor)) str(index.create_sql(Article, schema_editor=editor))
) )
editor.add_index(index=index, model=Article) editor.add_index(index=index, model=Article)
@ -292,7 +292,7 @@ class PartialIndexTests(TransactionTestCase):
condition=Q(published=True), condition=Q(published=True),
) )
self.assertIn( self.assertIn(
'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name('published')), 'WHERE %s' % editor.quote_name('published'),
str(index.create_sql(Article, schema_editor=editor)) str(index.create_sql(Article, schema_editor=editor))
) )
editor.add_index(index=index, model=Article) editor.add_index(index=index, model=Article)
@ -319,7 +319,7 @@ class PartialIndexTests(TransactionTestCase):
sql = str(index.create_sql(Article, schema_editor=editor)) sql = str(index.create_sql(Article, schema_editor=editor))
where = sql.find('WHERE') where = sql.find('WHERE')
self.assertIn( self.assertIn(
'WHERE (%s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")), 'WHERE (%s' % editor.quote_name('pub_date'),
sql sql
) )
# Because each backend has different syntax for the operators, # Because each backend has different syntax for the operators,
@ -339,7 +339,7 @@ class PartialIndexTests(TransactionTestCase):
condition=Q(pub_date__isnull=False), condition=Q(pub_date__isnull=False),
) )
self.assertIn( self.assertIn(
'WHERE %s.%s IS NOT NULL' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")), 'WHERE %s IS NOT NULL' % editor.quote_name('pub_date'),
str(index.create_sql(Article, schema_editor=editor)) str(index.create_sql(Article, schema_editor=editor))
) )
editor.add_index(index=index, model=Article) editor.add_index(index=index, model=Article)