1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
From c6d66195d7f816aeb47a77570bdd3836a99d4183 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hertzog@debian.org>
Date: Mon, 29 May 2017 15:44:39 +0200
Subject: [PATCH 1/2] Move detect_soft_applied() from
django.db.migrations.executor to .loader
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
We want to be able to use that method in
loader.check_consistent_history() to accept an history where the initial
migration is going to be fake-applied. Since the executor has the
knowledge of the loader (but not the opposite), it makes sens to move
the code around.
Signed-off-by: Raphaƫl Hertzog <hertzog@debian.org>
Bug: https://code.djangoproject.com/ticket/28250
Bug-Debian: https://bugs.debian.org/863267
---
django/db/migrations/executor.py | 83 +--------------------------------------
django/db/migrations/loader.py | 81 ++++++++++++++++++++++++++++++++++++++
tests/migrations/test_executor.py | 12 +++---
3 files changed, 88 insertions(+), 88 deletions(-)
--- a/django/db/migrations/executor.py
+++ b/django/db/migrations/executor.py
@@ -1,8 +1,5 @@
from __future__ import unicode_literals
-from django.apps.registry import apps as global_apps
-from django.db import migrations, router
-
from .exceptions import InvalidMigrationPlan
from .loader import MigrationLoader
from .recorder import MigrationRecorder
@@ -235,7 +232,7 @@ class MigrationExecutor(object):
if not fake:
if fake_initial:
# Test to see if this is an already-applied initial migration
- applied, state = self.detect_soft_applied(state, migration)
+ applied, state = self.loader.detect_soft_applied(state, migration)
if applied:
fake = True
if not fake:
@@ -290,81 +287,3 @@ class MigrationExecutor(object):
if all_applied and key not in applied:
self.recorder.record_applied(*key)
- def detect_soft_applied(self, project_state, migration):
- """
- Tests whether a migration has been implicitly applied - that the
- tables or columns it would create exist. This is intended only for use
- on initial migrations (as it only looks for CreateModel and AddField).
- """
- def should_skip_detecting_model(migration, model):
- """
- No need to detect tables for proxy models, unmanaged models, or
- models that can't be migrated on the current database.
- """
- return (
- model._meta.proxy or not model._meta.managed or not
- router.allow_migrate(
- self.connection.alias, migration.app_label,
- model_name=model._meta.model_name,
- )
- )
-
- if migration.initial is None:
- # Bail if the migration isn't the first one in its app
- if any(app == migration.app_label for app, name in migration.dependencies):
- return False, project_state
- elif migration.initial is False:
- # Bail if it's NOT an initial migration
- return False, project_state
-
- if project_state is None:
- after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
- else:
- after_state = migration.mutate_state(project_state)
- apps = after_state.apps
- found_create_model_migration = False
- found_add_field_migration = False
- existing_table_names = self.connection.introspection.table_names(self.connection.cursor())
- # Make sure all create model and add field operations are done
- for operation in migration.operations:
- if isinstance(operation, migrations.CreateModel):
- model = apps.get_model(migration.app_label, operation.name)
- 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)
- if should_skip_detecting_model(migration, model):
- continue
- if model._meta.db_table not in existing_table_names:
- return False, project_state
- found_create_model_migration = True
- elif isinstance(operation, migrations.AddField):
- model = apps.get_model(migration.app_label, operation.model_name)
- 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)
- if should_skip_detecting_model(migration, model):
- continue
-
- table = model._meta.db_table
- field = model._meta.get_field(operation.name)
-
- # Handle implicit many-to-many tables created by AddField.
- if field.many_to_many:
- if field.remote_field.through._meta.db_table not in existing_table_names:
- return False, project_state
- else:
- found_add_field_migration = True
- continue
-
- column_names = [
- column.name for column in
- self.connection.introspection.get_table_description(self.connection.cursor(), table)
- ]
- if field.column not in column_names:
- return False, project_state
- found_add_field_migration = True
- # If we get this far and we found at least one CreateModel or AddField migration,
- # the migration is considered implicitly applied.
- return (found_create_model_migration or found_add_field_migration), after_state
--- a/django/db/migrations/loader.py
+++ b/django/db/migrations/loader.py
@@ -4,8 +4,9 @@ import os
import sys
from importlib import import_module
-from django.apps import apps
+from django.apps import apps as global_apps
from django.conf import settings
+from django.db import migrations, router
from django.db.migrations.graph import MigrationGraph
from django.db.migrations.recorder import MigrationRecorder
from django.utils import six
@@ -56,7 +57,7 @@ class MigrationLoader(object):
if app_label in settings.MIGRATION_MODULES:
return settings.MIGRATION_MODULES[app_label]
else:
- app_package_name = apps.get_app_config(app_label).name
+ app_package_name = global_apps.get_app_config(app_label).name
return '%s.%s' % (app_package_name, MIGRATIONS_MODULE_NAME)
def load_disk(self):
@@ -66,7 +67,7 @@ class MigrationLoader(object):
self.disk_migrations = {}
self.unmigrated_apps = set()
self.migrated_apps = set()
- for app_config in apps.get_app_configs():
+ for app_config in global_apps.get_app_configs():
# Get the migrations module directory
module_name = self.migrations_module(app_config.label)
if module_name is None:
@@ -315,3 +316,82 @@ class MigrationLoader(object):
See graph.make_state for the meaning of "nodes" and "at_end"
"""
return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
+
+ def detect_soft_applied(self, project_state, migration):
+ """
+ Tests whether a migration has been implicitly applied - that the
+ tables or columns it would create exist. This is intended only for use
+ on initial migrations (as it only looks for CreateModel and AddField).
+ """
+ def should_skip_detecting_model(migration, model):
+ """
+ No need to detect tables for proxy models, unmanaged models, or
+ models that can't be migrated on the current database.
+ """
+ return (
+ model._meta.proxy or not model._meta.managed or not
+ router.allow_migrate(
+ self.connection.alias, migration.app_label,
+ model_name=model._meta.model_name,
+ )
+ )
+
+ if migration.initial is None:
+ # Bail if the migration isn't the first one in its app
+ if any(app == migration.app_label for app, name in migration.dependencies):
+ return False, project_state
+ elif migration.initial is False:
+ # Bail if it's NOT an initial migration
+ return False, project_state
+
+ if project_state is None:
+ after_state = self.project_state((migration.app_label, migration.name), at_end=True)
+ else:
+ after_state = migration.mutate_state(project_state)
+ apps = after_state.apps
+ found_create_model_migration = False
+ found_add_field_migration = False
+ existing_table_names = self.connection.introspection.table_names(self.connection.cursor())
+ # Make sure all create model and add field operations are done
+ for operation in migration.operations:
+ if isinstance(operation, migrations.CreateModel):
+ model = apps.get_model(migration.app_label, operation.name)
+ 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)
+ if should_skip_detecting_model(migration, model):
+ continue
+ if model._meta.db_table not in existing_table_names:
+ return False, project_state
+ found_create_model_migration = True
+ elif isinstance(operation, migrations.AddField):
+ model = apps.get_model(migration.app_label, operation.model_name)
+ 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)
+ if should_skip_detecting_model(migration, model):
+ continue
+
+ table = model._meta.db_table
+ field = model._meta.get_field(operation.name)
+
+ # Handle implicit many-to-many tables created by AddField.
+ if field.many_to_many:
+ if field.remote_field.through._meta.db_table not in existing_table_names:
+ return False, project_state
+ else:
+ found_add_field_migration = True
+ continue
+
+ column_names = [
+ column.name for column in
+ self.connection.introspection.get_table_description(self.connection.cursor(), table)
+ ]
+ if field.column not in column_names:
+ return False, project_state
+ found_add_field_migration = True
+ # If we get this far and we found at least one CreateModel or AddField migration,
+ # the migration is considered implicitly applied.
+ return (found_create_model_migration or found_add_field_migration), after_state
--- a/tests/migrations/test_executor.py
+++ b/tests/migrations/test_executor.py
@@ -326,7 +326,7 @@ class ExecutorTests(MigrationTestBase):
global_apps.get_app_config("migrations").models["author"] = migrations_apps.get_model("migrations", "author")
try:
migration = executor.loader.get_migration("auth", "0001_initial")
- self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
+ self.assertIs(executor.loader.detect_soft_applied(None, migration)[0], True)
finally:
connection.introspection.table_names = old_table_names
del global_apps.get_app_config("migrations").models["author"]
@@ -343,7 +343,7 @@ class ExecutorTests(MigrationTestBase):
)
def test_detect_soft_applied_add_field_manytomanyfield(self):
"""
- executor.detect_soft_applied() detects ManyToManyField tables from an
+ loader.detect_soft_applied() detects ManyToManyField tables from an
AddField operation. This checks the case of AddField in a migration
with other operations (0001) and the case of AddField in its own
migration (0002).
@@ -365,9 +365,9 @@ class ExecutorTests(MigrationTestBase):
self.assertTableExists(table)
# Table detection sees 0001 is applied but not 0002.
migration = executor.loader.get_migration("migrations", "0001_initial")
- self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
+ self.assertIs(executor.loader.detect_soft_applied(None, migration)[0], True)
migration = executor.loader.get_migration("migrations", "0002_initial")
- self.assertIs(executor.detect_soft_applied(None, migration)[0], False)
+ self.assertIs(executor.loader.detect_soft_applied(None, migration)[0], False)
# Create the tables for both migrations but make it look like neither
# has been applied.
@@ -378,7 +378,7 @@ class ExecutorTests(MigrationTestBase):
executor.migrate([("migrations", None)], fake=True)
# Table detection sees 0002 is applied.
migration = executor.loader.get_migration("migrations", "0002_initial")
- self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
+ self.assertIs(executor.loader.detect_soft_applied(None, migration)[0], True)
# Leave the tables for 0001 except the many-to-many table. That missing
# table should cause detect_soft_applied() to return False.
@@ -386,7 +386,7 @@ class ExecutorTests(MigrationTestBase):
for table in tables[2:]:
editor.execute(editor.sql_delete_table % {"table": table})
migration = executor.loader.get_migration("migrations", "0001_initial")
- self.assertIs(executor.detect_soft_applied(None, migration)[0], False)
+ self.assertIs(executor.loader.detect_soft_applied(None, migration)[0], False)
# Cleanup by removing the remaining tables.
with connection.schema_editor() as editor:
|