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.
 
 
 
 

107 lines
4.7 KiB

  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. sql_rename_column = "ALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)s"
  9. sql_delete_unique = "ALTER TABLE %(table)s DROP INDEX %(name)s"
  10. sql_create_fk = (
  11. "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY "
  12. "(%(column)s) REFERENCES %(to_table)s (%(to_column)s)"
  13. )
  14. sql_delete_fk = "ALTER TABLE %(table)s DROP FOREIGN KEY %(name)s"
  15. sql_delete_index = "DROP INDEX %(name)s ON %(table)s"
  16. alter_string_set_null = 'MODIFY %(column)s %(type)s NULL;'
  17. alter_string_drop_null = 'MODIFY %(column)s %(type)s NOT NULL;'
  18. sql_create_pk = "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s PRIMARY KEY (%(columns)s)"
  19. sql_delete_pk = "ALTER TABLE %(table)s DROP PRIMARY KEY"
  20. def quote_value(self, value):
  21. # Inner import to allow module to fail to load gracefully
  22. import MySQLdb.converters
  23. return MySQLdb.escape(value, MySQLdb.converters.conversions)
  24. def skip_default(self, field):
  25. """
  26. MySQL doesn't accept default values for TEXT and BLOB types, and
  27. implicitly treats these columns as nullable.
  28. """
  29. db_type = field.db_type(self.connection)
  30. return (
  31. db_type is not None and
  32. db_type.lower() in {
  33. 'tinyblob', 'blob', 'mediumblob', 'longblob',
  34. 'tinytext', 'text', 'mediumtext', 'longtext',
  35. }
  36. )
  37. def add_field(self, model, field):
  38. super(DatabaseSchemaEditor, self).add_field(model, field)
  39. # Simulate the effect of a one-off default.
  40. # field.default may be unhashable, so a set isn't used for "in" check.
  41. if self.skip_default(field) and field.default not in (None, NOT_PROVIDED):
  42. effective_default = self.effective_default(field)
  43. self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
  44. 'table': self.quote_name(model._meta.db_table),
  45. 'column': self.quote_name(field.column),
  46. }, [effective_default])
  47. def _model_indexes_sql(self, model):
  48. storage = self.connection.introspection.get_storage_engine(
  49. self.connection.cursor(), model._meta.db_table
  50. )
  51. if storage == "InnoDB":
  52. for field in model._meta.local_fields:
  53. if field.db_index and not field.unique and field.get_internal_type() == "ForeignKey":
  54. # Temporary setting db_index to False (in memory) to disable
  55. # index creation for FKs (index automatically created by MySQL)
  56. field.db_index = False
  57. return super(DatabaseSchemaEditor, self)._model_indexes_sql(model)
  58. def _delete_composed_index(self, model, fields, *args):
  59. """
  60. MySQL can remove an implicit FK index on a field when that field is
  61. covered by another index like a unique_together. "covered" here means
  62. that the more complex index starts like the simpler one.
  63. http://bugs.mysql.com/bug.php?id=37910 / Django ticket #24757
  64. We check here before removing the [unique|index]_together if we have to
  65. recreate a FK index.
  66. """
  67. first_field = model._meta.get_field(fields[0])
  68. if first_field.get_internal_type() == 'ForeignKey':
  69. constraint_names = self._constraint_names(model, [first_field.column], index=True)
  70. if not constraint_names:
  71. self.execute(self._create_index_sql(model, [first_field], suffix=""))
  72. return super(DatabaseSchemaEditor, self)._delete_composed_index(model, fields, *args)
  73. def _set_field_new_type_null_status(self, field, new_type):
  74. """
  75. Keep the null property of the old field. If it has changed, it will be
  76. handled separately.
  77. """
  78. if field.null:
  79. new_type += " NULL"
  80. else:
  81. new_type += " NOT NULL"
  82. return new_type
  83. def _alter_column_type_sql(self, table, old_field, new_field, new_type):
  84. new_type = self._set_field_new_type_null_status(old_field, new_type)
  85. return super(DatabaseSchemaEditor, self)._alter_column_type_sql(table, old_field, new_field, new_type)
  86. def _rename_field_sql(self, table, old_field, new_field, new_type):
  87. new_type = self._set_field_new_type_null_status(old_field, new_type)
  88. return super(DatabaseSchemaEditor, self)._rename_field_sql(table, old_field, new_field, new_type)