2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
2014-05-18 12:58:16 +08:00
|
|
|
from django.db.models import NOT_PROVIDED
|
2012-08-18 19:29:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
|
|
|
|
|
|
|
|
sql_rename_table = "RENAME TABLE %(old_table)s TO %(new_table)s"
|
|
|
|
|
|
|
|
sql_alter_column_null = "MODIFY %(column)s %(type)s NULL"
|
2012-09-24 19:52:43 +08:00
|
|
|
sql_alter_column_not_null = "MODIFY %(column)s %(type)s NOT NULL"
|
2012-08-18 20:48:54 +08:00
|
|
|
sql_alter_column_type = "MODIFY %(column)s %(type)s"
|
2016-11-06 05:23:57 +08:00
|
|
|
|
|
|
|
# No 'CASCADE' which works as a no-op in MySQL but is undocumented
|
|
|
|
sql_delete_column = "ALTER TABLE %(table)s DROP COLUMN %(column)s"
|
|
|
|
|
2012-08-18 20:48:54 +08:00
|
|
|
sql_rename_column = "ALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)s"
|
2012-08-18 19:29:31 +08:00
|
|
|
|
|
|
|
sql_delete_unique = "ALTER TABLE %(table)s DROP INDEX %(name)s"
|
|
|
|
|
|
|
|
sql_delete_fk = "ALTER TABLE %(table)s DROP FOREIGN KEY %(name)s"
|
|
|
|
|
2012-08-31 06:11:56 +08:00
|
|
|
sql_delete_index = "DROP INDEX %(name)s ON %(table)s"
|
2012-08-18 19:29:31 +08:00
|
|
|
|
2012-09-05 00:53:31 +08:00
|
|
|
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"
|
2014-02-09 20:41:55 +08:00
|
|
|
|
|
|
|
def quote_value(self, value):
|
2018-02-09 23:04:00 +08:00
|
|
|
self.connection.ensure_connection()
|
|
|
|
quoted = self.connection.connection.escape(value, self.connection.connection.encoders)
|
|
|
|
if isinstance(value, str):
|
|
|
|
quoted = quoted.decode()
|
|
|
|
return quoted
|
2014-05-18 12:58:16 +08:00
|
|
|
|
2017-05-23 23:02:40 +08:00
|
|
|
def _is_limited_data_type(self, field):
|
2015-05-24 18:16:21 +08:00
|
|
|
db_type = field.db_type(self.connection)
|
2017-05-23 23:02:40 +08:00
|
|
|
return db_type is not None and db_type.lower() in self.connection._limited_data_types
|
|
|
|
|
|
|
|
def skip_default(self, field):
|
|
|
|
return self._is_limited_data_type(field)
|
2014-05-18 12:58:16 +08:00
|
|
|
|
|
|
|
def add_field(self, model, field):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().add_field(model, field)
|
2014-05-18 12:58:16 +08:00
|
|
|
|
|
|
|
# Simulate the effect of a one-off default.
|
2015-09-13 03:06:35 +08:00
|
|
|
# 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):
|
2014-05-18 12:58:16 +08:00
|
|
|
effective_default = self.effective_default(field)
|
2014-05-20 22:41:01 +08:00
|
|
|
self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
|
2014-05-18 12:58:16 +08:00
|
|
|
'table': self.quote_name(model._meta.db_table),
|
|
|
|
'column': self.quote_name(field.column),
|
2014-05-20 22:41:01 +08:00
|
|
|
}, [effective_default])
|
2014-12-24 22:55:57 +08:00
|
|
|
|
2016-06-28 20:22:20 +08:00
|
|
|
def _field_should_be_indexed(self, model, field):
|
2017-01-21 21:13:44 +08:00
|
|
|
create_index = super()._field_should_be_indexed(model, field)
|
2014-12-24 22:55:57 +08:00
|
|
|
storage = self.connection.introspection.get_storage_engine(
|
|
|
|
self.connection.cursor(), model._meta.db_table
|
|
|
|
)
|
2016-06-28 20:22:20 +08:00
|
|
|
# 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
|
|
|
|
create_index and
|
|
|
|
field.get_internal_type() == 'ForeignKey' and
|
|
|
|
field.db_constraint):
|
|
|
|
return False
|
2018-01-04 07:52:12 +08:00
|
|
|
return not self._is_limited_data_type(field) and create_index
|
2015-04-11 22:10:31 +08:00
|
|
|
|
2015-05-13 23:40:57 +08:00
|
|
|
def _delete_composed_index(self, model, fields, *args):
|
|
|
|
"""
|
|
|
|
MySQL can remove an implicit FK index on a field when that field is
|
|
|
|
covered by another index like a unique_together. "covered" here means
|
|
|
|
that the more complex index starts like the simpler one.
|
|
|
|
http://bugs.mysql.com/bug.php?id=37910 / Django ticket #24757
|
|
|
|
We check here before removing the [unique|index]_together if we have to
|
|
|
|
recreate a FK index.
|
|
|
|
"""
|
|
|
|
first_field = model._meta.get_field(fields[0])
|
|
|
|
if first_field.get_internal_type() == 'ForeignKey':
|
2015-06-12 14:22:08 +08:00
|
|
|
constraint_names = self._constraint_names(model, [first_field.column], index=True)
|
2015-05-13 23:40:57 +08:00
|
|
|
if not constraint_names:
|
2015-06-12 14:22:08 +08:00
|
|
|
self.execute(self._create_index_sql(model, [first_field], suffix=""))
|
2017-01-21 21:13:44 +08:00
|
|
|
return super()._delete_composed_index(model, fields, *args)
|
2015-05-13 23:40:57 +08:00
|
|
|
|
2015-05-27 06:18:21 +08:00
|
|
|
def _set_field_new_type_null_status(self, field, new_type):
|
|
|
|
"""
|
|
|
|
Keep the null property of the old field. If it has changed, it will be
|
|
|
|
handled separately.
|
|
|
|
"""
|
|
|
|
if field.null:
|
2015-04-11 22:10:31 +08:00
|
|
|
new_type += " NULL"
|
|
|
|
else:
|
|
|
|
new_type += " NOT NULL"
|
2015-05-27 06:18:21 +08:00
|
|
|
return new_type
|
|
|
|
|
2017-06-06 23:08:40 +08:00
|
|
|
def _alter_column_type_sql(self, model, old_field, new_field, new_type):
|
2015-05-27 06:18:21 +08:00
|
|
|
new_type = self._set_field_new_type_null_status(old_field, new_type)
|
2017-06-06 23:08:40 +08:00
|
|
|
return super()._alter_column_type_sql(model, old_field, new_field, new_type)
|
2015-05-27 06:18:21 +08:00
|
|
|
|
|
|
|
def _rename_field_sql(self, table, old_field, new_field, new_type):
|
|
|
|
new_type = self._set_field_new_type_null_status(old_field, new_type)
|
2017-01-21 21:13:44 +08:00
|
|
|
return super()._rename_field_sql(table, old_field, new_field, new_type)
|