2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-10-30 23:17:28 +08:00
|
|
|
from django.db import migrations
|
2014-07-26 00:35:25 +08:00
|
|
|
from django.apps.registry import apps as global_apps
|
2013-05-31 01:08:58 +08:00
|
|
|
from .loader import MigrationLoader
|
|
|
|
from .recorder import MigrationRecorder
|
|
|
|
|
|
|
|
|
|
|
|
class MigrationExecutor(object):
|
|
|
|
"""
|
|
|
|
End-to-end migration execution - loads migrations, and runs them
|
|
|
|
up or down to a specified set of targets.
|
|
|
|
"""
|
|
|
|
|
2013-06-08 01:47:17 +08:00
|
|
|
def __init__(self, connection, progress_callback=None):
|
2013-05-31 01:08:58 +08:00
|
|
|
self.connection = connection
|
|
|
|
self.loader = MigrationLoader(self.connection)
|
|
|
|
self.recorder = MigrationRecorder(self.connection)
|
2013-06-08 01:47:17 +08:00
|
|
|
self.progress_callback = progress_callback
|
2013-05-31 01:08:58 +08:00
|
|
|
|
|
|
|
def migration_plan(self, targets):
|
|
|
|
"""
|
|
|
|
Given a set of targets, returns a list of (Migration instance, backwards?).
|
|
|
|
"""
|
|
|
|
plan = []
|
2013-10-24 05:56:54 +08:00
|
|
|
applied = set(self.loader.applied_migrations)
|
2013-05-31 01:08:58 +08:00
|
|
|
for target in targets:
|
2013-12-28 16:53:02 +08:00
|
|
|
# If the target is (app_label, None), that means unmigrate everything
|
2013-07-23 02:43:58 +08:00
|
|
|
if target[1] is None:
|
|
|
|
for root in self.loader.graph.root_nodes():
|
|
|
|
if root[0] == target[0]:
|
|
|
|
for migration in self.loader.graph.backwards_plan(root):
|
|
|
|
if migration in applied:
|
|
|
|
plan.append((self.loader.graph.nodes[migration], True))
|
|
|
|
applied.remove(migration)
|
2013-05-31 01:08:58 +08:00
|
|
|
# If the migration is already applied, do backwards mode,
|
|
|
|
# otherwise do forwards mode.
|
2013-07-23 02:43:58 +08:00
|
|
|
elif target in applied:
|
2014-09-25 03:49:30 +08:00
|
|
|
backwards_plan = self.loader.graph.backwards_plan(target)[:-1]
|
|
|
|
# We only do this if the migration is not the most recent one
|
|
|
|
# in its app - that is, another migration with the same app
|
|
|
|
# label is in the backwards plan
|
|
|
|
if any(node[0] == target[0] for node in backwards_plan):
|
2013-08-11 22:28:51 +08:00
|
|
|
for migration in backwards_plan:
|
|
|
|
if migration in applied:
|
|
|
|
plan.append((self.loader.graph.nodes[migration], True))
|
|
|
|
applied.remove(migration)
|
2013-05-31 01:08:58 +08:00
|
|
|
else:
|
|
|
|
for migration in self.loader.graph.forwards_plan(target):
|
|
|
|
if migration not in applied:
|
|
|
|
plan.append((self.loader.graph.nodes[migration], False))
|
|
|
|
applied.add(migration)
|
|
|
|
return plan
|
|
|
|
|
2013-07-26 23:47:00 +08:00
|
|
|
def migrate(self, targets, plan=None, fake=False):
|
2013-05-31 01:08:58 +08:00
|
|
|
"""
|
|
|
|
Migrates the database up to the given targets.
|
|
|
|
"""
|
2013-06-08 01:47:17 +08:00
|
|
|
if plan is None:
|
|
|
|
plan = self.migration_plan(targets)
|
2013-05-31 01:08:58 +08:00
|
|
|
for migration, backwards in plan:
|
|
|
|
if not backwards:
|
2013-07-26 23:47:00 +08:00
|
|
|
self.apply_migration(migration, fake=fake)
|
2013-05-31 01:08:58 +08:00
|
|
|
else:
|
2013-07-26 23:47:00 +08:00
|
|
|
self.unapply_migration(migration, fake=fake)
|
2013-05-31 01:08:58 +08:00
|
|
|
|
2013-09-07 04:27:51 +08:00
|
|
|
def collect_sql(self, plan):
|
|
|
|
"""
|
|
|
|
Takes a migration plan and returns a list of collected SQL
|
|
|
|
statements that represent the best-efforts version of that plan.
|
|
|
|
"""
|
|
|
|
statements = []
|
|
|
|
for migration, backwards in plan:
|
|
|
|
with self.connection.schema_editor(collect_sql=True) as schema_editor:
|
2014-05-01 03:25:12 +08:00
|
|
|
project_state = self.loader.project_state((migration.app_label, migration.name), at_end=False)
|
2013-09-07 04:27:51 +08:00
|
|
|
if not backwards:
|
2013-09-25 21:37:44 +08:00
|
|
|
migration.apply(project_state, schema_editor, collect_sql=True)
|
2013-09-07 04:27:51 +08:00
|
|
|
else:
|
2013-09-25 21:37:44 +08:00
|
|
|
migration.unapply(project_state, schema_editor, collect_sql=True)
|
2013-11-28 00:29:37 +08:00
|
|
|
statements.extend(schema_editor.collected_sql)
|
2013-09-07 04:27:51 +08:00
|
|
|
return statements
|
|
|
|
|
2013-07-26 23:47:00 +08:00
|
|
|
def apply_migration(self, migration, fake=False):
|
2013-05-31 01:08:58 +08:00
|
|
|
"""
|
|
|
|
Runs a migration forwards.
|
|
|
|
"""
|
2013-06-08 01:47:17 +08:00
|
|
|
if self.progress_callback:
|
2013-10-30 23:17:28 +08:00
|
|
|
self.progress_callback("apply_start", migration, fake)
|
2013-07-26 23:47:00 +08:00
|
|
|
if not fake:
|
2013-10-30 23:17:28 +08:00
|
|
|
# Test to see if this is an already-applied initial migration
|
2014-01-24 20:26:19 +08:00
|
|
|
if self.detect_soft_applied(migration):
|
2013-10-30 23:17:28 +08:00
|
|
|
fake = True
|
|
|
|
else:
|
|
|
|
# Alright, do it normally
|
|
|
|
with self.connection.schema_editor() as schema_editor:
|
2014-05-01 03:25:12 +08:00
|
|
|
project_state = self.loader.project_state((migration.app_label, migration.name), at_end=False)
|
2013-10-30 23:17:28 +08:00
|
|
|
migration.apply(project_state, schema_editor)
|
2013-10-24 05:56:54 +08:00
|
|
|
# For replacement migrations, record individual statuses
|
|
|
|
if migration.replaces:
|
|
|
|
for app_label, name in migration.replaces:
|
|
|
|
self.recorder.record_applied(app_label, name)
|
|
|
|
else:
|
|
|
|
self.recorder.record_applied(migration.app_label, migration.name)
|
2013-10-30 23:17:28 +08:00
|
|
|
# Report progress
|
2013-06-08 01:47:17 +08:00
|
|
|
if self.progress_callback:
|
2013-10-30 23:17:28 +08:00
|
|
|
self.progress_callback("apply_success", migration, fake)
|
2013-05-31 01:08:58 +08:00
|
|
|
|
2013-07-26 23:47:00 +08:00
|
|
|
def unapply_migration(self, migration, fake=False):
|
2013-05-31 01:08:58 +08:00
|
|
|
"""
|
|
|
|
Runs a migration backwards.
|
|
|
|
"""
|
2013-06-08 01:47:17 +08:00
|
|
|
if self.progress_callback:
|
2013-10-30 23:17:28 +08:00
|
|
|
self.progress_callback("unapply_start", migration, fake)
|
2013-07-26 23:47:00 +08:00
|
|
|
if not fake:
|
|
|
|
with self.connection.schema_editor() as schema_editor:
|
2014-05-01 03:25:12 +08:00
|
|
|
project_state = self.loader.project_state((migration.app_label, migration.name), at_end=False)
|
2013-07-26 23:47:00 +08:00
|
|
|
migration.unapply(project_state, schema_editor)
|
2013-10-24 05:56:54 +08:00
|
|
|
# For replacement migrations, record individual statuses
|
|
|
|
if migration.replaces:
|
|
|
|
for app_label, name in migration.replaces:
|
|
|
|
self.recorder.record_unapplied(app_label, name)
|
|
|
|
else:
|
|
|
|
self.recorder.record_unapplied(migration.app_label, migration.name)
|
|
|
|
# Report progress
|
2013-06-08 01:47:17 +08:00
|
|
|
if self.progress_callback:
|
2013-10-30 23:17:28 +08:00
|
|
|
self.progress_callback("unapply_success", migration, fake)
|
|
|
|
|
|
|
|
def detect_soft_applied(self, migration):
|
|
|
|
"""
|
2014-03-02 22:25:53 +08:00
|
|
|
Tests whether a migration has been implicitly applied - that the
|
2013-10-30 23:17:28 +08:00
|
|
|
tables it would create exist. This is intended only for use
|
|
|
|
on initial migrations (as it only looks for CreateModel).
|
|
|
|
"""
|
2014-05-01 03:25:12 +08:00
|
|
|
project_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
|
2013-12-24 19:25:17 +08:00
|
|
|
apps = project_state.render()
|
2014-06-23 09:29:27 +08:00
|
|
|
found_create_migration = False
|
2014-07-30 00:24:07 +08:00
|
|
|
# Bail if the migration isn't the first one in its app
|
|
|
|
if [name for app, name in migration.dependencies if app == migration.app_label]:
|
|
|
|
return False
|
|
|
|
# Make sure all create model are done
|
2013-10-30 23:17:28 +08:00
|
|
|
for operation in migration.operations:
|
|
|
|
if isinstance(operation, migrations.CreateModel):
|
2013-12-24 19:25:17 +08:00
|
|
|
model = apps.get_model(migration.app_label, operation.name)
|
2014-07-26 00:35:25 +08:00
|
|
|
if model._meta.swapped:
|
|
|
|
# We have to fetch the model to test with from the
|
|
|
|
# main app cache, as it's not a direct dependency.
|
|
|
|
model = global_apps.get_model(model._meta.swapped)
|
2014-09-21 06:29:13 +08:00
|
|
|
if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
|
2013-10-30 23:17:28 +08:00
|
|
|
return False
|
2014-06-23 09:29:27 +08:00
|
|
|
found_create_migration = True
|
|
|
|
# If we get this far and we found at least one CreateModel migration,
|
|
|
|
# the migration is considered implicitly applied.
|
|
|
|
return found_create_migration
|