2013-08-14 03:54:57 +08:00
|
|
|
import copy
|
2014-02-09 20:41:55 +08:00
|
|
|
import datetime
|
2015-06-23 11:53:49 +08:00
|
|
|
import re
|
2013-09-07 12:56:40 +08:00
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
2013-08-14 03:54:57 +08:00
|
|
|
from django.db.utils import DatabaseError
|
2012-09-08 03:56:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
|
2013-09-07 12:56:40 +08:00
|
|
|
|
2013-08-13 03:05:20 +08:00
|
|
|
sql_create_column = "ALTER TABLE %(table)s ADD %(column)s %(definition)s"
|
|
|
|
sql_alter_column_type = "MODIFY %(column)s %(type)s"
|
|
|
|
sql_alter_column_null = "MODIFY %(column)s NULL"
|
|
|
|
sql_alter_column_not_null = "MODIFY %(column)s NOT NULL"
|
|
|
|
sql_alter_column_default = "MODIFY %(column)s DEFAULT %(default)s"
|
|
|
|
sql_alter_column_no_default = "MODIFY %(column)s DEFAULT NULL"
|
|
|
|
sql_delete_column = "ALTER TABLE %(table)s DROP COLUMN %(column)s"
|
|
|
|
sql_delete_table = "DROP TABLE %(table)s CASCADE CONSTRAINTS"
|
2013-09-07 12:56:40 +08:00
|
|
|
|
2014-02-09 20:41:55 +08:00
|
|
|
def quote_value(self, value):
|
|
|
|
if isinstance(value, (datetime.date, datetime.time, datetime.datetime)):
|
|
|
|
return "'%s'" % value
|
2016-12-29 23:27:49 +08:00
|
|
|
elif isinstance(value, str):
|
|
|
|
return "'%s'" % value.replace("\'", "\'\'")
|
|
|
|
elif isinstance(value, (bytes, bytearray, memoryview)):
|
2017-11-23 03:25:32 +08:00
|
|
|
return "'%s'" % value.hex()
|
2014-02-09 20:41:55 +08:00
|
|
|
elif isinstance(value, bool):
|
|
|
|
return "1" if value else "0"
|
|
|
|
else:
|
|
|
|
return str(value)
|
|
|
|
|
2017-06-01 17:49:57 +08:00
|
|
|
def remove_field(self, model, field):
|
|
|
|
# If the column is an identity column, drop the identity before
|
|
|
|
# removing the field.
|
|
|
|
if self._is_identity_column(model._meta.db_table, field.column):
|
|
|
|
self._drop_identity(model._meta.db_table, field.column)
|
|
|
|
super().remove_field(model, field)
|
|
|
|
|
2013-08-14 03:54:57 +08:00
|
|
|
def delete_model(self, model):
|
|
|
|
# Run superclass action
|
2017-01-21 21:13:44 +08:00
|
|
|
super().delete_model(model)
|
2017-06-01 17:49:57 +08:00
|
|
|
# Clean up manually created sequence.
|
2013-08-14 03:54:57 +08:00
|
|
|
self.execute("""
|
|
|
|
DECLARE
|
|
|
|
i INTEGER;
|
|
|
|
BEGIN
|
2016-10-13 06:58:33 +08:00
|
|
|
SELECT COUNT(1) INTO i FROM USER_SEQUENCES
|
|
|
|
WHERE SEQUENCE_NAME = '%(sq_name)s';
|
2013-08-14 03:54:57 +08:00
|
|
|
IF i = 1 THEN
|
|
|
|
EXECUTE IMMEDIATE 'DROP SEQUENCE "%(sq_name)s"';
|
|
|
|
END IF;
|
|
|
|
END;
|
2017-06-01 17:49:57 +08:00
|
|
|
/""" % {'sq_name': self.connection.ops._get_no_autofield_sequence_name(model._meta.db_table)})
|
2013-08-14 03:54:57 +08:00
|
|
|
|
|
|
|
def alter_field(self, model, old_field, new_field, strict=False):
|
|
|
|
try:
|
2017-01-21 21:13:44 +08:00
|
|
|
super().alter_field(model, old_field, new_field, strict)
|
2013-08-14 03:54:57 +08:00
|
|
|
except DatabaseError as e:
|
|
|
|
description = str(e)
|
2015-06-23 11:53:49 +08:00
|
|
|
# If we're changing type to an unsupported type we need a
|
2013-08-14 03:54:57 +08:00
|
|
|
# SQLite-ish workaround
|
|
|
|
if 'ORA-22858' in description or 'ORA-22859' in description:
|
2015-06-23 11:53:49 +08:00
|
|
|
self._alter_field_type_workaround(model, old_field, new_field)
|
2017-06-01 17:49:57 +08:00
|
|
|
# If an identity column is changing to a non-numeric type, drop the
|
|
|
|
# identity first.
|
|
|
|
elif 'ORA-30675' in description:
|
|
|
|
self._drop_identity(model._meta.db_table, old_field.column)
|
|
|
|
self.alter_field(model, old_field, new_field, strict)
|
|
|
|
# If a primary key column is changing to an identity column, drop
|
|
|
|
# the primary key first.
|
|
|
|
elif 'ORA-30673' in description and old_field.primary_key:
|
|
|
|
self._delete_primary_key(model, strict=True)
|
|
|
|
self._alter_field_type_workaround(model, old_field, new_field)
|
2013-08-14 03:54:57 +08:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2015-06-23 11:53:49 +08:00
|
|
|
def _alter_field_type_workaround(self, model, old_field, new_field):
|
2013-08-14 03:54:57 +08:00
|
|
|
"""
|
2015-06-23 11:53:49 +08:00
|
|
|
Oracle refuses to change from some type to other type.
|
2013-08-14 03:54:57 +08:00
|
|
|
What we need to do instead is:
|
2017-06-01 17:49:57 +08:00
|
|
|
- Add a nullable version of the desired field with a temporary name. If
|
|
|
|
the new column is an auto field, then the temporary column can't be
|
|
|
|
nullable.
|
2013-08-14 03:54:57 +08:00
|
|
|
- Update the table to transfer values from old to new
|
|
|
|
- Drop old column
|
2015-06-23 11:53:49 +08:00
|
|
|
- Rename the new column and possibly drop the nullable property
|
2013-08-14 03:54:57 +08:00
|
|
|
"""
|
|
|
|
# Make a new field that's like the new one but with a temporary
|
|
|
|
# column name.
|
|
|
|
new_temp_field = copy.deepcopy(new_field)
|
2017-06-01 17:49:57 +08:00
|
|
|
new_temp_field.null = (new_field.get_internal_type() not in ('AutoField', 'BigAutoField'))
|
2013-08-14 03:54:57 +08:00
|
|
|
new_temp_field.column = self._generate_temp_name(new_field.column)
|
|
|
|
# Add it
|
|
|
|
self.add_field(model, new_temp_field)
|
2015-06-23 11:53:49 +08:00
|
|
|
# Explicit data type conversion
|
2017-04-21 22:02:31 +08:00
|
|
|
# https://docs.oracle.com/database/121/SQLRF/sql_elements002.htm#SQLRF51054
|
2015-06-23 11:53:49 +08:00
|
|
|
new_value = self.quote_name(old_field.column)
|
|
|
|
old_type = old_field.db_type(self.connection)
|
|
|
|
if re.match('^N?CLOB', old_type):
|
|
|
|
new_value = "TO_CHAR(%s)" % new_value
|
|
|
|
old_type = 'VARCHAR2'
|
|
|
|
if re.match('^N?VARCHAR2', old_type):
|
|
|
|
new_internal_type = new_field.get_internal_type()
|
|
|
|
if new_internal_type == 'DateField':
|
|
|
|
new_value = "TO_DATE(%s, 'YYYY-MM-DD')" % new_value
|
|
|
|
elif new_internal_type == 'DateTimeField':
|
|
|
|
new_value = "TO_TIMESTAMP(%s, 'YYYY-MM-DD HH24:MI:SS.FF')" % new_value
|
|
|
|
elif new_internal_type == 'TimeField':
|
|
|
|
# TimeField are stored as TIMESTAMP with a 1900-01-01 date part.
|
|
|
|
new_value = "TO_TIMESTAMP(CONCAT('1900-01-01 ', %s), 'YYYY-MM-DD HH24:MI:SS.FF')" % new_value
|
2013-08-14 03:54:57 +08:00
|
|
|
# Transfer values across
|
|
|
|
self.execute("UPDATE %s set %s=%s" % (
|
|
|
|
self.quote_name(model._meta.db_table),
|
|
|
|
self.quote_name(new_temp_field.column),
|
2015-06-23 11:53:49 +08:00
|
|
|
new_value,
|
2013-08-14 03:54:57 +08:00
|
|
|
))
|
|
|
|
# Drop the old field
|
|
|
|
self.remove_field(model, old_field)
|
2015-06-23 11:53:49 +08:00
|
|
|
# Rename and possibly make the new field NOT NULL
|
2017-01-21 21:13:44 +08:00
|
|
|
super().alter_field(model, new_temp_field, new_field)
|
2013-08-14 03:54:57 +08:00
|
|
|
|
|
|
|
def normalize_name(self, name):
|
|
|
|
"""
|
2014-09-04 20:15:09 +08:00
|
|
|
Get the properly shortened and uppercased identifier as returned by
|
2017-01-25 07:04:12 +08:00
|
|
|
quote_name() but without the quotes.
|
2013-08-14 03:54:57 +08:00
|
|
|
"""
|
|
|
|
nn = self.quote_name(name)
|
|
|
|
if nn[0] == '"' and nn[-1] == '"':
|
|
|
|
nn = nn[1:-1]
|
|
|
|
return nn
|
|
|
|
|
|
|
|
def _generate_temp_name(self, for_name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Generate temporary names for workarounds that need temp columns."""
|
2013-08-14 03:54:57 +08:00
|
|
|
suffix = hex(hash(for_name)).upper()[1:]
|
|
|
|
return self.normalize_name(for_name + "_" + suffix)
|
2013-08-23 19:07:55 +08:00
|
|
|
|
|
|
|
def prepare_default(self, value):
|
2014-02-09 20:41:55 +08:00
|
|
|
return self.quote_value(value)
|
2017-05-23 23:02:40 +08:00
|
|
|
|
|
|
|
def _field_should_be_indexed(self, model, field):
|
|
|
|
create_index = super()._field_should_be_indexed(model, field)
|
|
|
|
db_type = field.db_type(self.connection)
|
|
|
|
if db_type is not None and db_type.lower() in self.connection._limited_data_types:
|
|
|
|
return False
|
|
|
|
return create_index
|
2017-06-01 17:49:57 +08:00
|
|
|
|
|
|
|
def _is_identity_column(self, table_name, column_name):
|
|
|
|
with self.connection.cursor() as cursor:
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT
|
|
|
|
CASE WHEN identity_column = 'YES' THEN 1 ELSE 0 END
|
|
|
|
FROM user_tab_cols
|
|
|
|
WHERE table_name = %s AND
|
|
|
|
column_name = %s
|
|
|
|
""", [self.normalize_name(table_name), self.normalize_name(column_name)])
|
|
|
|
row = cursor.fetchone()
|
|
|
|
return row[0] if row else False
|
|
|
|
|
|
|
|
def _drop_identity(self, table_name, column_name):
|
|
|
|
self.execute('ALTER TABLE %(table)s MODIFY %(column)s DROP IDENTITY' % {
|
|
|
|
'table': self.quote_name(table_name),
|
|
|
|
'column': self.quote_name(column_name),
|
|
|
|
})
|