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
|
2018-12-28 03:21:59 +08:00
|
|
|
from django.db.models import UniqueConstraint
|
2017-11-27 11:39:43 +08:00
|
|
|
from django.db.transaction import atomic
|
|
|
|
from django.db.utils import NotSupportedError
|
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"
|
2019-01-01 22:39:58 +08:00
|
|
|
sql_create_fk = None
|
2015-01-11 00:24:16 +08:00
|
|
|
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
|
2018-12-08 08:55:47 +08:00
|
|
|
# disabled. Enforce it here for the duration of the schema edition.
|
|
|
|
if not self.connection.disable_constraint_checking():
|
|
|
|
raise NotSupportedError(
|
|
|
|
'SQLite schema editor cannot be used while foreign key '
|
|
|
|
'constraint checks are enabled. Make sure to disable them '
|
|
|
|
'before entering a transaction.atomic() context because '
|
2018-12-22 07:29:48 +08:00
|
|
|
'SQLite does not support disabling them in the middle of '
|
2018-12-08 08:55:47 +08:00
|
|
|
'a multi-statement transaction.'
|
|
|
|
)
|
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):
|
2018-12-11 15:55:54 +08:00
|
|
|
self.connection.check_constraints()
|
2018-12-22 08:47:47 +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
|
2017-11-28 23:41:36 +08:00
|
|
|
if isinstance(value, bool):
|
2014-02-09 20:41:55 +08:00
|
|
|
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"
|
2017-11-23 03:25:32 +08:00
|
|
|
# character.
|
|
|
|
return "X'%s'" % value.hex()
|
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
|
|
|
|
2017-12-05 09:35:33 +08:00
|
|
|
def _is_referenced_by_fk_constraint(self, table_name, column_name=None, ignore_self=False):
|
|
|
|
"""
|
|
|
|
Return whether or not the provided table name is referenced by another
|
|
|
|
one. If `column_name` is specified, only references pointing to that
|
|
|
|
column are considered. If `ignore_self` is True, self-referential
|
|
|
|
constraints are ignored.
|
|
|
|
"""
|
|
|
|
with self.connection.cursor() as cursor:
|
|
|
|
for other_table in self.connection.introspection.get_table_list(cursor):
|
|
|
|
if ignore_self and other_table.name == table_name:
|
|
|
|
continue
|
|
|
|
constraints = self.connection.introspection._get_foreign_key_constraints(cursor, other_table.name)
|
|
|
|
for constraint in constraints.values():
|
|
|
|
constraint_table, constraint_column = constraint['foreign_key']
|
|
|
|
if (constraint_table == table_name and
|
|
|
|
(column_name is None or constraint_column == column_name)):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2017-11-27 11:39:43 +08:00
|
|
|
def alter_db_table(self, model, old_db_table, new_db_table, disable_constraints=True):
|
2018-12-11 16:06:24 +08:00
|
|
|
if (not self.connection.features.supports_atomic_references_rename and
|
|
|
|
disable_constraints and self._is_referenced_by_fk_constraint(old_db_table)):
|
2017-11-27 11:39:43 +08:00
|
|
|
if self.connection.in_atomic_block:
|
|
|
|
raise NotSupportedError((
|
|
|
|
'Renaming the %r table while in a transaction is not '
|
2018-12-11 16:06:24 +08:00
|
|
|
'supported on SQLite < 3.26 because it would break referential '
|
2017-11-27 11:39:43 +08:00
|
|
|
'integrity. Try adding `atomic = False` to the Migration class.'
|
|
|
|
) % old_db_table)
|
|
|
|
self.connection.enable_constraint_checking()
|
|
|
|
super().alter_db_table(model, old_db_table, new_db_table)
|
|
|
|
self.connection.disable_constraint_checking()
|
|
|
|
else:
|
|
|
|
super().alter_db_table(model, old_db_table, new_db_table)
|
|
|
|
|
|
|
|
def alter_field(self, model, old_field, new_field, strict=False):
|
|
|
|
old_field_name = old_field.name
|
2017-12-05 09:35:33 +08:00
|
|
|
table_name = model._meta.db_table
|
|
|
|
_, old_column_name = old_field.get_attname_column()
|
2017-11-27 11:39:43 +08:00
|
|
|
if (new_field.name != old_field_name and
|
2018-12-11 16:06:24 +08:00
|
|
|
not self.connection.features.supports_atomic_references_rename and
|
2017-12-05 09:35:33 +08:00
|
|
|
self._is_referenced_by_fk_constraint(table_name, old_column_name, ignore_self=True)):
|
2017-11-27 11:39:43 +08:00
|
|
|
if self.connection.in_atomic_block:
|
|
|
|
raise NotSupportedError((
|
|
|
|
'Renaming the %r.%r column while in a transaction is not '
|
2018-12-11 16:06:24 +08:00
|
|
|
'supported on SQLite < 3.26 because it would break referential '
|
2017-11-27 11:39:43 +08:00
|
|
|
'integrity. Try adding `atomic = False` to the Migration class.'
|
|
|
|
) % (model._meta.db_table, old_field_name))
|
|
|
|
with atomic(self.connection.alias):
|
|
|
|
super().alter_field(model, old_field, new_field, strict=strict)
|
|
|
|
# Follow SQLite's documented procedure for performing changes
|
|
|
|
# that don't affect the on-disk content.
|
|
|
|
# https://sqlite.org/lang_altertable.html#otheralter
|
|
|
|
with self.connection.cursor() as cursor:
|
|
|
|
schema_version = cursor.execute('PRAGMA schema_version').fetchone()[0]
|
|
|
|
cursor.execute('PRAGMA writable_schema = 1')
|
|
|
|
references_template = ' REFERENCES "%s" ("%%s") ' % table_name
|
|
|
|
new_column_name = new_field.get_attname_column()[1]
|
|
|
|
search = references_template % old_column_name
|
|
|
|
replacement = references_template % new_column_name
|
|
|
|
cursor.execute('UPDATE sqlite_master SET sql = replace(sql, %s, %s)', (search, replacement))
|
|
|
|
cursor.execute('PRAGMA schema_version = %d' % (schema_version + 1))
|
|
|
|
cursor.execute('PRAGMA writable_schema = 0')
|
|
|
|
# The integrity check will raise an exception and rollback
|
|
|
|
# the transaction if the sqlite_master updates corrupt the
|
|
|
|
# database.
|
|
|
|
cursor.execute('PRAGMA integrity_check')
|
|
|
|
# Perform a VACUUM to refresh the database representation from
|
|
|
|
# the sqlite_master table.
|
|
|
|
with self.connection.cursor() as cursor:
|
|
|
|
cursor.execute('VACUUM')
|
|
|
|
else:
|
|
|
|
super().alter_field(model, old_field, new_field, strict=strict)
|
|
|
|
|
2018-10-29 17:33:41 +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
|
|
|
|
2018-12-11 15:55:54 +08:00
|
|
|
This follows the correct procedure to perform non-rename or column
|
|
|
|
addition operations based on SQLite's documentation
|
|
|
|
|
|
|
|
https://www.sqlite.org/lang_altertable.html#caution
|
|
|
|
|
2015-02-12 00:15:41 +08:00
|
|
|
The essential steps are:
|
2018-12-11 15:55:54 +08:00
|
|
|
1. Create a table with the updated definition called "new__app_model"
|
|
|
|
2. Copy the data from the existing "app_model" table to the new table
|
|
|
|
3. Drop the "app_model" table
|
|
|
|
4. Rename the "new__app_model" table to "app_model"
|
|
|
|
5. Restore any index of the previous "app_model" 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
|
|
|
|
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
|
|
|
|
]
|
|
|
|
|
2016-11-05 21:12:12 +08:00
|
|
|
constraints = list(model._meta.constraints)
|
|
|
|
|
2018-12-11 15:55:54 +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.
|
|
|
|
body_copy = copy.deepcopy(body)
|
|
|
|
|
|
|
|
# Construct a new model with the new fields to allow self referential
|
|
|
|
# primary key to resolve to. This model won't ever be materialized as a
|
|
|
|
# table and solely exists for foreign key reference resolution purposes.
|
|
|
|
# This wouldn't be required if the schema editor was operating on model
|
|
|
|
# states instead of rendered models.
|
2012-09-08 00:51:11 +08:00
|
|
|
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,
|
2016-11-05 21:12:12 +08:00
|
|
|
'constraints': constraints,
|
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)
|
2018-12-11 15:55:54 +08:00
|
|
|
body_copy['Meta'] = meta
|
|
|
|
body_copy['__module__'] = model.__module__
|
|
|
|
type(model._meta.object_name, model.__bases__, body_copy)
|
2014-05-20 23:25:59 +08:00
|
|
|
|
2018-12-11 15:55:54 +08:00
|
|
|
# Construct a model with a renamed table name.
|
|
|
|
body_copy = copy.deepcopy(body)
|
|
|
|
meta_contents = {
|
|
|
|
'app_label': model._meta.app_label,
|
|
|
|
'db_table': 'new__%s' % model._meta.db_table,
|
|
|
|
'unique_together': unique_together,
|
|
|
|
'index_together': index_together,
|
|
|
|
'indexes': indexes,
|
|
|
|
'constraints': constraints,
|
|
|
|
'apps': apps,
|
|
|
|
}
|
|
|
|
meta = type("Meta", (), meta_contents)
|
|
|
|
body_copy['Meta'] = meta
|
|
|
|
body_copy['__module__'] = model.__module__
|
|
|
|
new_model = type('New%s' % model._meta.object_name, model.__bases__, body_copy)
|
2015-02-12 00:15:41 +08:00
|
|
|
|
2018-12-11 15:55:54 +08:00
|
|
|
# Create a new table with the updated schema.
|
|
|
|
self.create_model(new_model)
|
2015-02-12 00:15:41 +08:00
|
|
|
|
2018-12-11 15:55:54 +08:00
|
|
|
# Copy data from the old table into the new table
|
|
|
|
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
|
|
|
|
self.quote_name(new_model._meta.db_table),
|
|
|
|
', '.join(self.quote_name(x) for x in mapping),
|
|
|
|
', '.join(mapping.values()),
|
|
|
|
self.quote_name(model._meta.db_table),
|
|
|
|
))
|
2015-04-02 00:57:53 +08:00
|
|
|
|
2018-12-11 15:55:54 +08:00
|
|
|
# Delete the old table to make way for the new
|
|
|
|
self.delete_model(model, handle_autom2m=False)
|
2015-04-02 00:57:53 +08:00
|
|
|
|
2018-12-11 15:55:54 +08:00
|
|
|
# Rename the new table to take way for the old
|
|
|
|
self.alter_db_table(
|
|
|
|
new_model, new_model._meta.db_table, model._meta.db_table,
|
|
|
|
disable_constraints=False,
|
|
|
|
)
|
2015-02-12 00:15:41 +08:00
|
|
|
|
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."""
|
2018-10-25 19:22:40 +08:00
|
|
|
# Use "ALTER TABLE ... RENAME COLUMN" if only the column name
|
|
|
|
# changed and there aren't any constraints.
|
|
|
|
if (self.connection.features.can_alter_table_rename_column and
|
|
|
|
old_field.column != new_field.column and
|
|
|
|
self.column_sql(model, old_field) == self.column_sql(model, new_field) and
|
|
|
|
not (old_field.remote_field and old_field.db_constraint or
|
|
|
|
new_field.remote_field and new_field.db_constraint)):
|
|
|
|
return self.execute(self._rename_field_sql(model._meta.db_table, old_field, new_field, new_type))
|
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)
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def add_constraint(self, model, constraint):
|
2018-12-28 03:21:59 +08:00
|
|
|
if isinstance(constraint, UniqueConstraint) and constraint.condition:
|
|
|
|
super().add_constraint(model, constraint)
|
|
|
|
else:
|
|
|
|
self._remake_table(model)
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def remove_constraint(self, model, constraint):
|
2018-12-28 03:21:59 +08:00
|
|
|
if isinstance(constraint, UniqueConstraint) and constraint.condition:
|
|
|
|
super().remove_constraint(model, constraint)
|
|
|
|
else:
|
|
|
|
self._remake_table(model)
|