Refs #25530 -- Changed _create_index_name to take a table as first parameter.

This commit is contained in:
Simon Charette 2017-04-01 14:00:47 -04:00
parent ad524980ac
commit ea91ad4c13
5 changed files with 20 additions and 13 deletions

View File

@ -674,7 +674,9 @@ class BaseDatabaseSchemaEditor:
self.execute( self.execute(
self.sql_create_pk % { self.sql_create_pk % {
"table": self.quote_name(model._meta.db_table), "table": self.quote_name(model._meta.db_table),
"name": self.quote_name(self._create_index_name(model, [new_field.column], suffix="_pk")), "name": self.quote_name(
self._create_index_name(model._meta.db_table, [new_field.column], suffix="_pk")
),
"columns": self.quote_name(new_field.column), "columns": self.quote_name(new_field.column),
} }
) )
@ -711,7 +713,9 @@ class BaseDatabaseSchemaEditor:
self.execute( self.execute(
self.sql_create_check % { self.sql_create_check % {
"table": self.quote_name(model._meta.db_table), "table": self.quote_name(model._meta.db_table),
"name": self.quote_name(self._create_index_name(model, [new_field.column], suffix="_check")), "name": self.quote_name(
self._create_index_name(model._meta.db_table, [new_field.column], suffix="_check")
),
"column": self.quote_name(new_field.column), "column": self.quote_name(new_field.column),
"check": new_db_params['check'], "check": new_db_params['check'],
} }
@ -824,14 +828,14 @@ class BaseDatabaseSchemaEditor:
new_field.remote_field.through._meta.get_field(new_field.m2m_field_name()), new_field.remote_field.through._meta.get_field(new_field.m2m_field_name()),
) )
def _create_index_name(self, model, column_names, suffix=""): def _create_index_name(self, table_name, column_names, suffix=""):
""" """
Generate a unique name for an index/unique constraint. Generate a unique name for an index/unique constraint.
The name is divided into 3 parts: the table name, the column names, The name is divided into 3 parts: the table name, the column names,
and a unique digest and suffix. and a unique digest and suffix.
""" """
table_name = strip_quotes(model._meta.db_table) table_name = strip_quotes(table_name)
hash_data = [table_name] + list(column_names) hash_data = [table_name] + list(column_names)
hash_suffix_part = '%s%s' % (self._digest(*hash_data), suffix) hash_suffix_part = '%s%s' % (self._digest(*hash_data), suffix)
max_length = self.connection.ops.max_name_length() or 200 max_length = self.connection.ops.max_name_length() or 200
@ -876,7 +880,7 @@ class BaseDatabaseSchemaEditor:
sql_create_index = sql or self.sql_create_index sql_create_index = sql or self.sql_create_index
return sql_create_index % { return sql_create_index % {
"table": self.quote_name(model._meta.db_table), "table": self.quote_name(model._meta.db_table),
"name": self.quote_name(self._create_index_name(model, columns, suffix=suffix)), "name": self.quote_name(self._create_index_name(model._meta.db_table, columns, suffix=suffix)),
"using": "", "using": "",
"columns": ", ".join(self.quote_name(column) for column in columns), "columns": ", ".join(self.quote_name(column) for column in columns),
"extra": tablespace_sql, "extra": tablespace_sql,
@ -933,7 +937,7 @@ class BaseDatabaseSchemaEditor:
return self.sql_create_fk % { return self.sql_create_fk % {
"table": self.quote_name(from_table), "table": self.quote_name(from_table),
"name": self.quote_name(self._create_index_name(model, [from_column], suffix=suffix)), "name": self.quote_name(self._create_index_name(model._meta.db_table, [from_column], suffix=suffix)),
"column": self.quote_name(from_column), "column": self.quote_name(from_column),
"to_table": self.quote_name(to_table), "to_table": self.quote_name(to_table),
"to_column": self.quote_name(to_column), "to_column": self.quote_name(to_column),
@ -943,7 +947,7 @@ class BaseDatabaseSchemaEditor:
def _create_unique_sql(self, model, columns): def _create_unique_sql(self, model, columns):
return self.sql_create_unique % { return self.sql_create_unique % {
"table": self.quote_name(model._meta.db_table), "table": self.quote_name(model._meta.db_table),
"name": self.quote_name(self._create_index_name(model, columns, suffix="_uniq")), "name": self.quote_name(self._create_index_name(model._meta.db_table, columns, suffix="_uniq")),
"columns": ", ".join(self.quote_name(column) for column in columns), "columns": ", ".join(self.quote_name(column) for column in columns),
} }

View File

@ -111,7 +111,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
(old_type.startswith('varchar') and not new_type.startswith('varchar')) or (old_type.startswith('varchar') and not new_type.startswith('varchar')) or
(old_type.startswith('text') and not new_type.startswith('text')) (old_type.startswith('text') and not new_type.startswith('text'))
): ):
index_name = self._create_index_name(model, [old_field.column], suffix='_like') index_name = self._create_index_name(model._meta.db_table, [old_field.column], suffix='_like')
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name)) self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name))
super()._alter_field( super()._alter_field(
@ -127,5 +127,5 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# Removed an index? Drop any PostgreSQL-specific indexes. # Removed an index? Drop any PostgreSQL-specific indexes.
if old_field.unique and not (new_field.db_index or new_field.unique): if old_field.unique and not (new_field.db_index or new_field.unique):
index_to_remove = self._create_index_name(model, [old_field.column], suffix='_like') index_to_remove = self._create_index_name(model._meta.db_table, [old_field.column], suffix='_like')
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_to_remove)) self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_to_remove))

