2014-05-17 00:11:29 +08:00
|
|
|
import codecs
|
2015-02-12 00:15:41 +08:00
|
|
|
import contextlib
|
2014-05-20 23:25:59 +08:00
|
|
|
import copy
|
2014-05-08 04:46:23 +08:00
|
|
|
from decimal import Decimal
|
2015-01-13 04:20:40 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps.registry import Apps
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
2016-05-25 03:25:05 +08:00
|
|
|
from django.db.backends.ddl_references import Statement
|
2015-01-13 04:20:40 +08:00
|
|
|
|
2012-08-18 21:16:52 +08:00
|
|
|
|
|
|
|
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
|
|
|
|
|
|
|
|
sql_delete_table = "DROP TABLE %(table)s"
|
2015-01-11 00:24:16 +08:00
|
|
|
sql_create_fk = None
|
|
|
|
sql_create_inline_fk = "REFERENCES %(to_table)s (%(to_column)s) DEFERRABLE INITIALLY DEFERRED"
|
2015-03-22 00:38:23 +08:00
|
|
|
sql_create_unique = "CREATE UNIQUE INDEX %(name)s ON %(table)s (%(columns)s)"
|
|
|
|
sql_delete_unique = "DROP INDEX %(name)s"
|
2012-09-08 00:51:11 +08:00
|
|
|
|
2015-02-18 05:59:25 +08:00
|
|
|
def __enter__(self):
|
2015-01-11 00:24:16 +08:00
|
|
|
# Some SQLite schema alterations need foreign key constraints to be
|
|
|
|
# disabled. Enforce it here for the duration of the transaction.
|
|
|
|
self.connection.disable_constraint_checking()
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().__enter__()
|
2015-02-18 05:59:25 +08:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__exit__(exc_type, exc_value, traceback)
|
2015-01-11 00:24:16 +08:00
|
|
|
self.connection.enable_constraint_checking()
|
2015-02-18 05:59:25 +08:00
|
|
|
|
2014-02-09 20:41:55 +08:00
|
|
|
def quote_value(self, value):
|
2015-05-04 01:41:34 +08:00
|
|
|
# The backend "mostly works" without this function and there are use
|
|
|
|
# cases for compiling Python without the sqlite3 libraries (e.g.
|
|
|
|
# security hardening).
|
2014-02-09 20:41:55 +08:00
|
|
|
try:
|
2016-05-17 07:43:07 +08:00
|
|
|
import sqlite3
|
2015-06-06 03:47:17 +08:00
|
|
|
value = sqlite3.adapt(value)
|
2016-05-17 07:43:07 +08:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2015-06-06 03:47:17 +08:00
|
|
|
except sqlite3.ProgrammingError:
|
2014-02-09 20:41:55 +08:00
|
|
|
pass
|
|
|
|
# Manual emulation of SQLite parameter quoting
|
|
|
|
if isinstance(value, type(True)):
|
|
|
|
return str(int(value))
|
2016-12-29 23:27:49 +08:00
|
|
|
elif isinstance(value, (Decimal, float, int)):
|
2014-05-08 04:46:23 +08:00
|
|
|
return str(value)
|
2016-12-29 23:27:49 +08:00
|
|
|
elif isinstance(value, str):
|
|
|
|
return "'%s'" % value.replace("\'", "\'\'")
|
2014-02-09 20:41:55 +08:00
|
|
|
elif value is None:
|
|
|
|
return "NULL"
|
2016-12-29 23:27:49 +08:00
|
|
|
elif isinstance(value, (bytes, bytearray, memoryview)):
|
2014-05-17 00:11:29 +08:00
|
|
|
# Bytes are only allowed for BLOB fields, encoded as string
|
|
|
|
# literals containing hexadecimal data and preceded by a single "X"
|
|
|
|
# character:
|
|
|
|
# value = b'\x01\x02' => value_hex = b'0102' => return X'0102'
|
|
|
|
value = bytes(value)
|
|
|
|
hex_encoder = codecs.getencoder('hex_codec')
|
|
|
|
value_hex, _length = hex_encoder(value)
|
|
|
|
# Use 'ascii' encoding for b'01' => '01', no need to use force_text here.
|
|
|
|
return "X'%s'" % value_hex.decode('ascii')
|
2014-02-09 20:41:55 +08:00
|
|
|
else:
|
2014-05-08 04:46:23 +08:00
|
|
|
raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value)))
|
2014-02-09 20:41:55 +08:00
|
|
|
|
2016-07-18 20:47:30 +08:00
|
|
|
def _remake_table(self, model, create_field=None, delete_field=None, alter_field=None):
|
2012-09-18 17:37:30 +08:00
|
|
|
"""
|
|
|
|
Shortcut to transform a model from old_model into new_model
|
2015-02-12 00:15:41 +08:00
|
|
|
|
|
|
|
The essential steps are:
|
|
|
|
1. rename the model's existing table, e.g. "app_model" to "app_model__old"
|
|
|
|
2. create a table with the updated definition called "app_model"
|
|
|
|
3. copy the data from the old renamed table to the new table
|
|
|
|
4. delete the "app_model__old" table
|
2012-09-18 17:37:30 +08:00
|
|
|
"""
|
2016-03-29 16:58:04 +08:00
|
|
|
# Self-referential fields must be recreated rather than copied from
|
|
|
|
# the old model to ensure their remote_field.field_name doesn't refer
|
|
|
|
# to an altered field.
|
|
|
|
def is_self_referential(f):
|
|
|
|
return f.is_relation and f.remote_field.model is model
|
2012-09-08 00:51:11 +08:00
|
|
|
# Work out the new fields dict / mapping
|
2016-03-29 16:58:04 +08:00
|
|
|
body = {
|
|
|
|
f.name: f.clone() if is_self_referential(f) else f
|
|
|
|
for f in model._meta.local_concrete_fields
|
|
|
|
}
|
2014-05-17 00:11:29 +08:00
|
|
|
# Since mapping might mix column names and default values,
|
|
|
|
# its values must be already quoted.
|
2015-07-16 02:38:10 +08:00
|
|
|
mapping = {f.column: self.quote_name(f.column) for f in model._meta.local_concrete_fields}
|
2014-07-21 18:36:34 +08:00
|
|
|
# This maps field names (not columns) for things like unique_together
|
|
|
|
rename_mapping = {}
|
2012-09-08 00:51:11 +08:00
|
|
|
# If any of the new or altered fields is introducing a new PK,
|
|
|
|
# remove the old one
|
|
|
|
restore_pk_field = None
|
2016-07-18 20:47:30 +08:00
|
|
|
if getattr(create_field, 'primary_key', False) or (
|
|
|
|
alter_field and getattr(alter_field[1], 'primary_key', False)):
|
2012-09-08 00:51:11 +08:00
|
|
|
for name, field in list(body.items()):
|
|
|
|
if field.primary_key:
|
|
|
|
field.primary_key = False
|
|
|
|
restore_pk_field = field
|
|
|
|
if field.auto_created:
|
|
|
|
del body[name]
|
|
|
|
del mapping[field.column]
|
|
|
|
# Add in any created fields
|
2016-07-18 20:47:30 +08:00
|
|
|
if create_field:
|
|
|
|
body[create_field.name] = create_field
|
2014-12-15 22:14:39 +08:00
|
|
|
# Choose a default and insert it into the copy map
|
2016-07-18 20:47:30 +08:00
|
|
|
if not create_field.many_to_many and create_field.concrete:
|
|
|
|
mapping[create_field.column] = self.quote_value(
|
|
|
|
self.effective_default(create_field)
|
2014-01-20 01:10:24 +08:00
|
|
|
)
|
2012-09-08 00:51:11 +08:00
|
|
|
# Add in any altered fields
|
2016-07-18 20:47:30 +08:00
|
|
|
if alter_field:
|
|
|
|
old_field, new_field = alter_field
|
2014-10-24 00:24:34 +08:00
|
|
|
body.pop(old_field.name, None)
|
|
|
|
mapping.pop(old_field.column, None)
|
2012-09-08 00:51:11 +08:00
|
|
|
body[new_field.name] = new_field
|
2014-10-07 07:53:21 +08:00
|
|
|
if old_field.null and not new_field.null:
|
|
|
|
case_sql = "coalesce(%(col)s, %(default)s)" % {
|
|
|
|
'col': self.quote_name(old_field.column),
|
|
|
|
'default': self.quote_value(self.effective_default(new_field))
|
|
|
|
}
|
|
|
|
mapping[new_field.column] = case_sql
|
|
|
|
else:
|
|
|
|
mapping[new_field.column] = self.quote_name(old_field.column)
|
2014-07-21 18:36:34 +08:00
|
|
|
rename_mapping[old_field.name] = new_field.name
|
2012-09-08 00:51:11 +08:00
|
|
|
# Remove any deleted fields
|
2016-07-18 20:47:30 +08:00
|
|
|
if delete_field:
|
|
|
|
del body[delete_field.name]
|
|
|
|
del mapping[delete_field.column]
|
2014-03-18 11:54:35 +08:00
|
|
|
# Remove any implicit M2M tables
|
2016-07-18 20:47:30 +08:00
|
|
|
if delete_field.many_to_many and delete_field.remote_field.through._meta.auto_created:
|
|
|
|
return self.delete_model(delete_field.remote_field.through)
|
2013-12-24 19:25:17 +08:00
|
|
|
# Work inside a new app registry
|
|
|
|
apps = Apps()
|
2014-05-20 23:25:59 +08:00
|
|
|
|
2015-02-12 00:15:41 +08:00
|
|
|
# Provide isolated instances of the fields to the new model body so
|
|
|
|
# that the existing model's internals aren't interfered with when
|
|
|
|
# the dummy model is constructed.
|
2014-05-20 23:25:59 +08:00
|
|
|
body = copy.deepcopy(body)
|
|
|
|
|
2014-07-21 18:36:34 +08:00
|
|
|
# Work out the new value of unique_together, taking renames into
|
|
|
|
# account
|
2016-07-06 16:03:47 +08:00
|
|
|
unique_together = [
|
|
|
|
[rename_mapping.get(n, n) for n in unique]
|
|
|
|
for unique in model._meta.unique_together
|
|
|
|
]
|
2014-07-21 18:36:34 +08:00
|
|
|
|
2014-12-01 01:11:52 +08:00
|
|
|
# Work out the new value for index_together, taking renames into
|
|
|
|
# account
|
2016-07-06 16:03:47 +08:00
|
|
|
index_together = [
|
|
|
|
[rename_mapping.get(n, n) for n in index]
|
|
|
|
for index in model._meta.index_together
|
|
|
|
]
|
2014-12-01 01:11:52 +08:00
|
|
|
|
2016-06-20 23:50:05 +08:00
|
|
|
indexes = model._meta.indexes
|
|
|
|
if delete_field:
|
|
|
|
indexes = [
|
|
|
|
index for index in indexes
|
|
|
|
if delete_field.name not in index.fields
|
|
|
|
]
|
|
|
|
|
2012-09-08 00:51:11 +08:00
|
|
|
# Construct a new model for the new state
|
|
|
|
meta_contents = {
|
|
|
|
'app_label': model._meta.app_label,
|
2015-02-12 00:15:41 +08:00
|
|
|
'db_table': model._meta.db_table,
|
2016-07-06 16:03:47 +08:00
|
|
|
'unique_together': unique_together,
|
|
|
|
'index_together': index_together,
|
2016-06-20 23:50:05 +08:00
|
|
|
'indexes': indexes,
|
2013-12-24 19:25:17 +08:00
|
|
|
'apps': apps,
|
2012-09-08 00:51:11 +08:00
|
|
|
}
|
2017-06-02 07:08:59 +08:00
|
|
|
meta = type("Meta", (), meta_contents)
|
2012-09-08 00:51:11 +08:00
|
|
|
body['Meta'] = meta
|
2012-09-22 07:47:04 +08:00
|
|
|
body['__module__'] = model.__module__
|
2014-05-20 23:25:59 +08:00
|
|
|
|
2012-09-22 07:47:04 +08:00
|
|
|
temp_model = type(model._meta.object_name, model.__bases__, body)
|
2015-02-12 00:15:41 +08:00
|
|
|
|
|
|
|
# We need to modify model._meta.db_table, but everything explodes
|
|
|
|
# if the change isn't reversed before the end of this method. This
|
|
|
|
# context manager helps us avoid that situation.
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def altered_table_name(model, temporary_table_name):
|
|
|
|
original_table_name = model._meta.db_table
|
|
|
|
model._meta.db_table = temporary_table_name
|
|
|
|
yield
|
|
|
|
model._meta.db_table = original_table_name
|
|
|
|
|
2015-04-02 00:57:53 +08:00
|
|
|
with altered_table_name(model, model._meta.db_table + "__old"):
|
|
|
|
# Rename the old table to make way for the new
|
2015-02-12 00:15:41 +08:00
|
|
|
self.alter_db_table(model, temp_model._meta.db_table, model._meta.db_table)
|
|
|
|
|
2016-05-25 03:25:05 +08:00
|
|
|
# Create a new table with the updated schema.
|
2015-04-02 00:57:53 +08:00
|
|
|
self.create_model(temp_model)
|
|
|
|
|
|
|
|
# Copy data from the old table into the new table
|
|
|
|
field_maps = list(mapping.items())
|
|
|
|
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
|
|
|
|
self.quote_name(temp_model._meta.db_table),
|
|
|
|
', '.join(self.quote_name(x) for x, y in field_maps),
|
|
|
|
', '.join(y for x, y in field_maps),
|
|
|
|
self.quote_name(model._meta.db_table),
|
|
|
|
))
|
|
|
|
|
|
|
|
# Delete the old table
|
2015-02-12 00:15:41 +08:00
|
|
|
self.delete_model(model, handle_autom2m=False)
|
|
|
|
|
2012-09-08 00:51:11 +08:00
|
|
|
# Run deferred SQL on correct table
|
|
|
|
for sql in self.deferred_sql:
|
2015-02-12 00:15:41 +08:00
|
|
|
self.execute(sql)
|
2012-09-08 00:51:11 +08:00
|
|
|
self.deferred_sql = []
|
|
|
|
# Fix any PK-removed field
|
|
|
|
if restore_pk_field:
|
|
|
|
restore_pk_field.primary_key = True
|
|
|
|
|
2014-07-16 17:06:33 +08:00
|
|
|
def delete_model(self, model, handle_autom2m=True):
|
|
|
|
if handle_autom2m:
|
2017-01-21 21:13:44 +08:00
|
|
|
super().delete_model(model)
|
2014-07-16 17:06:33 +08:00
|
|
|
else:
|
|
|
|
# Delete the table (and only that)
|
|
|
|
self.execute(self.sql_delete_table % {
|
|
|
|
"table": self.quote_name(model._meta.db_table),
|
|
|
|
})
|
2016-05-25 03:38:01 +08:00
|
|
|
# Remove all deferred statements referencing the deleted table.
|
|
|
|
for sql in list(self.deferred_sql):
|
|
|
|
if isinstance(sql, Statement) and sql.references_table(model._meta.db_table):
|
|
|
|
self.deferred_sql.remove(sql)
|
2014-07-16 17:06:33 +08:00
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def add_field(self, model, field):
|
2012-09-08 00:51:11 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Create a field on a model. Usually involves adding a column, but may
|
|
|
|
involve adding a table instead (for M2M fields).
|
2012-09-08 00:51:11 +08:00
|
|
|
"""
|
|
|
|
# Special-case implicit M2M tables
|
2015-02-26 22:19:17 +08:00
|
|
|
if field.many_to_many and field.remote_field.through._meta.auto_created:
|
|
|
|
return self.create_model(field.remote_field.through)
|
2016-07-18 20:47:30 +08:00
|
|
|
self._remake_table(model, create_field=field)
|
2012-09-08 00:51:11 +08:00
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def remove_field(self, model, field):
|
2012-09-08 00:51:11 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Remove a field from a model. Usually involves deleting a column,
|
2012-09-08 00:51:11 +08:00
|
|
|
but for M2Ms may involve deleting a table.
|
|
|
|
"""
|
2014-04-08 13:30:25 +08:00
|
|
|
# M2M fields are a special case
|
2015-01-09 06:51:00 +08:00
|
|
|
if field.many_to_many:
|
2014-04-08 13:30:25 +08:00
|
|
|
# For implicit M2M tables, delete the auto-created table
|
2015-02-26 22:19:17 +08:00
|
|
|
if field.remote_field.through._meta.auto_created:
|
|
|
|
self.delete_model(field.remote_field.through)
|
2014-04-08 13:30:25 +08:00
|
|
|
# For explicit "through" M2M fields, do nothing
|
2012-09-08 00:51:11 +08:00
|
|
|
# For everything else, remake.
|
2014-04-08 13:30:25 +08:00
|
|
|
else:
|
2014-08-10 19:31:06 +08:00
|
|
|
# It might not actually have a column behind it
|
|
|
|
if field.db_parameters(connection=self.connection)['type'] is None:
|
|
|
|
return
|
2016-07-18 20:47:30 +08:00
|
|
|
self._remake_table(model, delete_field=field)
|
2012-09-08 00:51:11 +08:00
|
|
|
|
2014-09-04 20:15:09 +08:00
|
|
|
def _alter_field(self, model, old_field, new_field, old_type, new_type,
|
|
|
|
old_db_params, new_db_params, strict=False):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Perform a "physical" (non-ManyToMany) field update."""
|
2012-09-08 00:51:11 +08:00
|
|
|
# Alter by remaking table
|
2016-07-18 20:47:30 +08:00
|
|
|
self._remake_table(model, alter_field=(old_field, new_field))
|
2015-01-11 00:24:16 +08:00
|
|
|
# Rebuild tables with FKs pointing to this field if the PK type changed.
|
|
|
|
if old_field.primary_key and new_field.primary_key and old_type != new_type:
|
|
|
|
for rel in new_field.model._meta.related_objects:
|
|
|
|
if not rel.many_to_many:
|
|
|
|
self._remake_table(rel.related_model)
|
2012-09-08 00:51:11 +08:00
|
|
|
|
2012-09-08 02:39:22 +08:00
|
|
|
def _alter_many_to_many(self, model, old_field, new_field, strict):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Alter M2Ms to repoint their to= endpoints."""
|
2015-02-26 22:19:17 +08:00
|
|
|
if old_field.remote_field.through._meta.db_table == new_field.remote_field.through._meta.db_table:
|
2014-05-20 23:25:59 +08:00
|
|
|
# The field name didn't change, but some options did; we have to propagate this altering.
|
|
|
|
self._remake_table(
|
2015-02-26 22:19:17 +08:00
|
|
|
old_field.remote_field.through,
|
2016-07-18 20:47:30 +08:00
|
|
|
alter_field=(
|
2014-05-20 23:25:59 +08:00
|
|
|
# We need the field that points to the target model, so we can tell alter_field to change it -
|
|
|
|
# this is m2m_reverse_field_name() (as opposed to m2m_field_name, which points to our model)
|
2015-02-26 22:19:17 +08:00
|
|
|
old_field.remote_field.through._meta.get_field(old_field.m2m_reverse_field_name()),
|
|
|
|
new_field.remote_field.through._meta.get_field(new_field.m2m_reverse_field_name()),
|
2016-07-18 20:47:30 +08:00
|
|
|
),
|
2014-05-20 23:25:59 +08:00
|
|
|
)
|
2014-03-20 12:08:28 +08:00
|
|
|
return
|
|
|
|
|
2012-09-08 02:39:22 +08:00
|
|
|
# Make a new through table
|
2015-02-26 22:19:17 +08:00
|
|
|
self.create_model(new_field.remote_field.through)
|
2012-09-08 02:39:22 +08:00
|
|
|
# Copy the data across
|
2013-09-07 04:27:51 +08:00
|
|
|
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
|
2015-02-26 22:19:17 +08:00
|
|
|
self.quote_name(new_field.remote_field.through._meta.db_table),
|
2012-09-08 02:39:22 +08:00
|
|
|
', '.join([
|
|
|
|
"id",
|
|
|
|
new_field.m2m_column_name(),
|
|
|
|
new_field.m2m_reverse_name(),
|
|
|
|
]),
|
|
|
|
', '.join([
|
|
|
|
"id",
|
|
|
|
old_field.m2m_column_name(),
|
|
|
|
old_field.m2m_reverse_name(),
|
|
|
|
]),
|
2015-02-26 22:19:17 +08:00
|
|
|
self.quote_name(old_field.remote_field.through._meta.db_table),
|
2012-09-08 02:39:22 +08:00
|
|
|
))
|
|
|
|
# Delete the old through table
|
2015-02-26 22:19:17 +08:00
|
|
|
self.delete_model(old_field.remote_field.through)
|