From ea91ad4c131816fd0ea8d5f1bfb21b7abd82b47e Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sat, 1 Apr 2017 14:00:47 -0400 Subject: [PATCH] Refs #25530 -- Changed _create_index_name to take a table as first parameter. --- django/db/backends/base/schema.py | 18 +++++++++++------- django/db/backends/postgresql/schema.py | 4 ++-- docs/releases/2.0.txt | 3 +++ tests/indexes/tests.py | 6 +++--- tests/schema/tests.py | 2 +- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 11316a990c..20243ae070 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -674,7 +674,9 @@ class BaseDatabaseSchemaEditor: self.execute( self.sql_create_pk % { "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), } ) @@ -711,7 +713,9 @@ class BaseDatabaseSchemaEditor: self.execute( self.sql_create_check % { "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), "check": new_db_params['check'], } @@ -824,14 +828,14 @@ class BaseDatabaseSchemaEditor: 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. The name is divided into 3 parts: the table name, the column names, 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_suffix_part = '%s%s' % (self._digest(*hash_data), suffix) 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 return sql_create_index % { "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": "", "columns": ", ".join(self.quote_name(column) for column in columns), "extra": tablespace_sql, @@ -933,7 +937,7 @@ class BaseDatabaseSchemaEditor: return self.sql_create_fk % { "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), "to_table": self.quote_name(to_table), "to_column": self.quote_name(to_column), @@ -943,7 +947,7 @@ class BaseDatabaseSchemaEditor: def _create_unique_sql(self, model, columns): return self.sql_create_unique % { "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), } diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py index 1495de29c4..1e505a2134 100644 --- a/django/db/backends/postgresql/schema.py +++ b/django/db/backends/postgresql/schema.py @@ -111,7 +111,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): (old_type.startswith('varchar') and not new_type.startswith('varchar')) or (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)) super()._alter_field( @@ -127,5 +127,5 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): # Removed an index? Drop any PostgreSQL-specific indexes. 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)) diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt index 5170e66cbe..37bfb48d89 100644 --- a/docs/releases/2.0.txt +++ b/docs/releases/2.0.txt @@ -321,6 +321,9 @@ backends. * The first argument of ``SchemaEditor._alter_column_type_sql()`` is now ``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 ------------------------------- diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py index b266d71e9a..c2d76feeb9 100644 --- a/tests/indexes/tests.py +++ b/tests/indexes/tests.py @@ -19,7 +19,7 @@ class SchemaIndexesTests(TestCase): """ with connection.schema_editor() as editor: index_name = editor._create_index_name( - model=Article, + table_name=Article._meta.db_table, column_names=("c1",), suffix="123", ) @@ -35,7 +35,7 @@ class SchemaIndexesTests(TestCase): long_name = 'l%sng' % ('o' * 100) with connection.schema_editor() as editor: index_name = editor._create_index_name( - model=Article, + table_name=Article._meta.db_table, column_names=('c1', 'c2', long_name), suffix='ix', ) @@ -56,7 +56,7 @@ class SchemaIndexesTests(TestCase): # Ensure the index name is properly quoted self.assertIn( 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] ) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index ecc6aeb1c6..d2707ecea7 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -1640,7 +1640,7 @@ class SchemaTests(TransactionTestCase): author_index_name = index.name with connection.schema_editor() as editor: db_index_name = editor._create_index_name( - model=AuthorWithIndexedName, + table_name=AuthorWithIndexedName._meta.db_table, column_names=('name',), ) if connection.features.uppercases_column_names: