2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
2014-05-08 05:28:34 +08:00
|
|
|
from django.db.transaction import atomic
|
2014-05-06 01:50:51 +08:00
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def mutate_state(self, project_state):
|
|
|
|
"""
|
|
|
|
Takes a ProjectState and returns a new one with the migration's
|
|
|
|
operations applied to it.
|
|
|
|
"""
|
|
|
|
new_state = project_state.clone()
|
|
|
|
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("--")
|
|
|
|
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
|
2013-05-31 01:08:58 +08:00
|
|
|
# Get the state after the operation has run
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards(self.app_label, new_state)
|
|
|
|
# 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):
|
|
|
|
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
|
|
|
|
else:
|
|
|
|
# Normal behaviour
|
|
|
|
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
|
2013-05-31 01:08:58 +08:00
|
|
|
# Switch states
|
|
|
|
project_state = new_state
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
# We need to pre-calculate the stack of project states
|
|
|
|
to_run = []
|
|
|
|
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("--")
|
|
|
|
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
|
|
|
|
# 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))
|
2013-05-31 01:08:58 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards(self.app_label, new_state)
|
|
|
|
to_run.append((operation, project_state, new_state))
|
|
|
|
project_state = new_state
|
|
|
|
# Now run them in reverse
|
|
|
|
to_run.reverse()
|
|
|
|
for operation, to_state, from_state in to_run:
|
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-01-15 22:20:47 +08:00
|
|
|
def swappable_dependency(value):
|
|
|
|
"""
|
|
|
|
Turns a setting value into a dependency.
|
|
|
|
"""
|
|
|
|
return (value.split(".", 1)[0], "__first__")
|