You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

schema.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from django.db.backends.base.schema import BaseDatabaseSchemaEditor
  2. from django.db.models import NOT_PROVIDED
  3. class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
  4. sql_rename_table = "RENAME TABLE %(old_table)s TO %(new_table)s"
  5. sql_alter_column_null = "MODIFY %(column)s %(type)s NULL"
  6. sql_alter_column_not_null = "MODIFY %(column)s %(type)s NOT NULL"
  7. sql_alter_column_type = "MODIFY %(column)s %(type)s"
  8. # No 'CASCADE' which works as a no-op in MySQL but is undocumented
  9. sql_delete_column = "ALTER TABLE %(table)s DROP COLUMN %(column)s"
  10. sql_rename_column = "ALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)s"
  11. sql_delete_unique = "ALTER TABLE %(table)s DROP INDEX %(name)s"
  12. sql_delete_fk = "ALTER TABLE %(table)s DROP FOREIGN KEY %(name)s"
  13. sql_delete_index = "DROP INDEX %(name)s ON %(table)s"
  14. sql_create_pk = "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s PRIMARY KEY (%(columns)s)"
  15. sql_delete_pk = "ALTER TABLE %(table)s DROP PRIMARY KEY"
  16. sql_create_index = 'CREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)s'
  17. def quote_value(self, value):
  18. self.connection.ensure_connection()
  19. quoted = self.connection.connection.escape(value, self.connection.connection.encoders)
  20. if isinstance(value, str):
  21. quoted = quoted.decode()
  22. return quoted
  23. def _is_limited_data_type(self, field):
  24. db_type = field.db_type(self.connection)
  25. return db_type is not None and db_type.lower() in self.connection._limited_data_types
  26. def skip_default(self, field):
  27. return self._is_limited_data_type(field)
  28. def add_field(self, model, field):
  29. super().add_field(model, field)
  30. # Simulate the effect of a one-off default.
  31. # field.default may be unhashable, so a set isn't used for "in" check.
  32. if self.skip_default(field) and field.default not in (None, NOT_PROVIDED):
  33. effective_default = self.effective_default(field)
  34. self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
  35. 'table': self.quote_name(model._meta.db_table),
  36. 'column': self.quote_name(field.column),
  37. }, [effective_default])
  38. def _field_should_be_indexed(self, model, field):
  39. create_index = super()._field_should_be_indexed(model, field)
  40. storage = self.connection.introspection.get_storage_engine(
  41. self.connection.cursor(), model._meta.db_table
  42. )
  43. # No need to create an index for ForeignKey fields except if
  44. # db_constraint=False because the index from that constraint won't be
  45. # created.
  46. if (storage == "InnoDB" and
  47. create_index and
  48. field.get_internal_type() == 'ForeignKey' and
  49. field.db_constraint):
  50. return False
  51. return not self._is_limited_data_type(field) and create_index
  52. def _delete_composed_index(self, model, fields, *args):
  53. """
  54. MySQL can remove an implicit FK index on a field when that field is
  55. covered by another index like a unique_together. "covered" here means
  56. that the more complex index starts like the simpler one.
  57. http://bugs.mysql.com/bug.php?id=37910 / Django ticket #24757
  58. We check here before removing the [unique|index]_together if we have to
  59. recreate a FK index.
  60. """
  61. first_field = model._meta.get_field(fields[0])
  62. if first_field.get_internal_type() == 'ForeignKey':
  63. constraint_names = self._constraint_names(model, [first_field.column], index=True)
  64. if not constraint_names:
  65. self.execute(self._create_index_sql(model, [first_field], suffix=""))
  66. return super()._delete_composed_index(model, fields, *args)
  67. def _set_field_new_type_null_status(self, field, new_type):
  68. """
  69. Keep the null property of the old field. If it has changed, it will be
  70. handled separately.
  71. """
  72. if field.null:
  73. new_type += " NULL"
  74. else:
  75. new_type += " NOT NULL"
  76. return new_type
  77. def _alter_column_type_sql(self, model, old_field, new_field, new_type):
  78. new_type = self._set_field_new_type_null_status(old_field, new_type)
  79. return super()._alter_column_type_sql(model, old_field, new_field, new_type)
  80. def _rename_field_sql(self, table, old_field, new_field, new_type):
  81. new_type = self._set_field_new_type_null_status(old_field, new_type)
  82. return super()._rename_field_sql(table, old_field, new_field, new_type)