summaryrefslogtreecommitdiff
path: root/django/db/backends/mysql/schema.py
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2022-02-03 20:24:19 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-02-07 20:37:05 +0100
commit9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch)
treef0506b668a013d0063e5fba3dbf4863b466713ba /django/db/backends/mysql/schema.py
parentf68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff)
downloaddjango-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/db/backends/mysql/schema.py')
-rw-r--r--django/db/backends/mysql/schema.py65
1 files changed, 40 insertions, 25 deletions
diff --git a/django/db/backends/mysql/schema.py b/django/db/backends/mysql/schema.py
index 17827c2195..562b209eef 100644
--- a/django/db/backends/mysql/schema.py
+++ b/django/db/backends/mysql/schema.py
@@ -10,24 +10,26 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
sql_alter_column_not_null = "MODIFY %(column)s %(type)s NOT NULL"
sql_alter_column_type = "MODIFY %(column)s %(type)s"
sql_alter_column_collate = "MODIFY %(column)s %(type)s%(collation)s"
- sql_alter_column_no_default_null = 'ALTER COLUMN %(column)s SET DEFAULT NULL'
+ sql_alter_column_no_default_null = "ALTER COLUMN %(column)s SET DEFAULT NULL"
# No 'CASCADE' which works as a no-op in MySQL but is undocumented
sql_delete_column = "ALTER TABLE %(table)s DROP COLUMN %(column)s"
sql_delete_unique = "ALTER TABLE %(table)s DROP INDEX %(name)s"
sql_create_column_inline_fk = (
- ', ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) '
- 'REFERENCES %(to_table)s(%(to_column)s)'
+ ", ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) "
+ "REFERENCES %(to_table)s(%(to_column)s)"
)
sql_delete_fk = "ALTER TABLE %(table)s DROP FOREIGN KEY %(name)s"
sql_delete_index = "DROP INDEX %(name)s ON %(table)s"
- sql_create_pk = "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s PRIMARY KEY (%(columns)s)"
+ sql_create_pk = (
+ "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s PRIMARY KEY (%(columns)s)"
+ )
sql_delete_pk = "ALTER TABLE %(table)s DROP PRIMARY KEY"
- sql_create_index = 'CREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)s'
+ sql_create_index = "CREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)s"
@property
def sql_delete_check(self):
@@ -35,8 +37,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# The name of the column check constraint is the same as the field
# name on MariaDB. Adding IF EXISTS clause prevents migrations
# crash. Constraint is removed during a "MODIFY" column statement.
- return 'ALTER TABLE %(table)s DROP CONSTRAINT IF EXISTS %(name)s'
- return 'ALTER TABLE %(table)s DROP CHECK %(name)s'
+ return "ALTER TABLE %(table)s DROP CONSTRAINT IF EXISTS %(name)s"
+ return "ALTER TABLE %(table)s DROP CHECK %(name)s"
@property
def sql_rename_column(self):
@@ -47,21 +49,26 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
return super().sql_rename_column
elif self.connection.mysql_version >= (8, 0, 4):
return super().sql_rename_column
- return 'ALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)s'
+ return "ALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)s"
def quote_value(self, value):
self.connection.ensure_connection()
if isinstance(value, str):
- value = value.replace('%', '%%')
+ value = value.replace("%", "%%")
# MySQLdb escapes to string, PyMySQL to bytes.
- quoted = self.connection.connection.escape(value, self.connection.connection.encoders)
+ quoted = self.connection.connection.escape(
+ value, self.connection.connection.encoders
+ )
if isinstance(value, str) and isinstance(quoted, bytes):
quoted = quoted.decode()
return quoted
def _is_limited_data_type(self, field):
db_type = field.db_type(self.connection)
- return db_type is not None and db_type.lower() in self.connection._limited_data_types
+ return (
+ db_type is not None
+ and db_type.lower() in self.connection._limited_data_types
+ )
def skip_default(self, field):
if not self._supports_limited_data_type_defaults:
@@ -84,13 +91,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
def _column_default_sql(self, field):
if (
- not self.connection.mysql_is_mariadb and
- self._supports_limited_data_type_defaults and
- self._is_limited_data_type(field)
+ not self.connection.mysql_is_mariadb
+ and self._supports_limited_data_type_defaults
+ and self._is_limited_data_type(field)
):
# MySQL supports defaults for BLOB and TEXT columns only if the
# default value is written as an expression i.e. in parentheses.
- return '(%s)'
+ return "(%s)"
return super()._column_default_sql(field)
def add_field(self, model, field):
@@ -100,10 +107,14 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# field.default may be unhashable, so a set isn't used for "in" check.
if self.skip_default(field) and field.default not in (None, NOT_PROVIDED):
effective_default = self.effective_default(field)
- self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
- 'table': self.quote_name(model._meta.db_table),
- 'column': self.quote_name(field.column),
- }, [effective_default])
+ self.execute(
+ "UPDATE %(table)s SET %(column)s = %%s"
+ % {
+ "table": self.quote_name(model._meta.db_table),
+ "column": self.quote_name(field.column),
+ },
+ [effective_default],
+ )
def _field_should_be_indexed(self, model, field):
if not super()._field_should_be_indexed(model, field):
@@ -115,9 +126,11 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# No need to create an index for ForeignKey fields except if
# db_constraint=False because the index from that constraint won't be
# created.
- if (storage == "InnoDB" and
- field.get_internal_type() == 'ForeignKey' and
- field.db_constraint):
+ if (
+ storage == "InnoDB"
+ and field.get_internal_type() == "ForeignKey"
+ and field.db_constraint
+ ):
return False
return not self._is_limited_data_type(field)
@@ -131,11 +144,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
recreate a FK index.
"""
first_field = model._meta.get_field(fields[0])
- if first_field.get_internal_type() == 'ForeignKey':
- constraint_names = self._constraint_names(model, [first_field.column], index=True)
+ if first_field.get_internal_type() == "ForeignKey":
+ constraint_names = self._constraint_names(
+ model, [first_field.column], index=True
+ )
if not constraint_names:
self.execute(
- self._create_index_sql(model, fields=[first_field], suffix='')
+ self._create_index_sql(model, fields=[first_field], suffix="")
)
return super()._delete_composed_index(model, fields, *args)