2014-05-08 05:28:34 +08:00
|
|
|
from django.db.transaction import atomic
|
2014-05-06 01:50:51 +08:00
|
|
|
|
2015-05-02 02:46:07 +08:00
|
|
|
from .exceptions import IrreversibleError
|
|
|
|
|
2014-05-06 01:50:51 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Migration:
|
2013-05-10 23:00:55 +08:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
2015-04-01 04:30:39 +08:00
|
|
|
# Is this an initial migration? Initial migrations are skipped on
|
|
|
|
# --fake-initial if the table or fields already exist. If None, check if
|
|
|
|
# the migration has any dependencies to determine if there are dependencies
|
|
|
|
# to tell if db introspection needs to be done. If True, always perform
|
|
|
|
# introspection. If False, never perform introspection.
|
|
|
|
initial = None
|
|
|
|
|
2016-01-31 04:46:28 +08:00
|
|
|
# Whether to wrap the whole migration in a transaction. Only has an effect
|
|
|
|
# on database backends which support transactional DDL.
|
|
|
|
atomic = True
|
|
|
|
|
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):
|
2017-09-28 17:39:12 +08:00
|
|
|
return (
|
|
|
|
isinstance(other, Migration) and
|
|
|
|
self.name == other.name and
|
|
|
|
self.app_label == other.app_label
|
|
|
|
)
|
2013-05-31 01:08:58 +08:00
|
|
|
|
|
|
|
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
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Take a ProjectState and return a new one with the migration's
|
|
|
|
operations applied to it. Preserve the original object state by
|
|
|
|
default and 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
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Take a project_state representing all migrations prior to this one
|
|
|
|
and a schema_editor for a live database and apply the migration
|
2013-05-31 01:08:58 +08:00
|
|
|
in a forwards order.
|
|
|
|
|
2017-01-25 07:04:12 +08:00
|
|
|
Return the resulting project state for efficient reuse by following
|
2013-05-31 01:08:58 +08:00
|
|
|
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
|
2015-03-29 22:59:35 +08:00
|
|
|
if collect_sql:
|
2013-09-25 21:37:44 +08:00
|
|
|
schema_editor.collected_sql.append("--")
|
2015-03-29 22:59:35 +08:00
|
|
|
if not operation.reduces_to_sql:
|
|
|
|
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("--")
|
2015-03-29 22:59:35 +08:00
|
|
|
if not operation.reduces_to_sql:
|
|
|
|
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
|
2016-01-31 04:46:28 +08:00
|
|
|
atomic_operation = operation.atomic or (self.atomic and operation.atomic is not False)
|
|
|
|
if not schema_editor.atomic_migration and atomic_operation:
|
|
|
|
# Force a transaction on a non-transactional-DDL backend or an
|
|
|
|
# atomic operation inside a non-atomic migration.
|
2014-05-08 05:28:34 +08:00
|
|
|
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
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Take a project_state representing all migrations prior to this one
|
|
|
|
and a schema_editor for a live database and apply the migration
|
2013-05-31 01:08:58 +08:00
|
|
|
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:
|
2015-05-02 02:46:07 +08:00
|
|
|
raise 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:
|
2015-03-29 22:59:35 +08:00
|
|
|
schema_editor.collected_sql.append("--")
|
|
|
|
if not operation.reduces_to_sql:
|
|
|
|
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("--")
|
2015-01-10 22:18:06 +08:00
|
|
|
if not operation.reduces_to_sql:
|
|
|
|
continue
|
2017-07-12 00:40:18 +08:00
|
|
|
atomic_operation = operation.atomic or (self.atomic and operation.atomic is not False)
|
|
|
|
if not schema_editor.atomic_migration and atomic_operation:
|
|
|
|
# Force a transaction on a non-transactional-DDL backend or an
|
|
|
|
# atomic operation inside a non-atomic migration.
|
2014-05-08 05:28:34 +08:00
|
|
|
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):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Turn a setting value into a dependency."""
|
2014-07-30 01:22:00 +08:00
|
|
|
return SwappableTuple((value.split(".", 1)[0], "__first__"), value)
|