2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
2015-02-10 02:19:34 +08:00
|
|
|
|
2014-05-08 05:28:34 +08:00
|
|
|
from django.db.transaction import atomic
|
2014-12-18 07:41:36 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2014-05-06 01:50:51 +08:00
|
|
|
|
|
|
|
|
2014-12-18 07:41:36 +08:00
|
|
|
@python_2_unicode_compatible
|
2013-05-10 23:00:55 +08:00
|
|
|
class Migration(object):
|
|
|
|
"""
|
|
|
|
The base class for all migrations.
|
|
|
|
|
|
|
|
Migration files will import this from django.db.migrations.Migration
|
|
|
|
and subclass it as a class called Migration. It will have one or more
|
|
|
|
of the following attributes:
|
|
|
|
|
|
|
|
- operations: A list of Operation instances, probably from django.db.migrations.operations
|
|
|
|
- dependencies: A list of tuples of (app_path, migration_name)
|
|
|
|
- run_before: A list of tuples of (app_path, migration_name)
|
|
|
|
- replaces: A list of migration_names
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
Note that all migrations come out of migrations and into the Loader or
|
2014-03-02 22:25:53 +08:00
|
|
|
Graph as instances, having been initialized with their app label and name.
|
2013-05-10 23:00:55 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Operations to apply during this migration, in order.
|
|
|
|
operations = []
|
|
|
|
|
|
|
|
# Other migrations that should be run before this migration.
|
|
|
|
# Should be a list of (app, migration_name).
|
|
|
|
dependencies = []
|
|
|
|
|
|
|
|
# Other migrations that should be run after this one (i.e. have
|
|
|
|
# this migration added to their dependencies). Useful to make third-party
|
|
|
|
# apps' migrations run after your AUTH_USER replacement, for example.
|
|
|
|
run_before = []
|
|
|
|
|
|
|
|
# Migration names in this app that this migration replaces. If this is
|
|
|
|
# non-empty, this migration will only be applied if all these migrations
|
|
|
|
# are not applied.
|
|
|
|
replaces = []
|
2013-05-30 00:47:10 +08:00
|
|
|
|
2013-09-25 20:58:07 +08:00
|
|
|
# Error class which is raised when a migration is irreversible
|
|
|
|
class IrreversibleError(RuntimeError):
|
|
|
|
pass
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def __init__(self, name, app_label):
|
|
|
|
self.name = name
|
|
|
|
self.app_label = app_label
|
2013-10-24 05:56:54 +08:00
|
|
|
# Copy dependencies & other attrs as we might mutate them at runtime
|
|
|
|
self.operations = list(self.__class__.operations)
|
|
|
|
self.dependencies = list(self.__class__.dependencies)
|
|
|
|
self.run_before = list(self.__class__.run_before)
|
|
|
|
self.replaces = list(self.__class__.replaces)
|
2013-05-30 00:47:10 +08:00
|
|
|
|
2013-05-31 01:08:58 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(other, Migration):
|
|
|
|
return False
|
|
|
|
return (self.name == other.name) and (self.app_label == other.app_label)
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not (self == other)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Migration %s.%s>" % (self.app_label, self.name)
|
2013-06-08 01:47:17 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s.%s" % (self.app_label, self.name)
|
2013-05-31 01:08:58 +08:00
|
|
|
|
2013-06-07 22:49:48 +08:00
|
|
|
def __hash__(self):
|
|
|
|
return hash("%s.%s" % (self.app_label, self.name))
|
|
|
|
|
2015-04-01 05:35:41 +08:00
|
|
|
def mutate_state(self, project_state, preserve=True):
|
2013-05-30 00:47:10 +08:00
|
|
|
"""
|
|
|
|
Takes a ProjectState and returns a new one with the migration's
|
2015-04-01 05:35:41 +08:00
|
|
|
operations applied to it. Preserves the original object state by
|
|
|
|
default and will return a mutated state from a copy.
|
2013-05-30 00:47:10 +08:00
|
|
|
"""
|
2015-04-01 05:35:41 +08:00
|
|
|
new_state = project_state
|
|
|
|
if preserve:
|
|
|
|
new_state = project_state.clone()
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
for operation in self.operations:
|
|
|
|
operation.state_forwards(self.app_label, new_state)
|
|
|
|
return new_state
|
2013-05-31 01:08:58 +08:00
|
|
|
|
2013-09-25 21:37:44 +08:00
|
|
|
def apply(self, project_state, schema_editor, collect_sql=False):
|
2013-05-31 01:08:58 +08:00
|
|
|
"""
|
|
|
|
Takes a project_state representing all migrations prior to this one
|
|
|
|
and a schema_editor for a live database and applies the migration
|
|
|
|
in a forwards order.
|
|
|
|
|
|
|
|
Returns the resulting project state for efficient re-use by following
|
|
|
|
Migrations.
|
|
|
|
"""
|
|
|
|
for operation in self.operations:
|
2013-09-25 21:37:44 +08:00
|
|
|
# If this operation cannot be represented as SQL, place a comment
|
|
|
|
# there instead
|
|
|
|
if collect_sql and not operation.reduces_to_sql:
|
|
|
|
schema_editor.collected_sql.append("--")
|
2014-10-25 12:42:44 +08:00
|
|
|
schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE "
|
|
|
|
"WRITTEN AS SQL:")
|
2013-09-25 21:37:44 +08:00
|
|
|
schema_editor.collected_sql.append("-- %s" % operation.describe())
|
|
|
|
schema_editor.collected_sql.append("--")
|
|
|
|
continue
|
2014-11-06 03:53:39 +08:00
|
|
|
# Save the state before the operation has run
|
|
|
|
old_state = project_state.clone()
|
|
|
|
operation.state_forwards(self.app_label, project_state)
|
2013-05-31 01:08:58 +08:00
|
|
|
# Run the operation
|
2014-05-08 05:28:34 +08:00
|
|
|
if not schema_editor.connection.features.can_rollback_ddl and operation.atomic:
|
|
|
|
# We're forcing a transaction on a non-transactional-DDL backend
|
|
|
|
with atomic(schema_editor.connection.alias):
|
2014-11-06 03:53:39 +08:00
|
|
|
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
|
2014-05-08 05:28:34 +08:00
|
|
|
else:
|
|
|
|
# Normal behaviour
|
2014-11-06 03:53:39 +08:00
|
|
|
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
|
2013-05-31 01:08:58 +08:00
|
|
|
return project_state
|
|
|
|
|
2013-09-25 21:37:44 +08:00
|
|
|
def unapply(self, project_state, schema_editor, collect_sql=False):
|
2013-05-31 01:08:58 +08:00
|
|
|
"""
|
|
|
|
Takes a project_state representing all migrations prior to this one
|
|
|
|
and a schema_editor for a live database and applies the migration
|
|
|
|
in a reverse order.
|
2015-01-10 22:18:06 +08:00
|
|
|
|
|
|
|
The backwards migration process consists of two phases:
|
|
|
|
|
|
|
|
1. The intermediate states from right before the first until right
|
2015-01-11 07:30:47 +08:00
|
|
|
after the last operation inside this migration are preserved.
|
2015-01-10 22:18:06 +08:00
|
|
|
2. The operations are applied in reverse order using the states
|
|
|
|
recorded in step 1.
|
2013-05-31 01:08:58 +08:00
|
|
|
"""
|
2015-01-10 22:18:06 +08:00
|
|
|
# Construct all the intermediate states we need for a reverse migration
|
2013-05-31 01:08:58 +08:00
|
|
|
to_run = []
|
2015-01-10 22:18:06 +08:00
|
|
|
new_state = project_state
|
|
|
|
# Phase 1
|
2013-05-31 01:08:58 +08:00
|
|
|
for operation in self.operations:
|
2013-09-25 21:37:44 +08:00
|
|
|
# If it's irreversible, error out
|
2013-09-25 20:58:07 +08:00
|
|
|
if not operation.reversible:
|
2013-09-25 21:37:44 +08:00
|
|
|
raise Migration.IrreversibleError("Operation %s in %s is not reversible" % (operation, self))
|
2015-01-10 22:18:06 +08:00
|
|
|
# Preserve new state from previous run to not tamper the same state
|
|
|
|
# over all operations
|
|
|
|
new_state = new_state.clone()
|
|
|
|
old_state = new_state.clone()
|
|
|
|
operation.state_forwards(self.app_label, new_state)
|
|
|
|
to_run.insert(0, (operation, old_state, new_state))
|
|
|
|
|
|
|
|
# Phase 2
|
2013-05-31 01:08:58 +08:00
|
|
|
for operation, to_state, from_state in to_run:
|
2015-01-10 22:18:06 +08:00
|
|
|
if collect_sql:
|
|
|
|
if not operation.reduces_to_sql:
|
|
|
|
schema_editor.collected_sql.append("--")
|
|
|
|
schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE "
|
|
|
|
"WRITTEN AS SQL:")
|
|
|
|
schema_editor.collected_sql.append("-- %s" % operation.describe())
|
|
|
|
schema_editor.collected_sql.append("--")
|
|
|
|
continue
|
2014-05-08 05:28:34 +08:00
|
|
|
if not schema_editor.connection.features.can_rollback_ddl and operation.atomic:
|
|
|
|
# We're forcing a transaction on a non-transactional-DDL backend
|
|
|
|
with atomic(schema_editor.connection.alias):
|
|
|
|
operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
|
|
|
|
else:
|
|
|
|
# Normal behaviour
|
|
|
|
operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
|
2014-04-08 13:30:25 +08:00
|
|
|
return project_state
|
2014-01-15 22:20:47 +08:00
|
|
|
|
2014-04-21 01:08:04 +08:00
|
|
|
|
2014-07-30 01:22:00 +08:00
|
|
|
class SwappableTuple(tuple):
|
|
|
|
"""
|
|
|
|
Subclass of tuple so Django can tell this was originally a swappable
|
|
|
|
dependency when it reads the migration file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __new__(cls, value, setting):
|
|
|
|
self = tuple.__new__(cls, value)
|
|
|
|
self.setting = setting
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2014-01-15 22:20:47 +08:00
|
|
|
def swappable_dependency(value):
|
|
|
|
"""
|
|
|
|
Turns a setting value into a dependency.
|
|
|
|
"""
|
2014-07-30 01:22:00 +08:00
|
|
|
return SwappableTuple((value.split(".", 1)[0], "__first__"), value)
|