Development of an internal social media platform with personalised dashboards for students
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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. def quote_value(self, value):
  17. self.connection.ensure_connection()
  18. quoted = self.connection.connection.escape(value, self.connection.connection.encoders)
  19. if isinstance(value, str):
  20. quoted = quoted.decode()
  21. return quoted
  22. def _is_limited_data_type(self, field):
  23. db_type = field.db_type(self.connection)
  24. return db_type is not None and db_type.lower() in self.connection._limited_data_types
  25. def skip_default(self, field):
  26. return self._is_limited_data_type(field)
  27. def add_field(self, model, field):
  28. super().add_field(model, field)
  29. # Simulate the effect of a one-off default.
  30. # field.default may be unhashable, so a set isn't used for "in" check.
  31. if self.skip_default(field) and field.default not in (None, NOT_PROVIDED):
  32. effective_default = self.effective_default(field)
  33. self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
  34. 'table': self.quote_name(model._meta.db_table),
  35. 'column': self.quote_name(field.column),
  36. }, [effective_default])
  37. def _field_should_be_indexed(self, model, field):
  38. create_index = super()._field_should_be_indexed(model, field)
  39. storage = self.connection.introspection.get_storage_engine(
  40. self.connection.cursor(), model._meta.db_table
  41. )
  42. # No need to create an index for ForeignKey fields except if
  43. # db_constraint=False because the index from that constraint won't be
  44. # created.
  45. if (storage == "InnoDB" and
  46. create_index and
  47. field.get_internal_type() == 'ForeignKey' and
  48. field.db_constraint):
  49. return False
  50. return not self._is_limited_data_type(field) and create_index
  51. def _delete_composed_index(self, model, fields, *args):
  52. """
  53. MySQL can remove an implicit FK index on a field when that field is
  54. covered by another index like a unique_together. "covered" here means
  55. that the more complex index starts like the simpler one.
  56. http://bugs.mysql.com/bug.php?id=37910 / Django ticket #24757
  57. We check here before removing the [unique|index]_together if we have to
  58. recreate a FK index.
  59. """
  60. first_field = model._meta.get_field(fields[0])
  61. if first_field.get_internal_type() == 'ForeignKey':
  62. constraint_names = self._constraint_names(model, [first_field.column], index=True)
  63. if not constraint_names:
  64. self.execute(self._create_index_sql(model, [first_field], suffix=""))
  65. return super()._delete_composed_index(model, fields, *args)
  66. def _set_field_new_type_null_status(self, field, new_type):
  67. """
  68. Keep the null property of the old field. If it has changed, it will be
  69. handled separately.
  70. """
  71. if field.null:
  72. new_type += " NULL"
  73. else:
  74. new_type += " NOT NULL"
  75. return new_type
  76. def _alter_column_type_sql(self, model, old_field, new_field, new_type):
  77. new_type = self._set_field_new_type_null_status(old_field, new_type)
  78. return super()._alter_column_type_sql(model, old_field, new_field, new_type)
  79. def _rename_field_sql(self, table, old_field, new_field, new_type):
  80. new_type = self._set_field_new_type_null_status(old_field, new_type)
  81. return super()._rename_field_sql(table, old_field, new_field, new_type)