diff options
author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
tree | f0506b668a013d0063e5fba3dbf4863b466713ba /django/db/backends/postgresql/schema.py | |
parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
download | django-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/db/backends/postgresql/schema.py')
-rw-r--r-- | django/db/backends/postgresql/schema.py | 204 |
1 files changed, 141 insertions, 63 deletions
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py index f3b5baecbe..47e9a6a8f3 100644 --- a/django/db/backends/postgresql/schema.py +++ b/django/db/backends/postgresql/schema.py @@ -9,16 +9,18 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): sql_create_sequence = "CREATE SEQUENCE %(sequence)s" sql_delete_sequence = "DROP SEQUENCE IF EXISTS %(sequence)s CASCADE" - sql_set_sequence_max = "SELECT setval('%(sequence)s', MAX(%(column)s)) FROM %(table)s" - sql_set_sequence_owner = 'ALTER SEQUENCE %(sequence)s OWNED BY %(table)s.%(column)s' + sql_set_sequence_max = ( + "SELECT setval('%(sequence)s', MAX(%(column)s)) FROM %(table)s" + ) + sql_set_sequence_owner = "ALTER SEQUENCE %(sequence)s OWNED BY %(table)s.%(column)s" sql_create_index = ( - 'CREATE INDEX %(name)s ON %(table)s%(using)s ' - '(%(columns)s)%(include)s%(extra)s%(condition)s' + "CREATE INDEX %(name)s ON %(table)s%(using)s " + "(%(columns)s)%(include)s%(extra)s%(condition)s" ) sql_create_index_concurrently = ( - 'CREATE INDEX CONCURRENTLY %(name)s ON %(table)s%(using)s ' - '(%(columns)s)%(include)s%(extra)s%(condition)s' + "CREATE INDEX CONCURRENTLY %(name)s ON %(table)s%(using)s " + "(%(columns)s)%(include)s%(extra)s%(condition)s" ) sql_delete_index = "DROP INDEX IF EXISTS %(name)s" sql_delete_index_concurrently = "DROP INDEX CONCURRENTLY IF EXISTS %(name)s" @@ -26,21 +28,21 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): # Setting the constraint to IMMEDIATE to allow changing data in the same # transaction. sql_create_column_inline_fk = ( - 'CONSTRAINT %(name)s REFERENCES %(to_table)s(%(to_column)s)%(deferrable)s' - '; SET CONSTRAINTS %(namespace)s%(name)s IMMEDIATE' + "CONSTRAINT %(name)s REFERENCES %(to_table)s(%(to_column)s)%(deferrable)s" + "; SET CONSTRAINTS %(namespace)s%(name)s IMMEDIATE" ) # Setting the constraint to IMMEDIATE runs any deferred checks to allow # dropping it in the same transaction. sql_delete_fk = "SET CONSTRAINTS %(name)s IMMEDIATE; ALTER TABLE %(table)s DROP CONSTRAINT %(name)s" - sql_delete_procedure = 'DROP FUNCTION %(procedure)s(%(param_types)s)' + sql_delete_procedure = "DROP FUNCTION %(procedure)s(%(param_types)s)" def quote_value(self, value): if isinstance(value, str): - value = value.replace('%', '%%') + value = value.replace("%", "%%") adapted = psycopg2.extensions.adapt(value) - if hasattr(adapted, 'encoding'): - adapted.encoding = 'utf8' + if hasattr(adapted, "encoding"): + adapted.encoding = "utf8" # getquoted() returns a quoted bytestring of the adapted value. return adapted.getquoted().decode() @@ -61,7 +63,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): def _field_base_data_types(self, field): # Yield base data types for array fields. - if field.base_field.get_internal_type() == 'ArrayField': + if field.base_field.get_internal_type() == "ArrayField": yield from self._field_base_data_types(field.base_field) else: yield self._field_data_type(field.base_field) @@ -80,45 +82,52 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): # # The same doesn't apply to array fields such as varchar[size] # and text[size], so skip them. - if '[' in db_type: + if "[" in db_type: return None - if db_type.startswith('varchar'): + if db_type.startswith("varchar"): return self._create_index_sql( model, fields=[field], - suffix='_like', - opclasses=['varchar_pattern_ops'], + suffix="_like", + opclasses=["varchar_pattern_ops"], ) - elif db_type.startswith('text'): + elif db_type.startswith("text"): return self._create_index_sql( model, fields=[field], - suffix='_like', - opclasses=['text_pattern_ops'], + suffix="_like", + opclasses=["text_pattern_ops"], ) return None def _alter_column_type_sql(self, model, old_field, new_field, new_type): - self.sql_alter_column_type = 'ALTER COLUMN %(column)s TYPE %(type)s' + self.sql_alter_column_type = "ALTER COLUMN %(column)s TYPE %(type)s" # Cast when data type changed. - using_sql = ' USING %(column)s::%(type)s' + using_sql = " USING %(column)s::%(type)s" new_internal_type = new_field.get_internal_type() old_internal_type = old_field.get_internal_type() - if new_internal_type == 'ArrayField' and new_internal_type == old_internal_type: + if new_internal_type == "ArrayField" and new_internal_type == old_internal_type: # Compare base data types for array fields. - if list(self._field_base_data_types(old_field)) != list(self._field_base_data_types(new_field)): + if list(self._field_base_data_types(old_field)) != list( + self._field_base_data_types(new_field) + ): self.sql_alter_column_type += using_sql elif self._field_data_type(old_field) != self._field_data_type(new_field): self.sql_alter_column_type += using_sql # Make ALTER TYPE with SERIAL make sense. table = strip_quotes(model._meta.db_table) - serial_fields_map = {'bigserial': 'bigint', 'serial': 'integer', 'smallserial': 'smallint'} + serial_fields_map = { + "bigserial": "bigint", + "serial": "integer", + "smallserial": "smallint", + } if new_type.lower() in serial_fields_map: column = strip_quotes(new_field.column) sequence_name = "%s_%s_seq" % (table, column) return ( ( - self.sql_alter_column_type % { + self.sql_alter_column_type + % { "column": self.quote_name(column), "type": serial_fields_map[new_type.lower()], }, @@ -126,29 +135,35 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): ), [ ( - self.sql_delete_sequence % { + self.sql_delete_sequence + % { "sequence": self.quote_name(sequence_name), }, [], ), ( - self.sql_create_sequence % { + self.sql_create_sequence + % { "sequence": self.quote_name(sequence_name), }, [], ), ( - self.sql_alter_column % { + self.sql_alter_column + % { "table": self.quote_name(table), - "changes": self.sql_alter_column_default % { + "changes": self.sql_alter_column_default + % { "column": self.quote_name(column), - "default": "nextval('%s')" % self.quote_name(sequence_name), - } + "default": "nextval('%s')" + % self.quote_name(sequence_name), + }, }, [], ), ( - self.sql_set_sequence_max % { + self.sql_set_sequence_max + % { "table": self.quote_name(table), "column": self.quote_name(column), "sequence": self.quote_name(sequence_name), @@ -156,24 +171,31 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): [], ), ( - self.sql_set_sequence_owner % { - 'table': self.quote_name(table), - 'column': self.quote_name(column), - 'sequence': self.quote_name(sequence_name), + self.sql_set_sequence_owner + % { + "table": self.quote_name(table), + "column": self.quote_name(column), + "sequence": self.quote_name(sequence_name), }, [], ), ], ) - elif old_field.db_parameters(connection=self.connection)['type'] in serial_fields_map: + elif ( + old_field.db_parameters(connection=self.connection)["type"] + in serial_fields_map + ): # Drop the sequence if migrating away from AutoField. column = strip_quotes(new_field.column) - sequence_name = '%s_%s_seq' % (table, column) - fragment, _ = super()._alter_column_type_sql(model, old_field, new_field, new_type) + sequence_name = "%s_%s_seq" % (table, column) + fragment, _ = super()._alter_column_type_sql( + model, old_field, new_field, new_type + ) return fragment, [ ( - self.sql_delete_sequence % { - 'sequence': self.quote_name(sequence_name), + self.sql_delete_sequence + % { + "sequence": self.quote_name(sequence_name), }, [], ), @@ -181,58 +203,114 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): else: return super()._alter_column_type_sql(model, old_field, new_field, new_type) - def _alter_field(self, model, old_field, new_field, old_type, new_type, - old_db_params, new_db_params, strict=False): + def _alter_field( + self, + model, + old_field, + new_field, + old_type, + new_type, + old_db_params, + new_db_params, + strict=False, + ): # Drop indexes on varchar/text/citext columns that are changing to a # different type. if (old_field.db_index or old_field.unique) and ( - (old_type.startswith('varchar') and not new_type.startswith('varchar')) or - (old_type.startswith('text') and not new_type.startswith('text')) or - (old_type.startswith('citext') and not new_type.startswith('citext')) + (old_type.startswith("varchar") and not new_type.startswith("varchar")) + or (old_type.startswith("text") and not new_type.startswith("text")) + or (old_type.startswith("citext") and not new_type.startswith("citext")) ): - index_name = self._create_index_name(model._meta.db_table, [old_field.column], suffix='_like') + index_name = self._create_index_name( + model._meta.db_table, [old_field.column], suffix="_like" + ) self.execute(self._delete_index_sql(model, index_name)) super()._alter_field( - model, old_field, new_field, old_type, new_type, old_db_params, - new_db_params, strict, + model, + old_field, + new_field, + old_type, + new_type, + old_db_params, + new_db_params, + strict, ) # Added an index? Create any PostgreSQL-specific indexes. - if ((not (old_field.db_index or old_field.unique) and new_field.db_index) or - (not old_field.unique and new_field.unique)): + if (not (old_field.db_index or old_field.unique) and new_field.db_index) or ( + not old_field.unique and new_field.unique + ): like_index_statement = self._create_like_index_sql(model, new_field) if like_index_statement is not None: self.execute(like_index_statement) # 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._meta.db_table, [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_index_sql(model, index_to_remove)) def _index_columns(self, table, columns, col_suffixes, opclasses): if opclasses: - return IndexColumns(table, columns, self.quote_name, col_suffixes=col_suffixes, opclasses=opclasses) + return IndexColumns( + table, + columns, + self.quote_name, + col_suffixes=col_suffixes, + opclasses=opclasses, + ) return super()._index_columns(table, columns, col_suffixes, opclasses) def add_index(self, model, index, concurrently=False): - self.execute(index.create_sql(model, self, concurrently=concurrently), params=None) + self.execute( + index.create_sql(model, self, concurrently=concurrently), params=None + ) def remove_index(self, model, index, concurrently=False): self.execute(index.remove_sql(model, self, concurrently=concurrently)) def _delete_index_sql(self, model, name, sql=None, concurrently=False): - sql = self.sql_delete_index_concurrently if concurrently else self.sql_delete_index + sql = ( + self.sql_delete_index_concurrently + if concurrently + else self.sql_delete_index + ) return super()._delete_index_sql(model, name, sql) def _create_index_sql( - self, model, *, fields=None, name=None, suffix='', using='', - db_tablespace=None, col_suffixes=(), sql=None, opclasses=(), - condition=None, concurrently=False, include=None, expressions=None, + self, + model, + *, + fields=None, + name=None, + suffix="", + using="", + db_tablespace=None, + col_suffixes=(), + sql=None, + opclasses=(), + condition=None, + concurrently=False, + include=None, + expressions=None, ): - sql = self.sql_create_index if not concurrently else self.sql_create_index_concurrently + sql = ( + self.sql_create_index + if not concurrently + else self.sql_create_index_concurrently + ) return super()._create_index_sql( - model, fields=fields, name=name, suffix=suffix, using=using, - db_tablespace=db_tablespace, col_suffixes=col_suffixes, sql=sql, - opclasses=opclasses, condition=condition, include=include, + model, + fields=fields, + name=name, + suffix=suffix, + using=using, + db_tablespace=db_tablespace, + col_suffixes=col_suffixes, + sql=sql, + opclasses=opclasses, + condition=condition, + include=include, expressions=expressions, ) |