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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import copy
  2. import datetime
  3. import re
  4. from django.db.backends.base.schema import BaseDatabaseSchemaEditor
  5. from django.db.utils import DatabaseError
  6. class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
  7. sql_create_column = "ALTER TABLE %(table)s ADD %(column)s %(definition)s"
  8. sql_alter_column_type = "MODIFY %(column)s %(type)s"
  9. sql_alter_column_null = "MODIFY %(column)s NULL"
  10. sql_alter_column_not_null = "MODIFY %(column)s NOT NULL"
  11. sql_alter_column_default = "MODIFY %(column)s DEFAULT %(default)s"
  12. sql_alter_column_no_default = "MODIFY %(column)s DEFAULT NULL"
  13. sql_delete_column = "ALTER TABLE %(table)s DROP COLUMN %(column)s"
  14. sql_delete_table = "DROP TABLE %(table)s CASCADE CONSTRAINTS"
  15. def quote_value(self, value):
  16. if isinstance(value, (datetime.date, datetime.time, datetime.datetime)):
  17. return "'%s'" % value
  18. elif isinstance(value, str):
  19. return "'%s'" % value.replace("\'", "\'\'")
  20. elif isinstance(value, (bytes, bytearray, memoryview)):
  21. return "'%s'" % value.hex()
  22. elif isinstance(value, bool):
  23. return "1" if value else "0"
  24. else:
  25. return str(value)
  26. def remove_field(self, model, field):
  27. # If the column is an identity column, drop the identity before
  28. # removing the field.
  29. if self._is_identity_column(model._meta.db_table, field.column):
  30. self._drop_identity(model._meta.db_table, field.column)
  31. super().remove_field(model, field)
  32. def delete_model(self, model):
  33. # Run superclass action
  34. super().delete_model(model)
  35. # Clean up manually created sequence.
  36. self.execute("""
  37. DECLARE
  38. i INTEGER;
  39. BEGIN
  40. SELECT COUNT(1) INTO i FROM USER_SEQUENCES
  41. WHERE SEQUENCE_NAME = '%(sq_name)s';
  42. IF i = 1 THEN
  43. EXECUTE IMMEDIATE 'DROP SEQUENCE "%(sq_name)s"';
  44. END IF;
  45. END;
  46. /""" % {'sq_name': self.connection.ops._get_no_autofield_sequence_name(model._meta.db_table)})
  47. def alter_field(self, model, old_field, new_field, strict=False):
  48. try:
  49. super().alter_field(model, old_field, new_field, strict)
  50. except DatabaseError as e:
  51. description = str(e)
  52. # If we're changing type to an unsupported type we need a
  53. # SQLite-ish workaround
  54. if 'ORA-22858' in description or 'ORA-22859' in description:
  55. self._alter_field_type_workaround(model, old_field, new_field)
  56. # If an identity column is changing to a non-numeric type, drop the
  57. # identity first.
  58. elif 'ORA-30675' in description:
  59. self._drop_identity(model._meta.db_table, old_field.column)
  60. self.alter_field(model, old_field, new_field, strict)
  61. # If a primary key column is changing to an identity column, drop
  62. # the primary key first.
  63. elif 'ORA-30673' in description and old_field.primary_key:
  64. self._delete_primary_key(model, strict=True)
  65. self._alter_field_type_workaround(model, old_field, new_field)
  66. else:
  67. raise
  68. def _alter_field_type_workaround(self, model, old_field, new_field):
  69. """
  70. Oracle refuses to change from some type to other type.
  71. What we need to do instead is:
  72. - Add a nullable version of the desired field with a temporary name. If
  73. the new column is an auto field, then the temporary column can't be
  74. nullable.
  75. - Update the table to transfer values from old to new
  76. - Drop old column
  77. - Rename the new column and possibly drop the nullable property
  78. """
  79. # Make a new field that's like the new one but with a temporary
  80. # column name.
  81. new_temp_field = copy.deepcopy(new_field)
  82. new_temp_field.null = (new_field.get_internal_type() not in ('AutoField', 'BigAutoField'))
  83. new_temp_field.column = self._generate_temp_name(new_field.column)
  84. # Add it
  85. self.add_field(model, new_temp_field)
  86. # Explicit data type conversion
  87. # https://docs.oracle.com/database/121/SQLRF/sql_elements002.htm#SQLRF51054
  88. new_value = self.quote_name(old_field.column)
  89. old_type = old_field.db_type(self.connection)
  90. if re.match('^N?CLOB', old_type):
  91. new_value = "TO_CHAR(%s)" % new_value
  92. old_type = 'VARCHAR2'
  93. if re.match('^N?VARCHAR2', old_type):
  94. new_internal_type = new_field.get_internal_type()
  95. if new_internal_type == 'DateField':
  96. new_value = "TO_DATE(%s, 'YYYY-MM-DD')" % new_value
  97. elif new_internal_type == 'DateTimeField':
  98. new_value = "TO_TIMESTAMP(%s, 'YYYY-MM-DD HH24:MI:SS.FF')" % new_value
  99. elif new_internal_type == 'TimeField':
  100. # TimeField are stored as TIMESTAMP with a 1900-01-01 date part.
  101. new_value = "TO_TIMESTAMP(CONCAT('1900-01-01 ', %s), 'YYYY-MM-DD HH24:MI:SS.FF')" % new_value
  102. # Transfer values across
  103. self.execute("UPDATE %s set %s=%s" % (
  104. self.quote_name(model._meta.db_table),
  105. self.quote_name(new_temp_field.column),
  106. new_value,
  107. ))
  108. # Drop the old field
  109. self.remove_field(model, old_field)
  110. # Rename and possibly make the new field NOT NULL
  111. super().alter_field(model, new_temp_field, new_field)
  112. def normalize_name(self, name):
  113. """
  114. Get the properly shortened and uppercased identifier as returned by
  115. quote_name() but without the quotes.
  116. """
  117. nn = self.quote_name(name)
  118. if nn[0] == '"' and nn[-1] == '"':
  119. nn = nn[1:-1]
  120. return nn
  121. def _generate_temp_name(self, for_name):
  122. """Generate temporary names for workarounds that need temp columns."""
  123. suffix = hex(hash(for_name)).upper()[1:]
  124. return self.normalize_name(for_name + "_" + suffix)
  125. def prepare_default(self, value):
  126. return self.quote_value(value)
  127. def _field_should_be_indexed(self, model, field):
  128. create_index = super()._field_should_be_indexed(model, field)
  129. db_type = field.db_type(self.connection)
  130. if db_type is not None and db_type.lower() in self.connection._limited_data_types:
  131. return False
  132. return create_index
  133. def _unique_should_be_added(self, old_field, new_field):
  134. return (
  135. super()._unique_should_be_added(old_field, new_field) and
  136. not self._field_became_primary_key(old_field, new_field)
  137. )
  138. def _is_identity_column(self, table_name, column_name):
  139. with self.connection.cursor() as cursor:
  140. cursor.execute("""
  141. SELECT
  142. CASE WHEN identity_column = 'YES' THEN 1 ELSE 0 END
  143. FROM user_tab_cols
  144. WHERE table_name = %s AND
  145. column_name = %s
  146. """, [self.normalize_name(table_name), self.normalize_name(column_name)])
  147. row = cursor.fetchone()
  148. return row[0] if row else False
  149. def _drop_identity(self, table_name, column_name):
  150. self.execute('ALTER TABLE %(table)s MODIFY %(column)s DROP IDENTITY' % {
  151. 'table': self.quote_name(table_name),
  152. 'column': self.quote_name(column_name),
  153. })