View File

@ -321,6 +321,9 @@ backends.
* The first argument of ``SchemaEditor._alter_column_type_sql()`` is now * The first argument of ``SchemaEditor._alter_column_type_sql()`` is now
``model`` rather than ``table``. ``model`` rather than ``table``.
* The first argument of ``SchemaEditor._create_index_name()`` is now
``table_name`` rather than ``model``.
Dropped support for Oracle 11.2 Dropped support for Oracle 11.2
------------------------------- -------------------------------

View File

@ -19,7 +19,7 @@ class SchemaIndexesTests(TestCase):
""" """
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
index_name = editor._create_index_name( index_name = editor._create_index_name(
model=Article, table_name=Article._meta.db_table,
column_names=("c1",), column_names=("c1",),
suffix="123", suffix="123",
) )
@ -35,7 +35,7 @@ class SchemaIndexesTests(TestCase):
long_name = 'l%sng' % ('o' * 100) long_name = 'l%sng' % ('o' * 100)
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
index_name = editor._create_index_name( index_name = editor._create_index_name(
model=Article, table_name=Article._meta.db_table,
column_names=('c1', 'c2', long_name), column_names=('c1', 'c2', long_name),
suffix='ix', suffix='ix',
) )
@ -56,7 +56,7 @@ class SchemaIndexesTests(TestCase):
# Ensure the index name is properly quoted # Ensure the index name is properly quoted
self.assertIn( self.assertIn(
connection.ops.quote_name( connection.ops.quote_name(
editor._create_index_name(Article, ['headline', 'pub_date'], suffix='_idx') editor._create_index_name(Article._meta.db_table, ['headline', 'pub_date'], suffix='_idx')
), ),
index_sql[0] index_sql[0]
) )

View File

@ -1640,7 +1640,7 @@ class SchemaTests(TransactionTestCase):
author_index_name = index.name author_index_name = index.name
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
db_index_name = editor._create_index_name( db_index_name = editor._create_index_name(
model=AuthorWithIndexedName, table_name=AuthorWithIndexedName._meta.db_table,
column_names=('name',), column_names=('name',),
) )
if connection.features.uppercases_column_names: if connection.features.uppercases_column_names: